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 +37
-10
lines changed Expand file tree Collapse file tree 4 files changed +37
-10
lines changed Original file line number Diff line number Diff line change 11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: is_ty_alias;
3- use clippy_utils:: sugg :: DiagExt as _;
3+ use clippy_utils:: source :: SpanRangeExt as _;
44use hir:: ExprKind ;
55use hir:: def:: Res ;
66use rustc_errors:: Applicability ;
@@ -74,16 +74,20 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
7474 // do not suggest replacing an expression by a type name with placeholders
7575 && !base. is_suggestable_infer_ty ( )
7676 {
77+ let mut removals = vec ! [ ( expr. span. with_lo( qpath. qself_span( ) . hi( ) ) , String :: new( ) ) ] ;
78+ if expr. span . with_source_text ( cx, |s| s. starts_with ( '<' ) ) == Some ( true ) {
79+ // Remove `<`, '>` has already been removed by the existing removal expression.
80+ removals. push ( ( expr. span . with_hi ( qpath. qself_span ( ) . lo ( ) ) , String :: new ( ) ) ) ;
81+ }
7782 span_lint_and_then (
7883 cx,
7984 DEFAULT_CONSTRUCTED_UNIT_STRUCTS ,
8085 expr. span ,
8186 "use of `default` to create a unit struct" ,
8287 |diag| {
83- diag. suggest_remove_item (
84- cx,
85- expr. span . with_lo ( qpath. qself_span ( ) . hi ( ) ) ,
88+ diag. multipart_suggestion (
8689 "remove this call to `default`" ,
90+ removals,
8791 Applicability :: MachineApplicable ,
8892 ) ;
8993 } ,
Original file line number Diff line number Diff line change @@ -8,7 +8,8 @@ struct UnitStruct;
88impl UnitStruct {
99 fn new() -> Self {
1010 //should lint
11- Self//~^ default_constructed_unit_structs
11+ Self
12+ //~^ default_constructed_unit_structs
1213 }
1314}
1415
@@ -168,4 +169,9 @@ fn issue12654() {
168169 fn f(_g: G) {}
169170
170171 f(<_>::default());
172+ f(G);
173+ //~^ default_constructed_unit_structs
174+
175+ // No lint because `as Default` hides the singleton
176+ f(<G as Default>::default());
171177}
Original file line number Diff line number Diff line change @@ -169,4 +169,9 @@ fn issue12654() {
169169 fn f ( _g : G ) { }
170170
171171 f ( <_ >:: default ( ) ) ;
172+ f ( <G >:: default ( ) ) ;
173+ //~^ default_constructed_unit_structs
174+
175+ // No lint because `as Default` hides the singleton
176+ f ( <G as Default >:: default ( ) ) ;
172177}
Original file line number Diff line number Diff line change 11error: use of `default` to create a unit struct
22 --> tests/ui/default_constructed_unit_structs.rs:11:9
33 |
4- LL | Self::default()
5- | _________ ^^^^-^^^^^^^^^^
6- LL | |
7- | |________- help: remove this call to `default`
4+ LL | Self::default()
5+ | ^^^^-----------
6+ | |
7+ | help: remove this call to `default`
88 |
99 = note: `-D clippy::default-constructed-unit-structs` implied by `-D warnings`
1010 = help: to override `-D warnings` add `#[allow(clippy::default_constructed_unit_structs)]`
@@ -49,5 +49,17 @@ LL | let _ = UnitStruct::default();
4949 | |
5050 | help: remove this call to `default`
5151
52- error: aborting due to 6 previous errors
52+ error: use of `default` to create a unit struct
53+ --> tests/ui/default_constructed_unit_structs.rs:172:7
54+ |
55+ LL | f(<G>::default());
56+ | ^^^^^^^^^^^^^^
57+ |
58+ help: remove this call to `default`
59+ |
60+ LL - f(<G>::default());
61+ LL + f(G);
62+ |
63+
64+ error: aborting due to 7 previous errors
5365
You can’t perform that action at this time.
0 commit comments