]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/non_expressive_names.rs
Auto merge of #101893 - oli-obk:lift_derive, r=lcnr
[rust.git] / src / tools / clippy / clippy_lints / src / non_expressive_names.rs
1 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2 use rustc_ast::ast::{
3     self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind,
4 };
5 use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
6 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
7 use rustc_middle::lint::in_external_macro;
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::source_map::Span;
10 use rustc_span::sym;
11 use rustc_span::symbol::{Ident, Symbol};
12 use std::cmp::Ordering;
13
14 declare_clippy_lint! {
15     /// ### What it does
16     /// Checks for names that are very similar and thus confusing.
17     ///
18     /// Note: this lint looks for similar names throughout each
19     /// scope. To allow it, you need to allow it on the scope
20     /// level, not on the name that is reported.
21     ///
22     /// ### Why is this bad?
23     /// It's hard to distinguish between names that differ only
24     /// by a single character.
25     ///
26     /// ### Example
27     /// ```ignore
28     /// let checked_exp = something;
29     /// let checked_expr = something_else;
30     /// ```
31     #[clippy::version = "pre 1.29.0"]
32     pub SIMILAR_NAMES,
33     pedantic,
34     "similarly named items and bindings"
35 }
36
37 declare_clippy_lint! {
38     /// ### What it does
39     /// Checks for too many variables whose name consists of a
40     /// single character.
41     ///
42     /// ### Why is this bad?
43     /// It's hard to memorize what a variable means without a
44     /// descriptive name.
45     ///
46     /// ### Example
47     /// ```ignore
48     /// let (a, b, c, d, e, f, g) = (...);
49     /// ```
50     #[clippy::version = "pre 1.29.0"]
51     pub MANY_SINGLE_CHAR_NAMES,
52     pedantic,
53     "too many single character bindings"
54 }
55
56 declare_clippy_lint! {
57     /// ### What it does
58     /// Checks if you have variables whose name consists of just
59     /// underscores and digits.
60     ///
61     /// ### Why is this bad?
62     /// It's hard to memorize what a variable means without a
63     /// descriptive name.
64     ///
65     /// ### Example
66     /// ```rust
67     /// let _1 = 1;
68     /// let ___1 = 1;
69     /// let __1___2 = 11;
70     /// ```
71     #[clippy::version = "pre 1.29.0"]
72     pub JUST_UNDERSCORES_AND_DIGITS,
73     style,
74     "unclear name"
75 }
76
77 #[derive(Copy, Clone)]
78 pub struct NonExpressiveNames {
79     pub single_char_binding_names_threshold: u64,
80 }
81
82 impl_lint_pass!(NonExpressiveNames => [SIMILAR_NAMES, MANY_SINGLE_CHAR_NAMES, JUST_UNDERSCORES_AND_DIGITS]);
83
84 struct ExistingName {
85     interned: Symbol,
86     span: Span,
87     len: usize,
88     exemptions: &'static [&'static str],
89 }
90
91 struct SimilarNamesLocalVisitor<'a, 'tcx> {
92     names: Vec<ExistingName>,
93     cx: &'a EarlyContext<'tcx>,
94     lint: &'a NonExpressiveNames,
95
96     /// A stack of scopes containing the single-character bindings in each scope.
97     single_char_names: Vec<Vec<Ident>>,
98 }
99
100 impl<'a, 'tcx> SimilarNamesLocalVisitor<'a, 'tcx> {
101     fn check_single_char_names(&self) {
102         let num_single_char_names = self.single_char_names.iter().flatten().count();
103         let threshold = self.lint.single_char_binding_names_threshold;
104         if num_single_char_names as u64 > threshold {
105             let span = self
106                 .single_char_names
107                 .iter()
108                 .flatten()
109                 .map(|ident| ident.span)
110                 .collect::<Vec<_>>();
111             span_lint(
112                 self.cx,
113                 MANY_SINGLE_CHAR_NAMES,
114                 span,
115                 &format!(
116                     "{} bindings with single-character names in scope",
117                     num_single_char_names
118                 ),
119             );
120         }
121     }
122 }
123
124 // this list contains lists of names that are allowed to be similar
125 // the assumption is that no name is ever contained in multiple lists.
126 #[rustfmt::skip]
127 const ALLOWED_TO_BE_SIMILAR: &[&[&str]] = &[
128     &["parsed", "parser"],
129     &["lhs", "rhs"],
130     &["tx", "rx"],
131     &["set", "get"],
132     &["args", "arms"],
133     &["qpath", "path"],
134     &["lit", "lint"],
135     &["wparam", "lparam"],
136     &["iter", "item"],
137 ];
138
139 struct SimilarNamesNameVisitor<'a, 'tcx, 'b>(&'b mut SimilarNamesLocalVisitor<'a, 'tcx>);
140
141 impl<'a, 'tcx, 'b> Visitor<'tcx> for SimilarNamesNameVisitor<'a, 'tcx, 'b> {
142     fn visit_pat(&mut self, pat: &'tcx Pat) {
143         match pat.kind {
144             PatKind::Ident(_, ident, _) => {
145                 if !pat.span.from_expansion() {
146                     self.check_ident(ident);
147                 }
148             },
149             PatKind::Struct(_, _, ref fields, _) => {
150                 for field in fields {
151                     if !field.is_shorthand {
152                         self.visit_pat(&field.pat);
153                     }
154                 }
155             },
156             // just go through the first pattern, as either all patterns
157             // bind the same bindings or rustc would have errored much earlier
158             PatKind::Or(ref pats) => self.visit_pat(&pats[0]),
159             _ => walk_pat(self, pat),
160         }
161     }
162 }
163
164 #[must_use]
165 fn get_exemptions(interned_name: &str) -> Option<&'static [&'static str]> {
166     ALLOWED_TO_BE_SIMILAR
167         .iter()
168         .find(|&&list| allowed_to_be_similar(interned_name, list))
169         .copied()
170 }
171
172 #[must_use]
173 fn allowed_to_be_similar(interned_name: &str, list: &[&str]) -> bool {
174     list.iter()
175         .any(|&name| interned_name.starts_with(name) || interned_name.ends_with(name))
176 }
177
178 impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
179     fn check_short_ident(&mut self, ident: Ident) {
180         // Ignore shadowing
181         if self
182             .0
183             .single_char_names
184             .iter()
185             .flatten()
186             .any(|id| id.name == ident.name)
187         {
188             return;
189         }
190
191         if let Some(scope) = &mut self.0.single_char_names.last_mut() {
192             scope.push(ident);
193         }
194     }
195
196     #[expect(clippy::too_many_lines)]
197     fn check_ident(&mut self, ident: Ident) {
198         let interned_name = ident.name.as_str();
199         if interned_name.chars().any(char::is_uppercase) {
200             return;
201         }
202         if interned_name.chars().all(|c| c.is_ascii_digit() || c == '_') {
203             span_lint(
204                 self.0.cx,
205                 JUST_UNDERSCORES_AND_DIGITS,
206                 ident.span,
207                 "consider choosing a more descriptive name",
208             );
209             return;
210         }
211         if interned_name.starts_with('_') {
212             // these bindings are typically unused or represent an ignored portion of a destructuring pattern
213             return;
214         }
215         let count = interned_name.chars().count();
216         if count < 3 {
217             if count == 1 {
218                 self.check_short_ident(ident);
219             }
220             return;
221         }
222         for existing_name in &self.0.names {
223             if allowed_to_be_similar(interned_name, existing_name.exemptions) {
224                 continue;
225             }
226             match existing_name.len.cmp(&count) {
227                 Ordering::Greater => {
228                     if existing_name.len - count != 1
229                         || levenstein_not_1(interned_name, existing_name.interned.as_str())
230                     {
231                         continue;
232                     }
233                 },
234                 Ordering::Less => {
235                     if count - existing_name.len != 1
236                         || levenstein_not_1(existing_name.interned.as_str(), interned_name)
237                     {
238                         continue;
239                     }
240                 },
241                 Ordering::Equal => {
242                     let mut interned_chars = interned_name.chars();
243                     let interned_str = existing_name.interned.as_str();
244                     let mut existing_chars = interned_str.chars();
245                     let first_i = interned_chars.next().expect("we know we have at least one char");
246                     let first_e = existing_chars.next().expect("we know we have at least one char");
247                     let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric();
248
249                     if eq_or_numeric((first_i, first_e)) {
250                         let last_i = interned_chars.next_back().expect("we know we have at least two chars");
251                         let last_e = existing_chars.next_back().expect("we know we have at least two chars");
252                         if eq_or_numeric((last_i, last_e)) {
253                             if interned_chars
254                                 .zip(existing_chars)
255                                 .filter(|&ie| !eq_or_numeric(ie))
256                                 .count()
257                                 != 1
258                             {
259                                 continue;
260                             }
261                         } else {
262                             let second_last_i = interned_chars
263                                 .next_back()
264                                 .expect("we know we have at least three chars");
265                             let second_last_e = existing_chars
266                                 .next_back()
267                                 .expect("we know we have at least three chars");
268                             if !eq_or_numeric((second_last_i, second_last_e))
269                                 || second_last_i == '_'
270                                 || !interned_chars.zip(existing_chars).all(eq_or_numeric)
271                             {
272                                 // allowed similarity foo_x, foo_y
273                                 // or too many chars differ (foo_x, boo_y) or (foox, booy)
274                                 continue;
275                             }
276                         }
277                     } else {
278                         let second_i = interned_chars.next().expect("we know we have at least two chars");
279                         let second_e = existing_chars.next().expect("we know we have at least two chars");
280                         if !eq_or_numeric((second_i, second_e))
281                             || second_i == '_'
282                             || !interned_chars.zip(existing_chars).all(eq_or_numeric)
283                         {
284                             // allowed similarity x_foo, y_foo
285                             // or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
286                             continue;
287                         }
288                     }
289                 },
290             }
291             span_lint_and_then(
292                 self.0.cx,
293                 SIMILAR_NAMES,
294                 ident.span,
295                 "binding's name is too similar to existing binding",
296                 |diag| {
297                     diag.span_note(existing_name.span, "existing binding defined here");
298                 },
299             );
300             return;
301         }
302         self.0.names.push(ExistingName {
303             exemptions: get_exemptions(interned_name).unwrap_or(&[]),
304             interned: ident.name,
305             span: ident.span,
306             len: count,
307         });
308     }
309 }
310
311 impl<'a, 'b> SimilarNamesLocalVisitor<'a, 'b> {
312     /// ensure scoping rules work
313     fn apply<F: for<'c> Fn(&'c mut Self)>(&mut self, f: F) {
314         let n = self.names.len();
315         let single_char_count = self.single_char_names.len();
316         f(self);
317         self.names.truncate(n);
318         self.single_char_names.truncate(single_char_count);
319     }
320 }
321
322 impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
323     fn visit_local(&mut self, local: &'tcx Local) {
324         if let Some((init, els)) = &local.kind.init_else_opt() {
325             self.apply(|this| walk_expr(this, init));
326             if let Some(els) = els {
327                 self.apply(|this| walk_block(this, els));
328             }
329         }
330         // add the pattern after the expression because the bindings aren't available
331         // yet in the init
332         // expression
333         SimilarNamesNameVisitor(self).visit_pat(&local.pat);
334     }
335     fn visit_block(&mut self, blk: &'tcx Block) {
336         self.single_char_names.push(vec![]);
337
338         self.apply(|this| walk_block(this, blk));
339
340         self.check_single_char_names();
341         self.single_char_names.pop();
342     }
343     fn visit_arm(&mut self, arm: &'tcx Arm) {
344         self.single_char_names.push(vec![]);
345
346         self.apply(|this| {
347             SimilarNamesNameVisitor(this).visit_pat(&arm.pat);
348             this.apply(|this| walk_expr(this, &arm.body));
349         });
350
351         self.check_single_char_names();
352         self.single_char_names.pop();
353     }
354     fn visit_item(&mut self, _: &Item) {
355         // do not recurse into inner items
356     }
357 }
358
359 impl EarlyLintPass for NonExpressiveNames {
360     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
361         if in_external_macro(cx.sess(), item.span) {
362             return;
363         }
364
365         if let ItemKind::Fn(box ast::Fn {
366             ref sig,
367             body: Some(ref blk),
368             ..
369         }) = item.kind
370         {
371             do_check(self, cx, &item.attrs, &sig.decl, blk);
372         }
373     }
374
375     fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &AssocItem) {
376         if in_external_macro(cx.sess(), item.span) {
377             return;
378         }
379
380         if let AssocItemKind::Fn(box ast::Fn {
381             ref sig,
382             body: Some(ref blk),
383             ..
384         }) = item.kind
385         {
386             do_check(self, cx, &item.attrs, &sig.decl, blk);
387         }
388     }
389 }
390
391 fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attribute], decl: &FnDecl, blk: &Block) {
392     if !attrs.iter().any(|attr| attr.has_name(sym::test)) {
393         let mut visitor = SimilarNamesLocalVisitor {
394             names: Vec::new(),
395             cx,
396             lint,
397             single_char_names: vec![vec![]],
398         };
399
400         // initialize with function arguments
401         for arg in &decl.inputs {
402             SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat);
403         }
404         // walk all other bindings
405         walk_block(&mut visitor, blk);
406
407         visitor.check_single_char_names();
408     }
409 }
410
411 /// Precondition: `a_name.chars().count() < b_name.chars().count()`.
412 #[must_use]
413 fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
414     debug_assert!(a_name.chars().count() < b_name.chars().count());
415     let mut a_chars = a_name.chars();
416     let mut b_chars = b_name.chars();
417     while let (Some(a), Some(b)) = (a_chars.next(), b_chars.next()) {
418         if a == b {
419             continue;
420         }
421         if let Some(b2) = b_chars.next() {
422             // check if there's just one character inserted
423             return a != b2 || a_chars.ne(b_chars);
424         }
425         // tuple
426         // ntuple
427         return true;
428     }
429     // for item in items
430     true
431 }