Skip to content

Commit 78fb663

Browse files
committed
Add test for macro expansion
1 parent 6a3618c commit 78fb663

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

clippy_lints/src/default_box_assignments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl LateLintPass<'_> for DefaultBoxAssignments {
3838
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
3939
if let ExprKind::Assign(lhs, rhs, _) = &expr.kind {
4040
let lhs_ty = cx.typeck_results().expr_ty(lhs);
41-
if is_box_of_default(lhs_ty, cx) && is_default_call(rhs, cx) {
41+
if is_box_of_default(lhs_ty, cx) && is_default_call(rhs, cx) && !rhs.span.from_expansion() {
4242
span_lint_and_then(
4343
cx,
4444
DEFAULT_BOX_ASSIGNMENTS,

tests/ui/default_box_assignments.fixed

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#![warn(clippy::default_box_assignments)]
22

3+
macro_rules! create_default {
4+
() => {
5+
Default::default()
6+
};
7+
}
8+
39
fn main() {
410
let mut b = Box::new(1u32);
511
*(b) = Default::default();
@@ -11,7 +17,10 @@ fn main() {
1117
*b = Default::default();
1218
*b = u32::default();
1319

14-
// No lint for assigning to Box<T> where T is unsized
20+
// No lint for call originating in macro
21+
b = create_default!();
22+
23+
// No lint for assigning to Box<T> where T: !Default
1524
let mut b = Box::<str>::from("hi".to_string());
1625
b = Default::default();
1726
}

tests/ui/default_box_assignments.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#![warn(clippy::default_box_assignments)]
22

3+
macro_rules! create_default {
4+
() => {
5+
Default::default()
6+
};
7+
}
8+
39
fn main() {
410
let mut b = Box::new(1u32);
511
b = Default::default();
@@ -11,7 +17,10 @@ fn main() {
1117
*b = Default::default();
1218
*b = u32::default();
1319

14-
// No lint for assigning to Box<T> where T is unsized
20+
// No lint for call originating in macro
21+
b = create_default!();
22+
23+
// No lint for assigning to Box<T> where T: !Default
1524
let mut b = Box::<str>::from("hi".to_string());
1625
b = Default::default();
1726
}

tests/ui/default_box_assignments.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: assigning `Default::default()` to `Box<T>`
2-
--> tests/ui/default_box_assignments.rs:5:5
2+
--> tests/ui/default_box_assignments.rs:11:5
33
|
44
LL | b = Default::default();
55
| ^^^^^^^^^^^^^^^^^^^^^^ help: assign to the inner value: `*(b) = Default::default()`
@@ -9,7 +9,7 @@ LL | b = Default::default();
99
= help: to override `-D warnings` add `#[allow(clippy::default_box_assignments)]`
1010

1111
error: assigning `Default::default()` to `Box<T>`
12-
--> tests/ui/default_box_assignments.rs:7:5
12+
--> tests/ui/default_box_assignments.rs:13:5
1313
|
1414
LL | b = Box::default();
1515
| ^^^^^^^^^^^^^^^^^^ help: assign to the inner value: `*(b) = Default::default()`

0 commit comments

Comments
 (0)