Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: flag redundant "almost" + "nearly" (#4408)
Andrew Dunbare75ebc99c306
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex ce47c6b54..becc7164e 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -3235,6 +3235,13 @@"label": "Redundant Additive Adverbs"}},+ {+ "Bool": {+ "name": "RedundantAlmostNearly",+ "state": true,+ "label": "Redundant Almost Nearly"+ }+ },{"Bool": {"name": "RedundantIIRC",diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex 426b4e142..ca917daf3 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -239,6 +239,7 @@ use super::quote_spacing::QuoteSpacing;use super::reason_for_doing::ReasonForDoing;use super::redundant_acronyms::RedundantAcronyms;use super::redundant_additive_adverbs::RedundantAdditiveAdverbs;+use super::redundant_almost_nearly::RedundantAlmostNearly;use super::redundant_firsts::RedundantFirsts;use super::redundant_progressive_comparative::RedundantProgressiveComparative;use super::redundant_self::RedundantSelf;@@ -858,6 +859,7 @@ impl LintGroup {insert_expr_rule!(ReasonForDoing);insert_expr_rule!(RedundantAcronyms);insert_expr_rule!(RedundantAdditiveAdverbs);+ insert_expr_rule!(RedundantAlmostNearly);insert_expr_rule!(RedundantFirsts);insert_expr_rule!(RedundantProgressiveComparative);insert_expr_rule!(RedundantSelf);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 8e59f4d60..66f526eed 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -256,6 +256,7 @@ mod quote_spacing;mod reason_for_doing;mod redundant_acronyms;mod redundant_additive_adverbs;+mod redundant_almost_nearly;mod redundant_firsts;mod redundant_progressive_comparative;mod redundant_self;diff --git a/harper-core/src/linting/redundant_almost_nearly.rs b/harper-core/src/linting/redundant_almost_nearly.rsnew file mode 100644index 000000000..2bf7d80dd--- /dev/null+++ b/harper-core/src/linting/redundant_almost_nearly.rs@@ -0,0 +1,78 @@+use crate::{+ Token, TokenStringExt,+ expr::{Expr, FirstMatchOf, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct RedundantAlmostNearly {+ expr: FirstMatchOf,+}++impl Default for RedundantAlmostNearly {+ fn default() -> Self {+ Self {+ expr: FirstMatchOf::new(vec![+ Box::new(SequenceExpr::word_seq(&["almost", "nearly"])),+ Box::new(SequenceExpr::word_seq(&["nearly", "almost"])),+ ]),+ }+ }+}++impl ExprLinter for RedundantAlmostNearly {+ type Unit = Chunk;++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let span = toks.span()?;+ Some(Lint {+ span,+ lint_kind: LintKind::Redundancy,+ suggestions: vec![+ Suggestion::replace_with_match_case_str("almost", span.get_content(src)),+ Suggestion::replace_with_match_case_str("nearly", span.get_content(src)),+ ],+ message: "Use just one of `almost` or `nearly`.".to_string(),+ priority: 31,+ })+ }++ fn description(&self) -> &'static str {+ "Detects `almost` and `nearly` redundantly used together."+ }+}++#[cfg(test)]+mod tests {+ use super::RedundantAlmostNearly;+ use crate::linting::tests::assert_good_and_bad_suggestions;++ #[test]+ fn almost_nearly() {+ assert_good_and_bad_suggestions(+ "Sub-word encoding models are almost nearly identical to the Character encoding models.",+ RedundantAlmostNearly::default(),+ &[+ "Sub-word encoding models are almost identical to the Character encoding models.",+ "Sub-word encoding models are nearly identical to the Character encoding models.",+ ],+ &[],+ );+ }++ #[test]+ fn nearly_almost() {+ assert_good_and_bad_suggestions(+ "Running Openshift 3.11 inside WSL2. This is not running yet, but nearly almost.",+ RedundantAlmostNearly::default(),+ &[+ "Running Openshift 3.11 inside WSL2. This is not running yet, but almost.",+ "Running Openshift 3.11 inside WSL2. This is not running yet, but nearly.",+ ],+ &[],+ );+ }+}