Skip to content

Commit 0aa0d07

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 0aa0d07

4 files changed

+37
-10
lines changed

clippy_lints/src/default_constructed_unit_structs.rs

+8-4
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| 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

+7-1
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

+5
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
}

tests/ui/default_constructed_unit_structs.stderr

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error: 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

0 commit comments

Comments
 (0)