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