From ff83b3ecb9a9e791d1a0f389a2a3bf6a2d041fcf Mon Sep 17 00:00:00 2001 From: HMPerson1 Date: Fri, 2 Feb 2018 01:49:47 -0500 Subject: [PATCH] Fix `non_expressive_names` --- clippy_lints/src/non_expressive_names.rs | 39 +++++++++++++++--------- tests/ui/non_expressive_names.rs | 11 +++++++ tests/ui/non_expressive_names.stderr | 20 +++++++++++- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/non_expressive_names.rs b/clippy_lints/src/non_expressive_names.rs index d3b6aefe5f4..9ef55907ecd 100644 --- a/clippy_lints/src/non_expressive_names.rs +++ b/clippy_lints/src/non_expressive_names.rs @@ -313,22 +313,33 @@ impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> { impl EarlyLintPass for NonExpressiveNames { fn check_item(&mut self, cx: &EarlyContext, item: &Item) { if let ItemKind::Fn(ref decl, _, _, _, _, ref blk) = item.node { - if !attr::contains_name(&item.attrs, "test") { - let mut visitor = SimilarNamesLocalVisitor { - names: Vec::new(), - cx: cx, - lint: self, - single_char_names: Vec::new(), - }; - // initialize with function arguments - for arg in &decl.inputs { - SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat); - } - // walk all other bindings - walk_block(&mut visitor, blk); - } + do_check(self, cx, &item.attrs, decl, blk); } } + + fn check_impl_item(&mut self, cx: &EarlyContext, item: &ImplItem) { + if let ImplItemKind::Method(ref sig, ref blk) = item.node { + do_check(self, cx, &item.attrs, &sig.decl, blk); + } + } + +} + +fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext, attrs: &[Attribute], decl: &FnDecl, blk: &Block) { + if !attr::contains_name(attrs, "test") { + let mut visitor = SimilarNamesLocalVisitor { + names: Vec::new(), + cx: cx, + lint: lint, + single_char_names: Vec::new(), + }; + // initialize with function arguments + for arg in &decl.inputs { + SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat); + } + // walk all other bindings + walk_block(&mut visitor, blk); + } } /// Precondition: `a_name.chars().count() < b_name.chars().count()`. diff --git a/tests/ui/non_expressive_names.rs b/tests/ui/non_expressive_names.rs index 29a677004b8..19f0889a92c 100644 --- a/tests/ui/non_expressive_names.rs +++ b/tests/ui/non_expressive_names.rs @@ -141,3 +141,14 @@ fn underscores_and_numbers() { let __1___2 = 12; //~ERROR Consider a more descriptive name let _1_ok= 1; } + +struct Bar; + +impl Bar { + fn bar() { + let _1 = 1; + let ____1 = 1; + let __1___2 = 12; + let _1_ok= 1; + } +} diff --git a/tests/ui/non_expressive_names.stderr b/tests/ui/non_expressive_names.stderr index 850a3ccd951..4b95a1a9e70 100644 --- a/tests/ui/non_expressive_names.stderr +++ b/tests/ui/non_expressive_names.stderr @@ -149,5 +149,23 @@ error: consider choosing a more descriptive name 141 | let __1___2 = 12; //~ERROR Consider a more descriptive name | ^^^^^^^ -error: aborting due to 14 previous errors +error: consider choosing a more descriptive name + --> $DIR/non_expressive_names.rs:149:13 + | +149 | let _1 = 1; + | ^^ + +error: consider choosing a more descriptive name + --> $DIR/non_expressive_names.rs:150:13 + | +150 | let ____1 = 1; + | ^^^^^ + +error: consider choosing a more descriptive name + --> $DIR/non_expressive_names.rs:151:13 + | +151 | let __1___2 = 12; + | ^^^^^^^ + +error: aborting due to 17 previous errors