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.
This commit is contained in:
commit
1c81105b43
@ -1,4 +1,5 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
|
use clippy_utils::fulfill_or_allowed;
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
@ -71,6 +72,8 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
|
|||||||
&& let ty = cx.typeck_results().expr_ty(expr)
|
&& let ty = cx.typeck_results().expr_ty(expr)
|
||||||
&& let Some(adt_def) = ty.ty_adt_def()
|
&& let Some(adt_def) = ty.ty_adt_def()
|
||||||
&& adt_def.is_struct()
|
&& adt_def.is_struct()
|
||||||
|
&& let Some(local_def_id) = adt_def.did().as_local()
|
||||||
|
&& let ty_hir_id = cx.tcx.local_def_id_to_hir_id(local_def_id)
|
||||||
&& let Some(variant) = adt_def.variants().iter().next()
|
&& let Some(variant) = adt_def.variants().iter().next()
|
||||||
{
|
{
|
||||||
let mut def_order_map = FxHashMap::default();
|
let mut def_order_map = FxHashMap::default();
|
||||||
@ -103,6 +106,7 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
|
|||||||
snippet(cx, qpath.span(), ".."),
|
snippet(cx, qpath.span(), ".."),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if !fulfill_or_allowed(cx, INCONSISTENT_STRUCT_CONSTRUCTOR, Some(ty_hir_id)) {
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
INCONSISTENT_STRUCT_CONSTRUCTOR,
|
INCONSISTENT_STRUCT_CONSTRUCTOR,
|
||||||
@ -115,6 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check whether the order of the fields in the constructor is consistent with the order in the
|
// Check whether the order of the fields in the constructor is consistent with the order in the
|
||||||
// definition.
|
// definition.
|
||||||
|
@ -15,6 +15,14 @@ struct Foo {
|
|||||||
z: i32,
|
z: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
#[allow(clippy::inconsistent_struct_constructor)]
|
||||||
|
struct Bar {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
z: i32,
|
||||||
|
}
|
||||||
|
|
||||||
mod without_base {
|
mod without_base {
|
||||||
use super::Foo;
|
use super::Foo;
|
||||||
|
|
||||||
@ -70,4 +78,17 @@ mod with_base {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod with_allow_ty_def {
|
||||||
|
use super::Bar;
|
||||||
|
|
||||||
|
fn test() {
|
||||||
|
let x = 1;
|
||||||
|
let y = 1;
|
||||||
|
let z = 1;
|
||||||
|
|
||||||
|
// Should NOT lint because `Bar` is defined with `#[allow(clippy::inconsistent_struct_constructor)]`
|
||||||
|
Bar { y, x, z };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -15,6 +15,14 @@ struct Foo {
|
|||||||
z: i32,
|
z: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
#[allow(clippy::inconsistent_struct_constructor)]
|
||||||
|
struct Bar {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
z: i32,
|
||||||
|
}
|
||||||
|
|
||||||
mod without_base {
|
mod without_base {
|
||||||
use super::Foo;
|
use super::Foo;
|
||||||
|
|
||||||
@ -74,4 +82,17 @@ mod with_base {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod with_allow_ty_def {
|
||||||
|
use super::Bar;
|
||||||
|
|
||||||
|
fn test() {
|
||||||
|
let x = 1;
|
||||||
|
let y = 1;
|
||||||
|
let z = 1;
|
||||||
|
|
||||||
|
// Should NOT lint because `Bar` is defined with `#[allow(clippy::inconsistent_struct_constructor)]`
|
||||||
|
Bar { y, x, z };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
error: struct constructor field order is inconsistent with struct definition field order
|
error: struct constructor field order is inconsistent with struct definition field order
|
||||||
--> tests/ui/inconsistent_struct_constructor.rs:28:9
|
--> tests/ui/inconsistent_struct_constructor.rs:36:9
|
||||||
|
|
|
|
||||||
LL | Foo { y, x, z };
|
LL | Foo { y, x, z };
|
||||||
| ^^^^^^^^^^^^^^^ help: try: `Foo { x, y, z }`
|
| ^^^^^^^^^^^^^^^ help: try: `Foo { x, y, z }`
|
||||||
@ -8,7 +8,7 @@ LL | Foo { y, x, z };
|
|||||||
= help: to override `-D warnings` add `#[allow(clippy::inconsistent_struct_constructor)]`
|
= help: to override `-D warnings` add `#[allow(clippy::inconsistent_struct_constructor)]`
|
||||||
|
|
||||||
error: struct constructor field order is inconsistent with struct definition field order
|
error: struct constructor field order is inconsistent with struct definition field order
|
||||||
--> tests/ui/inconsistent_struct_constructor.rs:55:9
|
--> tests/ui/inconsistent_struct_constructor.rs:63:9
|
||||||
|
|
|
|
||||||
LL | / Foo {
|
LL | / Foo {
|
||||||
LL | | z,
|
LL | | z,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user