Skip to content

Commit 78d93db

Browse files
committed
Filter out LHS from macro expansion
1 parent b7a331a commit 78d93db

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ declare_lint_pass!(DefaultBoxAssignments => [DEFAULT_BOX_ASSIGNMENTS]);
3838
impl LateLintPass<'_> for DefaultBoxAssignments {
3939
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
4040
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind
41+
&& !lhs.span.from_expansion()
4142
&& !rhs.span.from_expansion()
4243
{
4344
let lhs_ty = cx.typeck_results().expr_ty(lhs);

tests/ui/default_box_assignments.fixed

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ macro_rules! create_zero_box {
2727
};
2828
}
2929

30+
macro_rules! same {
31+
($v:ident) => {
32+
$v
33+
};
34+
}
35+
3036
fn main() {
3137
let mut b = Box::new(1u32);
3238
*b = Default::default();
@@ -38,9 +44,10 @@ fn main() {
3844
*b = Default::default();
3945
*b = u32::default();
4046

41-
// No lint for call originating in macro
47+
// No lint if either LHS or RHS originates in macro
4248
b = create_default!();
4349
b = create_zero_box!();
50+
same!(b) = Default::default();
4451

4552
*b = 5;
4653
//~^ default_box_assignments

tests/ui/default_box_assignments.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ macro_rules! create_zero_box {
2727
};
2828
}
2929

30+
macro_rules! same {
31+
($v:ident) => {
32+
$v
33+
};
34+
}
35+
3036
fn main() {
3137
let mut b = Box::new(1u32);
3238
b = Default::default();
@@ -38,9 +44,10 @@ fn main() {
3844
*b = Default::default();
3945
*b = u32::default();
4046

41-
// No lint for call originating in macro
47+
// No lint if either LHS or RHS originates in macro
4248
b = create_default!();
4349
b = create_zero_box!();
50+
same!(b) = Default::default();
4451

4552
b = Box::new(5);
4653
//~^ 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:32:5
20+
--> tests/ui/default_box_assignments.rs:38: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:34:5
28+
--> tests/ui/default_box_assignments.rs:40: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:45:5
36+
--> tests/ui/default_box_assignments.rs:52:5
3737
|
3838
LL | b = Box::new(5);
3939
| ^^^^^^^^^^^^^^^ help: replace existing content with inner value instead: `*b = 5`

0 commit comments

Comments
 (0)