Skip to content

Commit 253ecb9

Browse files
committed
Signal the problem on the whole expression span
This is more consistent with what other lints are doing: the use of `default` to create a unit struct is the whole expression.
1 parent 429c09e commit 253ecb9

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

Diff for: clippy_lints/src/default_constructed_unit_structs.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_ty_alias;
3+
use clippy_utils::sugg::DiagExt as _;
34
use hir::ExprKind;
45
use hir::def::Res;
56
use rustc_errors::Applicability;
@@ -73,14 +74,19 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
7374
// do not suggest replacing an expression by a type name with placeholders
7475
&& !base.is_suggestable_infer_ty()
7576
{
76-
span_lint_and_sugg(
77+
span_lint_and_then(
7778
cx,
7879
DEFAULT_CONSTRUCTED_UNIT_STRUCTS,
79-
expr.span.with_lo(qpath.qself_span().hi()),
80+
expr.span,
8081
"use of `default` to create a unit struct",
81-
"remove this call to `default`",
82-
String::new(),
83-
Applicability::MachineApplicable,
82+
|diag| {
83+
diag.suggest_remove_item(
84+
cx,
85+
expr.span.with_lo(qpath.qself_span().hi()),
86+
"remove this call to `default`",
87+
Applicability::MachineApplicable,
88+
);
89+
},
8490
);
8591
}
8692
}

Diff for: tests/ui/default_constructed_unit_structs.fixed

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ struct UnitStruct;
88
impl UnitStruct {
99
fn new() -> Self {
1010
//should lint
11-
Self
12-
//~^ default_constructed_unit_structs
11+
Self//~^ default_constructed_unit_structs
1312
}
1413
}
1514

Diff for: tests/ui/default_constructed_unit_structs.stderr

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
error: use of `default` to create a unit struct
2-
--> tests/ui/default_constructed_unit_structs.rs:11:13
2+
--> tests/ui/default_constructed_unit_structs.rs:11:9
33
|
4-
LL | Self::default()
5-
| ^^^^^^^^^^^ help: remove this call to `default`
4+
LL | Self::default()
5+
| _________^^^^-^^^^^^^^^^
6+
LL | |
7+
| |________- help: remove this call to `default`
68
|
79
= note: `-D clippy::default-constructed-unit-structs` implied by `-D warnings`
810
= help: to override `-D warnings` add `#[allow(clippy::default_constructed_unit_structs)]`
911

1012
error: use of `default` to create a unit struct
11-
--> tests/ui/default_constructed_unit_structs.rs:54:31
13+
--> tests/ui/default_constructed_unit_structs.rs:54:20
1214
|
1315
LL | inner: PhantomData::default(),
14-
| ^^^^^^^^^^^ help: remove this call to `default`
16+
| ^^^^^^^^^^^-----------
17+
| |
18+
| help: remove this call to `default`
1519

1620
error: use of `default` to create a unit struct
17-
--> tests/ui/default_constructed_unit_structs.rs:128:33
21+
--> tests/ui/default_constructed_unit_structs.rs:128:13
1822
|
1923
LL | let _ = PhantomData::<usize>::default();
20-
| ^^^^^^^^^^^ help: remove this call to `default`
24+
| ^^^^^^^^^^^^^^^^^^^^-----------
25+
| |
26+
| help: remove this call to `default`
2127

2228
error: use of `default` to create a unit struct
23-
--> tests/ui/default_constructed_unit_structs.rs:130:42
29+
--> tests/ui/default_constructed_unit_structs.rs:130:31
2430
|
2531
LL | let _: PhantomData<i32> = PhantomData::default();
26-
| ^^^^^^^^^^^ help: remove this call to `default`
32+
| ^^^^^^^^^^^-----------
33+
| |
34+
| help: remove this call to `default`
2735

2836
error: use of `default` to create a unit struct
29-
--> tests/ui/default_constructed_unit_structs.rs:132:55
37+
--> tests/ui/default_constructed_unit_structs.rs:132:31
3038
|
3139
LL | let _: PhantomData<i32> = std::marker::PhantomData::default();
32-
| ^^^^^^^^^^^ help: remove this call to `default`
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^-----------
41+
| |
42+
| help: remove this call to `default`
3343

3444
error: use of `default` to create a unit struct
35-
--> tests/ui/default_constructed_unit_structs.rs:134:23
45+
--> tests/ui/default_constructed_unit_structs.rs:134:13
3646
|
3747
LL | let _ = UnitStruct::default();
38-
| ^^^^^^^^^^^ help: remove this call to `default`
48+
| ^^^^^^^^^^-----------
49+
| |
50+
| help: remove this call to `default`
3951

4052
error: aborting due to 6 previous errors
4153

0 commit comments

Comments
 (0)