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