1 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
3 self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind,
5 use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
6 use rustc_lint::{EarlyContext, EarlyLintPass};
7 use rustc_middle::lint::in_external_macro;
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::source_map::Span;
11 use rustc_span::symbol::{Ident, Symbol};
12 use std::cmp::Ordering;
14 declare_clippy_lint! {
16 /// Checks for names that are very similar and thus confusing.
18 /// ### Why is this bad?
19 /// It's hard to distinguish between names that differ only
20 /// by a single character.
24 /// let checked_exp = something;
25 /// let checked_expr = something_else;
27 #[clippy::version = "pre 1.29.0"]
30 "similarly named items and bindings"
33 declare_clippy_lint! {
35 /// Checks for too many variables whose name consists of a
38 /// ### Why is this bad?
39 /// It's hard to memorize what a variable means without a
44 /// let (a, b, c, d, e, f, g) = (...);
46 #[clippy::version = "pre 1.29.0"]
47 pub MANY_SINGLE_CHAR_NAMES,
49 "too many single character bindings"
52 declare_clippy_lint! {
54 /// Checks if you have variables whose name consists of just
55 /// underscores and digits.
57 /// ### Why is this bad?
58 /// It's hard to memorize what a variable means without a
67 #[clippy::version = "pre 1.29.0"]
68 pub JUST_UNDERSCORES_AND_DIGITS,
73 #[derive(Copy, Clone)]
74 pub struct NonExpressiveNames {
75 pub single_char_binding_names_threshold: u64,
78 impl_lint_pass!(NonExpressiveNames => [SIMILAR_NAMES, MANY_SINGLE_CHAR_NAMES, JUST_UNDERSCORES_AND_DIGITS]);
84 exemptions: &'static [&'static str],
87 struct SimilarNamesLocalVisitor<'a, 'tcx> {
88 names: Vec<ExistingName>,
89 cx: &'a EarlyContext<'tcx>,
90 lint: &'a NonExpressiveNames,
92 /// A stack of scopes containing the single-character bindings in each scope.
93 single_char_names: Vec<Vec<Ident>>,
96 impl<'a, 'tcx> SimilarNamesLocalVisitor<'a, 'tcx> {
97 fn check_single_char_names(&self) {
98 let num_single_char_names = self.single_char_names.iter().flatten().count();
99 let threshold = self.lint.single_char_binding_names_threshold;
100 if num_single_char_names as u64 > threshold {
105 .map(|ident| ident.span)
106 .collect::<Vec<_>>();
109 MANY_SINGLE_CHAR_NAMES,
112 "{} bindings with single-character names in scope",
113 num_single_char_names
120 // this list contains lists of names that are allowed to be similar
121 // the assumption is that no name is ever contained in multiple lists.
123 const ALLOWED_TO_BE_SIMILAR: &[&[&str]] = &[
124 &["parsed", "parser"],
131 &["wparam", "lparam"],
135 struct SimilarNamesNameVisitor<'a, 'tcx, 'b>(&'b mut SimilarNamesLocalVisitor<'a, 'tcx>);
137 impl<'a, 'tcx, 'b> Visitor<'tcx> for SimilarNamesNameVisitor<'a, 'tcx, 'b> {
138 fn visit_pat(&mut self, pat: &'tcx Pat) {
140 PatKind::Ident(_, ident, _) => {
141 if !pat.span.from_expansion() {
142 self.check_ident(ident);
145 PatKind::Struct(_, _, ref fields, _) => {
146 for field in fields {
147 if !field.is_shorthand {
148 self.visit_pat(&field.pat);
152 // just go through the first pattern, as either all patterns
153 // bind the same bindings or rustc would have errored much earlier
154 PatKind::Or(ref pats) => self.visit_pat(&pats[0]),
155 _ => walk_pat(self, pat),
161 fn get_exemptions(interned_name: &str) -> Option<&'static [&'static str]> {
162 for &list in ALLOWED_TO_BE_SIMILAR {
163 if allowed_to_be_similar(interned_name, list) {
171 fn allowed_to_be_similar(interned_name: &str, list: &[&str]) -> bool {
173 .any(|&name| interned_name.starts_with(name) || interned_name.ends_with(name))
176 impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
177 fn check_short_ident(&mut self, ident: Ident) {
184 .any(|id| id.name == ident.name)
189 if let Some(scope) = &mut self.0.single_char_names.last_mut() {
194 #[allow(clippy::too_many_lines)]
195 fn check_ident(&mut self, ident: Ident) {
196 let interned_name = ident.name.as_str();
197 if interned_name.chars().any(char::is_uppercase) {
200 if interned_name.chars().all(|c| c.is_digit(10) || c == '_') {
203 JUST_UNDERSCORES_AND_DIGITS,
205 "consider choosing a more descriptive name",
209 if interned_name.starts_with('_') {
210 // these bindings are typically unused or represent an ignored portion of a destructuring pattern
213 let count = interned_name.chars().count();
216 self.check_short_ident(ident);
220 for existing_name in &self.0.names {
221 if allowed_to_be_similar(interned_name, existing_name.exemptions) {
224 match existing_name.len.cmp(&count) {
225 Ordering::Greater => {
226 if existing_name.len - count != 1
227 || levenstein_not_1(interned_name, existing_name.interned.as_str())
233 if count - existing_name.len != 1
234 || levenstein_not_1(existing_name.interned.as_str(), interned_name)
240 let mut interned_chars = interned_name.chars();
241 let interned_str = existing_name.interned.as_str();
242 let mut existing_chars = interned_str.chars();
243 let first_i = interned_chars.next().expect("we know we have at least one char");
244 let first_e = existing_chars.next().expect("we know we have at least one char");
245 let eq_or_numeric = |(a, b): (char, char)| a == b || a.is_numeric() && b.is_numeric();
247 if eq_or_numeric((first_i, first_e)) {
248 let last_i = interned_chars.next_back().expect("we know we have at least two chars");
249 let last_e = existing_chars.next_back().expect("we know we have at least two chars");
250 if eq_or_numeric((last_i, last_e)) {
253 .filter(|&ie| !eq_or_numeric(ie))
260 let second_last_i = interned_chars
262 .expect("we know we have at least three chars");
263 let second_last_e = existing_chars
265 .expect("we know we have at least three chars");
266 if !eq_or_numeric((second_last_i, second_last_e))
267 || second_last_i == '_'
268 || !interned_chars.zip(existing_chars).all(eq_or_numeric)
270 // allowed similarity foo_x, foo_y
271 // or too many chars differ (foo_x, boo_y) or (foox, booy)
276 let second_i = interned_chars.next().expect("we know we have at least two chars");
277 let second_e = existing_chars.next().expect("we know we have at least two chars");
278 if !eq_or_numeric((second_i, second_e))
280 || !interned_chars.zip(existing_chars).all(eq_or_numeric)
282 // allowed similarity x_foo, y_foo
283 // or too many chars differ (x_foo, y_boo) or (xfoo, yboo)
293 "binding's name is too similar to existing binding",
295 diag.span_note(existing_name.span, "existing binding defined here");
300 self.0.names.push(ExistingName {
301 exemptions: get_exemptions(interned_name).unwrap_or(&[]),
302 interned: ident.name,
309 impl<'a, 'b> SimilarNamesLocalVisitor<'a, 'b> {
310 /// ensure scoping rules work
311 fn apply<F: for<'c> Fn(&'c mut Self)>(&mut self, f: F) {
312 let n = self.names.len();
313 let single_char_count = self.single_char_names.len();
315 self.names.truncate(n);
316 self.single_char_names.truncate(single_char_count);
320 impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> {
321 fn visit_local(&mut self, local: &'tcx Local) {
322 if let Some((init, els)) = &local.kind.init_else_opt() {
323 self.apply(|this| walk_expr(this, init));
324 if let Some(els) = els {
325 self.apply(|this| walk_block(this, els));
328 // add the pattern after the expression because the bindings aren't available
331 SimilarNamesNameVisitor(self).visit_pat(&*local.pat);
333 fn visit_block(&mut self, blk: &'tcx Block) {
334 self.single_char_names.push(vec![]);
336 self.apply(|this| walk_block(this, blk));
338 self.check_single_char_names();
339 self.single_char_names.pop();
341 fn visit_arm(&mut self, arm: &'tcx Arm) {
342 self.single_char_names.push(vec![]);
345 SimilarNamesNameVisitor(this).visit_pat(&arm.pat);
346 this.apply(|this| walk_expr(this, &arm.body));
349 self.check_single_char_names();
350 self.single_char_names.pop();
352 fn visit_item(&mut self, _: &Item) {
353 // do not recurse into inner items
357 impl EarlyLintPass for NonExpressiveNames {
358 fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
359 if in_external_macro(cx.sess, item.span) {
363 if let ItemKind::Fn(box ast::Fn {
369 do_check(self, cx, &item.attrs, &sig.decl, blk);
373 fn check_impl_item(&mut self, cx: &EarlyContext<'_>, item: &AssocItem) {
374 if in_external_macro(cx.sess, item.span) {
378 if let AssocItemKind::Fn(box ast::Fn {
384 do_check(self, cx, &item.attrs, &sig.decl, blk);
389 fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attribute], decl: &FnDecl, blk: &Block) {
390 if !attrs.iter().any(|attr| attr.has_name(sym::test)) {
391 let mut visitor = SimilarNamesLocalVisitor {
395 single_char_names: vec![vec![]],
398 // initialize with function arguments
399 for arg in &decl.inputs {
400 SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat);
402 // walk all other bindings
403 walk_block(&mut visitor, blk);
405 visitor.check_single_char_names();
409 /// Precondition: `a_name.chars().count() < b_name.chars().count()`.
411 fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
412 debug_assert!(a_name.chars().count() < b_name.chars().count());
413 let mut a_chars = a_name.chars();
414 let mut b_chars = b_name.chars();
415 while let (Some(a), Some(b)) = (a_chars.next(), b_chars.next()) {
419 if let Some(b2) = b_chars.next() {
420 // check if there's just one character inserted
421 return a != b2 || a_chars.ne(b_chars);