This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +55
-24
lines changed Expand file tree Collapse file tree 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 {
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 }
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ struct Person {
2020}
2121
2222pub struct S {
23- v: String ,
23+ v: usize ,
2424}
2525
2626fn 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
6980fn issue_3476() {
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ struct Person {
2020}
2121
2222pub struct S {
23- v : String ,
23+ v : usize ,
2424}
2525
2626fn 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
6980fn issue_3476 ( ) {
Original file line number Diff line number Diff line change @@ -44,10 +44,21 @@ LL | let _ = RangeToInclusive { end: end };
4444 | ^^^^^^^^ help: replace it with: `end`
4545
4646error: 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 |
4960LL | 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
You can’t perform that action at this time.
0 commit comments