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