harper_core/linting/
confident.rs

1use crate::{
2    Token,
3    patterns::{OwnedPatternExt, Pattern, SequencePattern, Word},
4};
5
6use super::{Lint, LintKind, PatternLinter, Suggestion};
7
8pub struct Confident {
9    pattern: Box<dyn Pattern>,
10}
11
12impl Default for Confident {
13    fn default() -> Self {
14        let pattern = SequencePattern::default()
15            .then(
16                (|tok: &Token, _source: &[char]| tok.kind.is_verb() || tok.kind.is_determiner())
17                    .or(Box::new(Word::new("very"))),
18            )
19            .then_whitespace()
20            .t_aco("confidant");
21
22        Self {
23            pattern: Box::new(pattern),
24        }
25    }
26}
27
28impl PatternLinter for Confident {
29    fn pattern(&self) -> &dyn Pattern {
30        self.pattern.as_ref()
31    }
32
33    fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Option<Lint> {
34        let span = matched_tokens.last()?.span;
35
36        Some(Lint {
37            span,
38            lint_kind: LintKind::WordChoice,
39            suggestions: vec![Suggestion::ReplaceWith("confident".chars().collect())],
40            message: "Use the adjective.".to_owned(),
41            priority: 127,
42        })
43    }
44
45    fn description(&self) -> &'static str {
46        "This linter detects instances where the noun `confidant` is incorrectly used in place of the adjective `confident`. `Confidant` refers to a trusted person, whereas `confident` describes certainty or self-assurance. The rule suggests replacing `confidant` with `confident` when used in an adjectival context."
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::Confident;
53    use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
54
55    #[test]
56    fn describing_person_incorrect() {
57        assert_suggestion_result(
58            "She felt confidant about her presentation.",
59            Confident::default(),
60            "She felt confident about her presentation.",
61        );
62    }
63
64    #[test]
65    fn describing_person_correct() {
66        assert_lint_count(
67            "She felt confident about her presentation.",
68            Confident::default(),
69            0,
70        );
71    }
72
73    #[test]
74    fn certainty_incorrect() {
75        assert_suggestion_result(
76            "I am confidant the test results are accurate.",
77            Confident::default(),
78            "I am confident the test results are accurate.",
79        );
80    }
81
82    #[test]
83    fn certainty_correct() {
84        assert_lint_count(
85            "I am confident the test results are accurate.",
86            Confident::default(),
87            0,
88        );
89    }
90
91    #[test]
92    fn demeanor_incorrect() {
93        assert_suggestion_result(
94            "He walked to the stage with a confidant stride.",
95            Confident::default(),
96            "He walked to the stage with a confident stride.",
97        );
98    }
99
100    #[test]
101    fn demeanor_correct() {
102        assert_lint_count(
103            "He walked to the stage with a confident stride.",
104            Confident::default(),
105            0,
106        );
107    }
108
109    #[test]
110    fn professional_incorrect() {
111        assert_suggestion_result(
112            "You should sound confidant during job interviews.",
113            Confident::default(),
114            "You should sound confident during job interviews.",
115        );
116    }
117
118    #[test]
119    fn professional_correct() {
120        assert_lint_count(
121            "You should sound confident during job interviews.",
122            Confident::default(),
123            0,
124        );
125    }
126
127    #[test]
128    fn assured_tone_incorrect() {
129        assert_suggestion_result(
130            "Present your argument in a confidant, persuasive manner.",
131            Confident::default(),
132            "Present your argument in a confident, persuasive manner.",
133        );
134    }
135
136    #[test]
137    fn assured_tone_correct() {
138        assert_lint_count(
139            "Present your argument in a confident, persuasive manner.",
140            Confident::default(),
141            0,
142        );
143    }
144
145    #[test]
146    fn extra_text_between() {
147        assert_suggestion_result(
148            "She felt very confidant about her presentation.",
149            Confident::default(),
150            "She felt very confident about her presentation.",
151        );
152    }
153
154    #[test]
155    fn linking_verb_was_confidant() {
156        assert_suggestion_result(
157            "She was confidant about her presentation.",
158            Confident::default(),
159            "She was confident about her presentation.",
160        );
161    }
162}