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