Skip to content

Commit 1c81105

Browse files
committed
Auto merge of #13211 - rzvxa:respect_allow_inconsistent_struct_constructor_on_adt, r=Alexendoo
Respect allow `inconsistent_struct_constructor` on the struct definition Closes #13203 Now we check if the target type is marked with `#[allow(clippy:inconsistent_struct_constructor)]` before lining. As a side-effect of this change, The rule in the subject no longer runs on non-local `AdtDef`s. However, as suggested by `@Jarcho` it shouldn't be a big deal since most of the time we didn't have access to this information anyway. > You can't get lint attributes from other crates. I would probably just restrict the lint to only work with types from the current crate while you're at it. Upstream crates don't have a definition order from the point of view of the current crate (with the exception of #[repr(C)] structs). changelog: Respect allow `inconsistent_struct_constructor` on the struct definition.
2 parents cb80611 + d85cf0b commit 1c81105

4 files changed

+58
-11
lines changed

clippy_lints/src/inconsistent_struct_constructor.rs

+14-9
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::fulfill_or_allowed;
23
use clippy_utils::source::snippet;
34
use rustc_data_structures::fx::FxHashMap;
45
use rustc_errors::Applicability;
@@ -71,6 +72,8 @@ 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)
7477
&& let Some(variant) = adt_def.variants().iter().next()
7578
{
7679
let mut def_order_map = FxHashMap::default();
@@ -103,15 +106,17 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
103106
snippet(cx, qpath.span(), ".."),
104107
);
105108

106-
span_lint_and_sugg(
107-
cx,
108-
INCONSISTENT_STRUCT_CONSTRUCTOR,
109-
expr.span,
110-
"struct constructor field order is inconsistent with struct definition field order",
111-
"try",
112-
sugg,
113-
Applicability::MachineApplicable,
114-
);
109+
if !fulfill_or_allowed(cx, INCONSISTENT_STRUCT_CONSTRUCTOR, Some(ty_hir_id)) {
110+
span_lint_and_sugg(
111+
cx,
112+
INCONSISTENT_STRUCT_CONSTRUCTOR,
113+
expr.span,
114+
"struct constructor field order is inconsistent with struct definition field order",
115+
"try",
116+
sugg,
117+
Applicability::MachineApplicable,
118+
);
119+
}
115120
}
116121
}
117122
}

tests/ui/inconsistent_struct_constructor.fixed

+21
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

+21
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

+2-2
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)