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