Skip to content

Commit 5364cbe

Browse files
committed
Respect allow inconsistent_struct_constructor on the type definition
1 parent 8dd459d commit 5364cbe

4 files changed

+48
-2
lines changed

clippy_lints/src/inconsistent_struct_constructor.rs

Lines changed: 4 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::is_lint_allowed;
23
use clippy_utils::source::snippet;
34
use rustc_data_structures::fx::FxHashMap;
45
use rustc_errors::Applicability;
@@ -71,6 +72,9 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
7172
&& let ty = cx.typeck_results().expr_ty(expr)
7273
&& let Some(adt_def) = ty.ty_adt_def()
7374
&& adt_def.is_struct()
75+
&& let Some(local_def_id) = adt_def.did().as_local()
76+
&& let ty_hir_id = cx.tcx.local_def_id_to_hir_id(local_def_id)
77+
&& !is_lint_allowed(cx, INCONSISTENT_STRUCT_CONSTRUCTOR, ty_hir_id)
7478
&& let Some(variant) = adt_def.variants().iter().next()
7579
{
7680
let mut def_order_map = FxHashMap::default();

tests/ui/inconsistent_struct_constructor.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ struct Foo {
1515
z: i32,
1616
}
1717

18+
#[derive(Default)]
19+
#[allow(clippy::inconsistent_struct_constructor)]
20+
struct Bar {
21+
x: i32,
22+
y: i32,
23+
z: i32,
24+
}
25+
1826
mod without_base {
1927
use super::Foo;
2028

@@ -70,4 +78,17 @@ mod with_base {
7078
}
7179
}
7280

81+
mod with_allow_ty_def {
82+
use super::Bar;
83+
84+
fn test() {
85+
let x = 1;
86+
let y = 1;
87+
let z = 1;
88+
89+
// Should NOT lint because `Bar` is defined with `#[allow(clippy::inconsistent_struct_constructor)]`
90+
Bar { y, x, z };
91+
}
92+
}
93+
7394
fn main() {}

tests/ui/inconsistent_struct_constructor.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ struct Foo {
1515
z: i32,
1616
}
1717

18+
#[derive(Default)]
19+
#[allow(clippy::inconsistent_struct_constructor)]
20+
struct Bar {
21+
x: i32,
22+
y: i32,
23+
z: i32,
24+
}
25+
1826
mod without_base {
1927
use super::Foo;
2028

@@ -74,4 +82,17 @@ mod with_base {
7482
}
7583
}
7684

85+
mod with_allow_ty_def {
86+
use super::Bar;
87+
88+
fn test() {
89+
let x = 1;
90+
let y = 1;
91+
let z = 1;
92+
93+
// Should NOT lint because `Bar` is defined with `#[allow(clippy::inconsistent_struct_constructor)]`
94+
Bar { y, x, z };
95+
}
96+
}
97+
7798
fn main() {}

tests/ui/inconsistent_struct_constructor.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: struct constructor field order is inconsistent with struct definition field order
2-
--> tests/ui/inconsistent_struct_constructor.rs:28:9
2+
--> tests/ui/inconsistent_struct_constructor.rs:36:9
33
|
44
LL | Foo { y, x, z };
55
| ^^^^^^^^^^^^^^^ help: try: `Foo { x, y, z }`
@@ -8,7 +8,7 @@ LL | Foo { y, x, z };
88
= help: to override `-D warnings` add `#[allow(clippy::inconsistent_struct_constructor)]`
99

1010
error: struct constructor field order is inconsistent with struct definition field order
11-
--> tests/ui/inconsistent_struct_constructor.rs:55:9
11+
--> tests/ui/inconsistent_struct_constructor.rs:63:9
1212
|
1313
LL | / Foo {
1414
LL | | z,

0 commit comments

Comments
 (0)