Skip to content

Commit ba80e06

Browse files
committed
Auto merge of rust-lang#12429 - Alexendoo:redundant-field-names-macro-ctxt, r=Manishearth
Don't lint `redundant_field_names` across macro boundaries Fixes rust-lang#12426 The `field.span.eq_ctxt(field.ident.span)` addition is the relevant line for the bugfix The current implementation checks that the field's name and the path are in the same context by comparing the idents, but not that the two are in the same context as the entire field itself, so in local macros `SomeStruct { $ident: $ident }` would get linted changelog: none
2 parents a79db2a + ac643a2 commit ba80e06

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

clippy_lints/src/redundant_field_names.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,22 @@ impl EarlyLintPass for RedundantFieldNames {
5959
}
6060
if let ExprKind::Struct(ref se) = expr.kind {
6161
for field in &se.fields {
62-
if field.is_shorthand {
63-
continue;
64-
}
65-
if let ExprKind::Path(None, path) = &field.expr.kind {
66-
if path.segments.len() == 1
67-
&& path.segments[0].ident == field.ident
68-
&& path.segments[0].args.is_none()
69-
{
70-
span_lint_and_sugg(
71-
cx,
72-
REDUNDANT_FIELD_NAMES,
73-
field.span,
74-
"redundant field names in struct initialization",
75-
"replace it with",
76-
field.ident.to_string(),
77-
Applicability::MachineApplicable,
78-
);
79-
}
62+
if !field.is_shorthand
63+
&& let ExprKind::Path(None, path) = &field.expr.kind
64+
&& let [segment] = path.segments.as_slice()
65+
&& segment.args.is_none()
66+
&& segment.ident == field.ident
67+
&& field.span.eq_ctxt(field.ident.span)
68+
{
69+
span_lint_and_sugg(
70+
cx,
71+
REDUNDANT_FIELD_NAMES,
72+
field.span,
73+
"redundant field names in struct initialization",
74+
"replace it with",
75+
field.ident.to_string(),
76+
Applicability::MachineApplicable,
77+
);
8078
}
8179
}
8280
}

tests/ui/redundant_field_names.fixed

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Person {
2020
}
2121

2222
pub struct S {
23-
v: String,
23+
v: usize,
2424
}
2525

2626
fn main() {
@@ -59,11 +59,22 @@ fn main() {
5959
let _ = RangeToInclusive { end };
6060

6161
external! {
62-
let v = String::new();
62+
let v = 1;
6363
let _ = S {
6464
v: v
6565
};
6666
}
67+
68+
let v = 2;
69+
macro_rules! internal {
70+
($i:ident) => {
71+
let _ = S { v };
72+
let _ = S { $i: v };
73+
let _ = S { v: $i };
74+
let _ = S { $i: $i };
75+
};
76+
}
77+
internal!(v);
6778
}
6879

6980
fn issue_3476() {

tests/ui/redundant_field_names.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Person {
2020
}
2121

2222
pub struct S {
23-
v: String,
23+
v: usize,
2424
}
2525

2626
fn main() {
@@ -59,11 +59,22 @@ fn main() {
5959
let _ = RangeToInclusive { end: end };
6060

6161
external! {
62-
let v = String::new();
62+
let v = 1;
6363
let _ = S {
6464
v: v
6565
};
6666
}
67+
68+
let v = 2;
69+
macro_rules! internal {
70+
($i:ident) => {
71+
let _ = S { v: v };
72+
let _ = S { $i: v };
73+
let _ = S { v: $i };
74+
let _ = S { $i: $i };
75+
};
76+
}
77+
internal!(v);
6778
}
6879

6980
fn issue_3476() {

tests/ui/redundant_field_names.stderr

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,21 @@ LL | let _ = RangeToInclusive { end: end };
4444
| ^^^^^^^^ help: replace it with: `end`
4545

4646
error: redundant field names in struct initialization
47-
--> tests/ui/redundant_field_names.rs:88:25
47+
--> tests/ui/redundant_field_names.rs:71:25
48+
|
49+
LL | let _ = S { v: v };
50+
| ^^^^ help: replace it with: `v`
51+
...
52+
LL | internal!(v);
53+
| ------------ in this macro invocation
54+
|
55+
= note: this error originates in the macro `internal` (in Nightly builds, run with -Z macro-backtrace for more info)
56+
57+
error: redundant field names in struct initialization
58+
--> tests/ui/redundant_field_names.rs:99:25
4859
|
4960
LL | let _ = RangeFrom { start: start };
5061
| ^^^^^^^^^^^^ help: replace it with: `start`
5162

52-
error: aborting due to 8 previous errors
63+
error: aborting due to 9 previous errors
5364

0 commit comments

Comments
 (0)