Skip to content

Commit

Permalink
Auto merge of rust-lang#7097 - yawara:fix/7069, r=llogiq
Browse files Browse the repository at this point in the history
Fixed inconsistent_struct_constructor triggers in macro-generated code

fixes rust-lang#7069

changelog: `inconsistent_struct_constructor`: Fix FP in macro expansion.
  • Loading branch information
bors committed Apr 20, 2021
2 parents b7c12f3 + 6eae905 commit ec38ea1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions clippy_lints/src/inconsistent_struct_constructor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::in_macro;
use clippy_utils::source::snippet;
use if_chain::if_chain;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -66,6 +67,7 @@ declare_lint_pass!(InconsistentStructConstructor => [INCONSISTENT_STRUCT_CONSTRU
impl LateLintPass<'_> for InconsistentStructConstructor {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if_chain! {
if !in_macro(expr.span);
if let ExprKind::Struct(qpath, fields, base) = expr.kind;
let ty = cx.typeck_results().expr_ty(expr);
if let Some(adt_def) = ty.ty_adt_def();
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/inconsistent_struct_constructor.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ struct Foo {
z: i32,
}

macro_rules! new_foo {
() => {
let x = 1;
let y = 1;
let z = 1;
Foo { y, x, z }
};
}

mod without_base {
use super::Foo;

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

// Should NOT lint.
// issue #7069.
new_foo!();

// Shoule NOT lint because the order is the same as in the definition.
Foo { x, y, z };

Expand Down
13 changes: 13 additions & 0 deletions tests/ui/inconsistent_struct_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ struct Foo {
z: i32,
}

macro_rules! new_foo {
() => {
let x = 1;
let y = 1;
let z = 1;
Foo { y, x, z }
};
}

mod without_base {
use super::Foo;

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

// Should NOT lint.
// issue #7069.
new_foo!();

// Shoule NOT lint because the order is the same as in the definition.
Foo { x, y, z };

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/inconsistent_struct_constructor.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error: struct constructor field order is inconsistent with struct definition field order
--> $DIR/inconsistent_struct_constructor.rs:25:9
--> $DIR/inconsistent_struct_constructor.rs:34:9
|
LL | Foo { y, x, z };
| ^^^^^^^^^^^^^^^ help: try: `Foo { x, y, z }`
|
= note: `-D clippy::inconsistent-struct-constructor` implied by `-D warnings`

error: struct constructor field order is inconsistent with struct definition field order
--> $DIR/inconsistent_struct_constructor.rs:43:9
--> $DIR/inconsistent_struct_constructor.rs:56:9
|
LL | / Foo {
LL | | z,
Expand Down

0 comments on commit ec38ea1

Please sign in to comment.