Skip to content

Commit 49e2570

Browse files
committed
don't lint at the use-site of bad struct field bindings if they're shorthand
fixes #899
1 parent 855b292 commit 49e2570

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/non_expressive_names.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use syntax::codemap::Span;
33
use syntax::parse::token::InternedString;
44
use syntax::ast::*;
55
use syntax::attr;
6-
use syntax::visit;
6+
use syntax::visit::{Visitor, walk_block, walk_pat, walk_expr};
77
use utils::{span_lint_and_then, in_macro, span_lint};
88

99
/// **What it does:** This lint warns about names that are very similar and thus confusing
@@ -68,12 +68,17 @@ const WHITELIST: &'static [&'static [&'static str]] = &[
6868

6969
struct SimilarNamesNameVisitor<'a, 'b: 'a, 'c: 'b>(&'a mut SimilarNamesLocalVisitor<'b, 'c>);
7070

71-
impl<'v, 'a, 'b, 'c> visit::Visitor<'v> for SimilarNamesNameVisitor<'a, 'b, 'c> {
71+
impl<'v, 'a, 'b, 'c> Visitor<'v> for SimilarNamesNameVisitor<'a, 'b, 'c> {
7272
fn visit_pat(&mut self, pat: &'v Pat) {
73-
if let PatKind::Ident(_, id, _) = pat.node {
74-
self.check_name(id.span, id.node.name);
73+
match pat.node {
74+
PatKind::Ident(_, id, _) => self.check_name(id.span, id.node.name),
75+
PatKind::Struct(_, ref fields, _) => for field in fields {
76+
if !field.node.is_shorthand {
77+
self.visit_pat(&field.node.pat);
78+
}
79+
},
80+
_ => walk_pat(self, pat),
7581
}
76-
visit::walk_pat(self, pat);
7782
}
7883
}
7984

@@ -219,22 +224,22 @@ impl<'a, 'b> SimilarNamesLocalVisitor<'a, 'b> {
219224
}
220225
}
221226

222-
impl<'v, 'a, 'b> visit::Visitor<'v> for SimilarNamesLocalVisitor<'a, 'b> {
227+
impl<'v, 'a, 'b> Visitor<'v> for SimilarNamesLocalVisitor<'a, 'b> {
223228
fn visit_local(&mut self, local: &'v Local) {
224229
if let Some(ref init) = local.init {
225-
self.apply(|this| visit::walk_expr(this, &**init));
230+
self.apply(|this| walk_expr(this, &**init));
226231
}
227232
// add the pattern after the expression because the bindings aren't available yet in the init expression
228233
SimilarNamesNameVisitor(self).visit_pat(&*local.pat);
229234
}
230235
fn visit_block(&mut self, blk: &'v Block) {
231-
self.apply(|this| visit::walk_block(this, blk));
236+
self.apply(|this| walk_block(this, blk));
232237
}
233238
fn visit_arm(&mut self, arm: &'v Arm) {
234239
self.apply(|this| {
235240
// just go through the first pattern, as either all patterns bind the same bindings or rustc would have errored much earlier
236241
SimilarNamesNameVisitor(this).visit_pat(&arm.pats[0]);
237-
this.apply(|this| visit::walk_expr(this, &arm.body));
242+
this.apply(|this| walk_expr(this, &arm.body));
238243
});
239244
}
240245
fn visit_item(&mut self, _: &'v Item) {
@@ -257,7 +262,7 @@ impl EarlyLintPass for NonExpressiveNames {
257262
visit::walk_pat(&mut SimilarNamesNameVisitor(&mut visitor), &arg.pat);
258263
}
259264
// walk all other bindings
260-
visit::walk_block(&mut visitor, blk);
265+
walk_block(&mut visitor, blk);
261266
}
262267
}
263268
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
#![deny(clippy,similar_names)]
4+
#![allow(unused)]
5+
6+
struct Foo {
7+
apple: i32,
8+
bpple: i32,
9+
}
10+
11+
fn main() {
12+
let Foo { apple, bpple } = unimplemented!();
13+
let Foo { apple: spring, bpple: sprang } = unimplemented!(); //~ ERROR: name is too similar
14+
}

0 commit comments

Comments
 (0)