File tree 4 files changed +55
-24
lines changed
4 files changed +55
-24
lines changed Original file line number Diff line number Diff line change @@ -59,24 +59,22 @@ impl EarlyLintPass for RedundantFieldNames {
59
59
}
60
60
if let ExprKind :: Struct ( ref se) = expr. kind {
61
61
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
+ ) ;
80
78
}
81
79
}
82
80
}
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ struct Person {
20
20
}
21
21
22
22
pub struct S {
23
- v: String ,
23
+ v: usize ,
24
24
}
25
25
26
26
fn main() {
@@ -59,11 +59,22 @@ fn main() {
59
59
let _ = RangeToInclusive { end };
60
60
61
61
external! {
62
- let v = String::new() ;
62
+ let v = 1 ;
63
63
let _ = S {
64
64
v: v
65
65
};
66
66
}
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);
67
78
}
68
79
69
80
fn issue_3476() {
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ struct Person {
20
20
}
21
21
22
22
pub struct S {
23
- v : String ,
23
+ v : usize ,
24
24
}
25
25
26
26
fn main ( ) {
@@ -59,11 +59,22 @@ fn main() {
59
59
let _ = RangeToInclusive { end : end } ;
60
60
61
61
external ! {
62
- let v = String :: new ( ) ;
62
+ let v = 1 ;
63
63
let _ = S {
64
64
v: v
65
65
} ;
66
66
}
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) ;
67
78
}
68
79
69
80
fn issue_3476 ( ) {
Original file line number Diff line number Diff line change @@ -44,10 +44,21 @@ LL | let _ = RangeToInclusive { end: end };
44
44
| ^^^^^^^^ help: replace it with: `end`
45
45
46
46
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
48
59
|
49
60
LL | let _ = RangeFrom { start: start };
50
61
| ^^^^^^^^^^^^ help: replace it with: `start`
51
62
52
- error: aborting due to 8 previous errors
63
+ error: aborting due to 9 previous errors
53
64
You can’t perform that action at this time.
0 commit comments