Skip to content

Commit 253d6eb

Browse files
committed
Remove brackets around type name when it is no longer a prefix
When `<T>::default()` is replaced by `T` when `T` is a singleton, the brackets around the type name can be removed.
1 parent 253ecb9 commit 253d6eb

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_ty_alias;
3-
use clippy_utils::sugg::DiagExt as _;
3+
use clippy_utils::source::SpanRangeExt as _;
44
use hir::ExprKind;
55
use hir::def::Res;
66
use 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| dbg!(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
},

tests/ui/default_constructed_unit_structs.fixed

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ struct UnitStruct;
88
impl 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
}

tests/ui/default_constructed_unit_structs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "Self::default()"
12
error: use of `default` to create a unit struct
23
--> tests/ui/default_constructed_unit_structs.rs:11:9
34
|
4-
LL | Self::default()
5-
| _________^^^^-^^^^^^^^^^
6-
LL | |
7-
| |________- help: remove this call to `default`
5+
LL | Self::default()
6+
| ^^^^-----------
7+
| |
8+
| help: remove this call to `default`
89
|
910
= note: `-D clippy::default-constructed-unit-structs` implied by `-D warnings`
1011
= help: to override `-D warnings` add `#[allow(clippy::default_constructed_unit_structs)]`
1112

13+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "PhantomData::default()"
1214
error: use of `default` to create a unit struct
1315
--> tests/ui/default_constructed_unit_structs.rs:54:20
1416
|
@@ -17,6 +19,7 @@ LL | inner: PhantomData::default(),
1719
| |
1820
| help: remove this call to `default`
1921

22+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "PhantomData::<usize>::default()"
2023
error: use of `default` to create a unit struct
2124
--> tests/ui/default_constructed_unit_structs.rs:128:13
2225
|
@@ -25,6 +28,7 @@ LL | let _ = PhantomData::<usize>::default();
2528
| |
2629
| help: remove this call to `default`
2730

31+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "PhantomData::default()"
2832
error: use of `default` to create a unit struct
2933
--> tests/ui/default_constructed_unit_structs.rs:130:31
3034
|
@@ -33,6 +37,7 @@ LL | let _: PhantomData<i32> = PhantomData::default();
3337
| |
3438
| help: remove this call to `default`
3539

40+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "std::marker::PhantomData::default()"
3641
error: use of `default` to create a unit struct
3742
--> tests/ui/default_constructed_unit_structs.rs:132:31
3843
|
@@ -41,6 +46,7 @@ LL | let _: PhantomData<i32> = std::marker::PhantomData::default();
4146
| |
4247
| help: remove this call to `default`
4348

49+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "UnitStruct::default()"
4450
error: use of `default` to create a unit struct
4551
--> tests/ui/default_constructed_unit_structs.rs:134:13
4652
|
@@ -49,5 +55,18 @@ LL | let _ = UnitStruct::default();
4955
| |
5056
| help: remove this call to `default`
5157

52-
error: aborting due to 6 previous errors
58+
[clippy_lints/src/default_constructed_unit_structs.rs:78:51] s = "<G>::default()"
59+
error: use of `default` to create a unit struct
60+
--> tests/ui/default_constructed_unit_structs.rs:172:7
61+
|
62+
LL | f(<G>::default());
63+
| ^^^^^^^^^^^^^^
64+
|
65+
help: remove this call to `default`
66+
|
67+
LL - f(<G>::default());
68+
LL + f(G);
69+
|
70+
71+
error: aborting due to 7 previous errors
5372

0 commit comments

Comments
 (0)