Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect allow inconsistent_struct_constructor on the struct definition #13211

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 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::is_lint_allowed;
use clippy_utils::source::snippet;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -71,6 +72,9 @@ impl<'tcx> LateLintPass<'tcx> for InconsistentStructConstructor {
&& let ty = cx.typeck_results().expr_ty(expr)
&& let Some(adt_def) = ty.ty_adt_def()
&& 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)
&& !is_lint_allowed(cx, INCONSISTENT_STRUCT_CONSTRUCTOR, ty_hir_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use fulfill_or_allow just before the span_lint_and_sugg so that #[expect] also works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I've missed this review in my notifications, Pushed a patch addressing this change request.
@rustbot review

&& let Some(variant) = adt_def.variants().iter().next()
{
let mut def_order_map = FxHashMap::default();
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/inconsistent_struct_constructor.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ struct Foo {
z: i32,
}

#[derive(Default)]
#[allow(clippy::inconsistent_struct_constructor)]
struct Bar {
x: i32,
y: i32,
z: i32,
}

mod without_base {
use super::Foo;

Expand Down Expand Up @@ -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() {}
21 changes: 21 additions & 0 deletions tests/ui/inconsistent_struct_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ struct Foo {
z: i32,
}

#[derive(Default)]
#[allow(clippy::inconsistent_struct_constructor)]
struct Bar {
x: i32,
y: i32,
z: i32,
}

mod without_base {
use super::Foo;

Expand Down Expand Up @@ -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() {}
4 changes: 2 additions & 2 deletions tests/ui/inconsistent_struct_constructor.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 };
| ^^^^^^^^^^^^^^^ help: try: `Foo { x, y, z }`
Expand All @@ -8,7 +8,7 @@ LL | Foo { y, x, z };
= help: to override `-D warnings` add `#[allow(clippy::inconsistent_struct_constructor)]`

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 | | z,
Expand Down