]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/non_expressive_names.rs
Add a new lint for comparison chains
[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, LintArray, LintPass};
3 use rustc::{declare_tool_lint, impl_lint_pass};
4 use std::cmp::Ordering;
5 use syntax::ast::*;
6 use syntax::attr;
7 use syntax::source_map::Span;
8 use syntax::symbol::LocalInternedString;
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: LocalInternedString,
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.node {
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 fn get_whitelist(interned_name: &str) -> Option<&'static [&'static str]> {
152     for &allow in WHITELIST {
153         if whitelisted(interned_name, allow) {
154             return Some(allow);
155         }
156     }
157     None
158 }
159
160 fn whitelisted(interned_name: &str, list: &[&str]) -> bool {
161     list.iter()
162         .any(|&name| interned_name.starts_with(name) || interned_name.ends_with(name))
163 }
164
165 impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
166     fn check_short_ident(&mut self, ident: Ident) {
167         // Ignore shadowing
168         if self
169             .0
170             .single_char_names
171             .iter()
172             .flatten()
173             .any(|id| id.name == ident.name)
174         {
175             return;
176         }
177
178         if let Some(scope) = &mut self.0.single_char_names.last_mut() {
179             scope.push(ident);
180         }
181     }
182
183     #[allow(clippy::too_many_lines)]
184     fn check_ident(&mut self, ident: Ident) {
185         let interned_name = ident.name.as_str();
186         if interned_name.chars().any(char::is_uppercase) {
187             return;
188         }
189         if interned_name.chars().all(|c| c.is_digit(10) || c == '_') {
190             span_lint(
191                 self.0.cx,
192                 JUST_UNDERSCORES_AND_DIGITS,
193                 ident.span,
194                 "consider choosing a more descriptive name",
195             );
196             return;
197         }
198         let count = interned_name.chars().count();
199         if count < 3 {
200             if count == 1 {
201                 self.check_short_ident(ident);
202             }
203             return;
204         }
205         for existing_name in &self.0.names {
206             if whitelisted(&interned_name, existing_name.whitelist) {
207                 continue;
208             }
209             let mut split_at = None;
210             match existing_name.len.cmp(&count) {
211                 Ordering::Greater => {
212                     if existing_name.len - count != 1 || levenstein_not_1(&interned_name, &existing_name.interned) {
213                         continue;
214                     }
215                 },
216                 Ordering::Less => {
217                     if count - existing_name.len != 1 || levenstein_not_1(&existing_name.interned, &interned_name) {
218                         continue;
219                     }
220                 },
221                 Ordering::Equal => {
222                     let mut interned_chars = interned_name.chars();
223                     let mut existing_chars = existing_name.interned.chars();
224                     let first_i = interned_chars.next().expect("we know we have at least one char");
225                     let first_e = existing_chars.next().expect("we know we have at least one char");
226                     let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric();
227
228                     if eq_or_numeric((first_i, first_e)) {
229                         let last_i = interned_chars.next_back().expect("we know we have at least two chars");
230                         let last_e = existing_chars.next_back().expect("we know we have at least two chars");
231                         if eq_or_numeric((last_i, last_e)) {
232                             if interned_chars
233                                 .zip(existing_chars)
234                                 .filter(|&ie| !eq_or_numeric(ie))
235                                 .count()
236                                 != 1
237                             {
238                                 continue;
239                             }
240                         } else {
241                             let second_last_i = interned_chars
242                                 .next_back()
243                                 .expect("we know we have at least three chars");
244                             let second_last_e = existing_chars
245                                 .next_back()
246                                 .expect("we know we have at least three chars");
247                             if !eq_or_numeric((second_last_i, second_last_e))
248                                 || second_last_i == '_'
249                                 || !interned_chars.zip(existing_chars).all(eq_or_numeric)
250                             {
251                                 // allowed similarity foo_x, foo_y
252                                 // or too many chars differ (foo_x, boo_y) or (foox, booy)
253                                 continue;
254                             }
255                             split_at = interned_name.char_indices().rev().next().map(|(i, _)| i);
256                         }
257                     } else {
258                         let second_i = interned_chars.next().expect("we know we have at least two chars");
259                         let second_e = existing_chars.next().expect("we know we have at least two chars");
260                         if !eq_or_numeric((second_i, second_e))
261                             || second_i == '_'
262                             || !interned_chars.zip(existing_chars).all(eq_or_numeric)
263                         {
264                             // allowed similarity x_foo, y_foo
265                             // or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
266                             continue;
267                         }
268                         split_at = interned_name.chars().next().map(char::len_utf8);
269                     }
270                 },
271             }
272             span_lint_and_then(
273                 self.0.cx,
274                 SIMILAR_NAMES,
275                 ident.span,
276                 "binding's name is too similar to existing binding",
277                 |diag| {
278                     diag.span_note(existing_name.span, "existing binding defined here");
279                     if let Some(split) = split_at {
280                         diag.span_help(
281                             ident.span,
282                             &format!(
283                                 "separate the discriminating character by an \
284                                  underscore like: `{}_{}`",
285                                 &interned_name[..split],
286                                 &interned_name[split..]
287                             ),
288                         );
289                     }
290                 },
291             );
292             return;
293         }
294         self.0.names.push(ExistingName {
295             whitelist: get_whitelist(&interned_name).unwrap_or(&[]),
296             interned: interned_name,
297             span: ident.span,
298             len: count,
299         });
300     }
301 }
302
303 impl<'a, 'b> SimilarNamesLocalVisitor<'a, 'b> {
304     /// ensure scoping rules work
305     fn apply<F: for<'c> Fn(&'c mut Self)>(&mut self, f: F) {
306         let n = self.names.len();
307         let single_char_count = self.single_char_names.len();
308         f(self);
309         self.names.truncate(n);
310         self.single_char_names.truncate(single_char_count);
311     }
312 }
313
314 impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
315     fn visit_local(&mut self, local: &'tcx Local) {
316         if let Some(ref init) = local.init {
317             self.apply(|this| walk_expr(this, &**init));
318         }
319         // add the pattern after the expression because the bindings aren't available
320         // yet in the init
321         // expression
322         SimilarNamesNameVisitor(self).visit_pat(&*local.pat);
323     }
324     fn visit_block(&mut self, blk: &'tcx Block) {
325         self.single_char_names.push(vec![]);
326
327         self.apply(|this| walk_block(this, blk));
328
329         self.check_single_char_names();
330         self.single_char_names.pop();
331     }
332     fn visit_arm(&mut self, arm: &'tcx Arm) {
333         self.single_char_names.push(vec![]);
334
335         self.apply(|this| {
336             SimilarNamesNameVisitor(this).visit_pat(&arm.pat);
337             this.apply(|this| walk_expr(this, &arm.body));
338         });
339
340         self.check_single_char_names();
341         self.single_char_names.pop();
342     }
343     fn visit_item(&mut self, _: &Item) {
344         // do not recurse into inner items
345     }
346     fn visit_mac(&mut self, _mac: &Mac) {
347         // do not check macs
348     }
349 }
350
351 impl EarlyLintPass for NonExpressiveNames {
352     fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
353         if let ItemKind::Fn(ref decl, _, _, ref blk) = item.node {
354             do_check(self, cx, &item.attrs, decl, blk);
355         }
356     }
357
358     fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &ImplItem) {
359         if let ImplItemKind::Method(ref sig, ref blk) = item.node {
360             do_check(self, cx, &item.attrs, &sig.decl, blk);
361         }
362     }
363 }
364
365 fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attribute], decl: &FnDecl, blk: &Block) {
366     if !attr::contains_name(attrs, sym!(test)) {
367         let mut visitor = SimilarNamesLocalVisitor {
368             names: Vec::new(),
369             cx,
370             lint,
371             single_char_names: vec![vec![]],
372         };
373
374         // initialize with function arguments
375         for arg in &decl.inputs {
376             SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat);
377         }
378         // walk all other bindings
379         walk_block(&mut visitor, blk);
380
381         visitor.check_single_char_names();
382     }
383 }
384
385 /// Precondition: `a_name.chars().count() < b_name.chars().count()`.
386 fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
387     debug_assert!(a_name.chars().count() < b_name.chars().count());
388     let mut a_chars = a_name.chars();
389     let mut b_chars = b_name.chars();
390     while let (Some(a), Some(b)) = (a_chars.next(), b_chars.next()) {
391         if a == b {
392             continue;
393         }
394         if let Some(b2) = b_chars.next() {
395             // check if there's just one character inserted
396             return a != b2 || a_chars.ne(b_chars);
397         } else {
398             // tuple
399             // ntuple
400             return true;
401         }
402     }
403     // for item in items
404     true
405 }