Skip to content

Commit 6eae905

Browse files
committed
Add a test for FP in macro expansion
1 parent 61230f4 commit 6eae905

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

clippy_lints/src/inconsistent_struct_constructor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::in_macro;
23
use clippy_utils::source::snippet;
34
use if_chain::if_chain;
45
use rustc_data_structures::fx::FxHashMap;
@@ -66,7 +67,7 @@ declare_lint_pass!(InconsistentStructConstructor => [INCONSISTENT_STRUCT_CONSTRU
6667
impl LateLintPass<'_> for InconsistentStructConstructor {
6768
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
6869
if_chain! {
69-
if !expr.span.from_expansion();
70+
if !in_macro(expr.span);
7071
if let ExprKind::Struct(qpath, fields, base) = expr.kind;
7172
let ty = cx.typeck_results().expr_ty(expr);
7273
if let Some(adt_def) = ty.ty_adt_def();

tests/ui/inconsistent_struct_constructor.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ struct Foo {
1313
z: i32,
1414
}
1515

16+
macro_rules! new_foo {
17+
() => {
18+
let x = 1;
19+
let y = 1;
20+
let z = 1;
21+
Foo { y, x, z }
22+
};
23+
}
24+
1625
mod without_base {
1726
use super::Foo;
1827

@@ -24,6 +33,10 @@ mod without_base {
2433
// Should lint.
2534
Foo { x, y, z };
2635

36+
// Should NOT lint.
37+
// issue #7069.
38+
new_foo!();
39+
2740
// Shoule NOT lint because the order is the same as in the definition.
2841
Foo { x, y, z };
2942

tests/ui/inconsistent_struct_constructor.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ struct Foo {
1313
z: i32,
1414
}
1515

16+
macro_rules! new_foo {
17+
() => {
18+
let x = 1;
19+
let y = 1;
20+
let z = 1;
21+
Foo { y, x, z }
22+
};
23+
}
24+
1625
mod without_base {
1726
use super::Foo;
1827

@@ -24,6 +33,10 @@ mod without_base {
2433
// Should lint.
2534
Foo { y, x, z };
2635

36+
// Should NOT lint.
37+
// issue #7069.
38+
new_foo!();
39+
2740
// Shoule NOT lint because the order is the same as in the definition.
2841
Foo { x, y, z };
2942

tests/ui/inconsistent_struct_constructor.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: struct constructor field order is inconsistent with struct definition field order
2-
--> $DIR/inconsistent_struct_constructor.rs:25:9
2+
--> $DIR/inconsistent_struct_constructor.rs:34:9
33
|
44
LL | Foo { y, x, z };
55
| ^^^^^^^^^^^^^^^ help: try: `Foo { x, y, z }`
66
|
77
= note: `-D clippy::inconsistent-struct-constructor` implied by `-D warnings`
88

99
error: struct constructor field order is inconsistent with struct definition field order
10-
--> $DIR/inconsistent_struct_constructor.rs:43:9
10+
--> $DIR/inconsistent_struct_constructor.rs:56:9
1111
|
1212
LL | / Foo {
1313
LL | | z,

0 commit comments

Comments
 (0)