Skip to content

Commit f966778

Browse files
committed
Merge pull request #914 from oli-obk/non_expressive_names
similar_names fixes
2 parents da122a3 + 0bef7b5 commit f966778

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/non_expressive_names.rs

Lines changed: 16 additions & 11 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) {
@@ -254,10 +259,10 @@ impl EarlyLintPass for NonExpressiveNames {
254259
};
255260
// initialize with function arguments
256261
for arg in &decl.inputs {
257-
visit::walk_pat(&mut SimilarNamesNameVisitor(&mut visitor), &arg.pat);
262+
SimilarNamesNameVisitor(&mut visitor).visit_pat(&arg.pat);
258263
}
259264
// walk all other bindings
260-
visit::walk_block(&mut visitor, blk);
265+
walk_block(&mut visitor, blk);
261266
}
262267
}
263268
}

src/shadow.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn check_pat(cx: &LateContext, pat: &Pat, init: &Option<&Expr>, span: Span, bind
195195
}
196196
}
197197

198-
fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init: &Option<T>, prev_span: Span)
198+
fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, pattern_span: Span, init: &Option<T>, prev_span: Span)
199199
where T: Deref<Target = Expr>
200200
{
201201
fn note_orig(cx: &LateContext, mut db: DiagnosticWrapper, lint: &'static Lint, span: Span) {
@@ -209,25 +209,25 @@ fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init: &
209209
SHADOW_SAME,
210210
span,
211211
&format!("{} is shadowed by itself in {}",
212-
snippet(cx, lspan, "_"),
212+
snippet(cx, pattern_span, "_"),
213213
snippet(cx, expr.span, "..")));
214214
note_orig(cx, db, SHADOW_SAME, prev_span);
215215
} else if contains_self(name, expr) {
216216
let db = span_note_and_lint(cx,
217217
SHADOW_REUSE,
218-
lspan,
218+
pattern_span,
219219
&format!("{} is shadowed by {} which reuses the original value",
220-
snippet(cx, lspan, "_"),
220+
snippet(cx, pattern_span, "_"),
221221
snippet(cx, expr.span, "..")),
222222
expr.span,
223223
"initialization happens here");
224224
note_orig(cx, db, SHADOW_REUSE, prev_span);
225225
} else {
226226
let db = span_note_and_lint(cx,
227227
SHADOW_UNRELATED,
228-
lspan,
228+
pattern_span,
229229
&format!("{} is shadowed by {}",
230-
snippet(cx, lspan, "_"),
230+
snippet(cx, pattern_span, "_"),
231231
snippet(cx, expr.span, "..")),
232232
expr.span,
233233
"initialization happens here");
@@ -238,7 +238,7 @@ fn lint_shadow<T>(cx: &LateContext, name: Name, span: Span, lspan: Span, init: &
238238
let db = span_lint(cx,
239239
SHADOW_UNRELATED,
240240
span,
241-
&format!("{} shadows a previous declaration", snippet(cx, lspan, "_")));
241+
&format!("{} shadows a previous declaration", snippet(cx, pattern_span, "_")));
242242
note_orig(cx, db, SHADOW_UNRELATED, prev_span);
243243
}
244244
}

tests/compile-fail/non_expressive_names.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
//~| NOTE: lint level defined here
1212
//~| NOTE: lint level defined here
1313
//~| NOTE: lint level defined here
14+
//~| NOTE: lint level defined here
1415
#![allow(unused)]
1516

17+
18+
struct Foo {
19+
apple: i32,
20+
bpple: i32,
21+
}
22+
1623
fn main() {
1724
let specter: i32;
1825
let spectre: i32;
@@ -90,6 +97,13 @@ fn main() {
9097
let rx_cake: i32;
9198
}
9299

100+
fn foo() {
101+
let Foo { apple, bpple } = unimplemented!();
102+
let Foo { apple: spring, //~NOTE existing binding defined here
103+
bpple: sprang } = unimplemented!(); //~ ERROR: name is too similar
104+
//~^HELP for further information
105+
}
106+
93107
#[derive(Clone, Debug)]
94108
enum MaybeInst {
95109
Split,

0 commit comments

Comments
 (0)