Skip to content

Commit a715d43

Browse files
committed
Filter out rhs.span.from_expansion() at the start
1 parent 231d4f0 commit a715d43

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

clippy_lints/src/default_box_assignments.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ declare_lint_pass!(DefaultBoxAssignments => [DEFAULT_BOX_ASSIGNMENTS]);
3737

3838
impl LateLintPass<'_> for DefaultBoxAssignments {
3939
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
40-
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind {
40+
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind
41+
&& !rhs.span.from_expansion()
42+
{
4143
let lhs_ty = cx.typeck_results().expr_ty(lhs);
4244

4345
// No diagnostic for late-initialized locals
@@ -54,7 +56,6 @@ impl LateLintPass<'_> for DefaultBoxAssignments {
5456
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
5557
&& implements_trait(cx, inner_ty, default_trait_id, &[])
5658
&& is_default_call(cx, rhs)
57-
&& !rhs.span.from_expansion()
5859
{
5960
span_lint_and_then(
6061
cx,

tests/ui/default_box_assignments.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ macro_rules! create_default {
2121
};
2222
}
2323

24+
macro_rules! create_zero_box {
25+
() => {
26+
Box::new(0)
27+
};
28+
}
29+
2430
fn main() {
2531
let mut b = Box::new(1u32);
2632
*b = Default::default();
@@ -34,6 +40,7 @@ fn main() {
3440

3541
// No lint for call originating in macro
3642
b = create_default!();
43+
b = create_zero_box!();
3744

3845
*b = 5;
3946
//~^ default_box_assignments

tests/ui/default_box_assignments.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ macro_rules! create_default {
2121
};
2222
}
2323

24+
macro_rules! create_zero_box {
25+
() => {
26+
Box::new(0)
27+
};
28+
}
29+
2430
fn main() {
2531
let mut b = Box::new(1u32);
2632
b = Default::default();
@@ -34,6 +40,7 @@ fn main() {
3440

3541
// No lint for call originating in macro
3642
b = create_default!();
43+
b = create_zero_box!();
3744

3845
b = Box::new(5);
3946
//~^ default_box_assignments

tests/ui/default_box_assignments.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ 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:26:5
20+
--> tests/ui/default_box_assignments.rs:32: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:28:5
28+
--> tests/ui/default_box_assignments.rs:34: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:38:5
36+
--> tests/ui/default_box_assignments.rs:45:5
3737
|
3838
LL | b = Box::new(5);
3939
| ^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*b = 5`

0 commit comments

Comments
 (0)