Skip to content

Commit d27feba

Browse files
committed
Use Sugg interface for suggestion
1 parent 78fb663 commit d27feba

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

clippy_lints/src/default_box_assignments.rs

Lines changed: 7 additions & 3 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_default_equivalent_call;
3-
use clippy_utils::source::snippet;
3+
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::implements_trait;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem};
@@ -45,13 +45,17 @@ impl LateLintPass<'_> for DefaultBoxAssignments {
4545
expr.span,
4646
"assigning `Default::default()` to `Box<T>`",
4747
|diag| {
48-
let suggestion = format!("*({}) = Default::default()", snippet(cx, lhs.span, "_"));
48+
let mut app = Applicability::MachineApplicable;
49+
let suggestion = format!(
50+
"{} = Default::default()",
51+
Sugg::hir_with_applicability(cx, lhs, "_", &mut app).deref()
52+
);
4953

5054
diag.note("this creates a needless allocation").span_suggestion(
5155
expr.span,
5256
"assign to the inner value",
5357
suggestion,
54-
Applicability::MaybeIncorrect,
58+
app,
5559
);
5660
},
5761
);

tests/ui/default_box_assignments.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ macro_rules! create_default {
88

99
fn main() {
1010
let mut b = Box::new(1u32);
11-
*(b) = Default::default();
11+
*b = Default::default();
1212
//~^ default_box_assignments
13-
*(b) = Default::default();
13+
*b = Default::default();
1414
//~^ default_box_assignments
1515

1616
// No lint for assigning to the storage

tests/ui/default_box_assignments.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: assigning `Default::default()` to `Box<T>`
22
--> tests/ui/default_box_assignments.rs:11:5
33
|
44
LL | b = Default::default();
5-
| ^^^^^^^^^^^^^^^^^^^^^^ help: assign to the inner value: `*(b) = Default::default()`
5+
| ^^^^^^^^^^^^^^^^^^^^^^ help: assign to the inner value: `*b = Default::default()`
66
|
77
= note: this creates a needless allocation
88
= note: `-D clippy::default-box-assignments` implied by `-D warnings`
@@ -12,7 +12,7 @@ error: assigning `Default::default()` to `Box<T>`
1212
--> tests/ui/default_box_assignments.rs:13:5
1313
|
1414
LL | b = Box::default();
15-
| ^^^^^^^^^^^^^^^^^^ help: assign to the inner value: `*(b) = Default::default()`
15+
| ^^^^^^^^^^^^^^^^^^ help: assign to the inner value: `*b = Default::default()`
1616
|
1717
= note: this creates a needless allocation
1818

0 commit comments

Comments
 (0)