Skip to content

Commit 6c30186

Browse files
committed
Fix suggestion for Box::new around macro invocation
1 parent 78d93db commit 6c30186

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

clippy_lints/src/default_box_assignments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl LateLintPass<'_> for DefaultBoxAssignments {
8888
let suggestion = format!(
8989
"{} = {}",
9090
Sugg::hir_with_applicability(cx, lhs, "_", &mut app).deref(),
91-
Sugg::hir_with_applicability(cx, rhs_inner, "_", &mut app),
91+
Sugg::hir_with_context(cx, rhs_inner, expr.span.ctxt(), "_", &mut app),
9292
);
9393

9494
diag.note("this creates a needless allocation").span_suggestion(

tests/ui/default_box_assignments.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ macro_rules! same {
3333
};
3434
}
3535

36+
macro_rules! mac {
37+
(three) => {
38+
3u32
39+
};
40+
}
41+
3642
fn main() {
3743
let mut b = Box::new(1u32);
3844
*b = Default::default();
@@ -52,6 +58,9 @@ fn main() {
5258
*b = 5;
5359
//~^ default_box_assignments
5460

61+
*b = mac!(three);
62+
//~^ default_box_assignments
63+
5564
// No lint for assigning to Box<T> where T: !Default
5665
let mut b = Box::<str>::from("hi".to_string());
5766
b = Default::default();

tests/ui/default_box_assignments.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ macro_rules! same {
3333
};
3434
}
3535

36+
macro_rules! mac {
37+
(three) => {
38+
3u32
39+
};
40+
}
41+
3642
fn main() {
3743
let mut b = Box::new(1u32);
3844
b = Default::default();
@@ -52,6 +58,9 @@ fn main() {
5258
b = Box::new(5);
5359
//~^ default_box_assignments
5460

61+
b = Box::new(mac!(three));
62+
//~^ default_box_assignments
63+
5564
// No lint for assigning to Box<T> where T: !Default
5665
let mut b = Box::<str>::from("hi".to_string());
5766
b = Default::default();

tests/ui/default_box_assignments.stderr

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,36 @@ LL | *b = Box::new(t);
1717
= note: this creates a needless allocation
1818

1919
error: creating a new box with default content
20-
--> tests/ui/default_box_assignments.rs:38:5
20+
--> tests/ui/default_box_assignments.rs:44:5
2121
|
2222
LL | b = Default::default();
2323
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace existing content with default instead: `*b = Default::default()`
2424
|
2525
= note: this creates a needless allocation
2626

2727
error: creating a new box with default content
28-
--> tests/ui/default_box_assignments.rs:40:5
28+
--> tests/ui/default_box_assignments.rs:46:5
2929
|
3030
LL | b = Box::default();
3131
| ^^^^^^^^^^^^^^^^^^ help: replace existing content with default instead: `*b = Default::default()`
3232
|
3333
= note: this creates a needless allocation
3434

3535
error: creating a new box
36-
--> tests/ui/default_box_assignments.rs:52:5
36+
--> tests/ui/default_box_assignments.rs:58:5
3737
|
3838
LL | b = Box::new(5);
3939
| ^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*b = 5`
4040
|
4141
= note: this creates a needless allocation
4242

43-
error: aborting due to 5 previous errors
43+
error: creating a new box
44+
--> tests/ui/default_box_assignments.rs:61:5
45+
|
46+
LL | b = Box::new(mac!(three));
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*b = mac!(three)`
48+
|
49+
= note: this creates a needless allocation
50+
51+
error: aborting due to 6 previous errors
4452

0 commit comments

Comments
 (0)