Skip to content

Commit ec38ea1

Browse files
committed
Auto merge of #7097 - yawara:fix/7069, r=llogiq
Fixed inconsistent_struct_constructor triggers in macro-generated code fixes #7069 changelog: `inconsistent_struct_constructor`: Fix FP in macro expansion.
2 parents b7c12f3 + 6eae905 commit ec38ea1

4 files changed

+30
-2
lines changed

clippy_lints/src/inconsistent_struct_constructor.rs

Lines changed: 2 additions & 0 deletions
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,6 +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! {
70+
if !in_macro(expr.span);
6971
if let ExprKind::Struct(qpath, fields, base) = expr.kind;
7072
let ty = cx.typeck_results().expr_ty(expr);
7173
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)