Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ use super::REST_PAT_IN_FULLY_BOUND_STRUCTS;

pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
if !pat.span.from_expansion()
&& let PatKind::Struct(QPath::Resolved(_, path), fields, Some(_)) = pat.kind
&& let PatKind::Struct(QPath::Resolved(_, path), fields, Some(dotdot)) = pat.kind
&& let Some(def_id) = path.res.opt_def_id()
&& let ty = cx.tcx.type_of(def_id).instantiate_identity()
&& let ty::Adt(def, _) = ty.kind()
&& (def.is_struct() || def.is_union())
&& fields.len() == def.non_enum_variant().fields.len()
&& !def.non_enum_variant().is_field_list_non_exhaustive()
{
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
REST_PAT_IN_FULLY_BOUND_STRUCTS,
pat.span,
"unnecessary use of `..` pattern in struct binding. All fields were already bound",
|diag| {
diag.help("consider removing `..` from this binding");
diag.span_suggestion_verbose(
dotdot,
"consider removing `..` from this binding",
"",
rustc_errors::Applicability::MachineApplicable,
);
},
);
}
Expand Down
61 changes: 61 additions & 0 deletions tests/ui/rest_pat_in_fully_bound_structs.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![warn(clippy::rest_pat_in_fully_bound_structs)]
#![allow(clippy::struct_field_names)]

struct A {
a: i32,
b: i64,
c: &'static str,
}

macro_rules! foo {
($param:expr) => {
match $param {
A { a: 0, b: 0, c: "", .. } => {},
_ => {},
}
};
}

fn main() {
let a_struct = A { a: 5, b: 42, c: "A" };

match a_struct {
A { a: 5, b: 42, c: "", } => {}, // Lint
//~^ rest_pat_in_fully_bound_structs
A { a: 0, b: 0, c: "", } => {}, // Lint
//~^ rest_pat_in_fully_bound_structs
_ => {},
}

match a_struct {
A { a: 5, b: 42, .. } => {},
A { a: 0, b: 0, c: "", } => {}, // Lint
//~^ rest_pat_in_fully_bound_structs
_ => {},
}

// No lint
match a_struct {
A { a: 5, .. } => {},
A { a: 0, b: 0, .. } => {},
_ => {},
}

// No lint
foo!(a_struct);

#[non_exhaustive]
struct B {
a: u32,
b: u32,
c: u64,
}

let b_struct = B { a: 5, b: 42, c: 342 };

match b_struct {
B { a: 5, b: 42, .. } => {},
B { a: 0, b: 0, c: 128, .. } => {}, // No Lint
_ => {},
}
}
18 changes: 15 additions & 3 deletions tests/ui/rest_pat_in_fully_bound_structs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@ error: unnecessary use of `..` pattern in struct binding. All fields were alread
LL | A { a: 5, b: 42, c: "", .. } => {}, // Lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `..` from this binding
= note: `-D clippy::rest-pat-in-fully-bound-structs` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::rest_pat_in_fully_bound_structs)]`
help: consider removing `..` from this binding
|
LL - A { a: 5, b: 42, c: "", .. } => {}, // Lint
LL + A { a: 5, b: 42, c: "", } => {}, // Lint
|

error: unnecessary use of `..` pattern in struct binding. All fields were already bound
--> tests/ui/rest_pat_in_fully_bound_structs.rs:25:9
|
LL | A { a: 0, b: 0, c: "", .. } => {}, // Lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `..` from this binding
help: consider removing `..` from this binding
|
LL - A { a: 0, b: 0, c: "", .. } => {}, // Lint
LL + A { a: 0, b: 0, c: "", } => {}, // Lint
|

error: unnecessary use of `..` pattern in struct binding. All fields were already bound
--> tests/ui/rest_pat_in_fully_bound_structs.rs:32:9
|
LL | A { a: 0, b: 0, c: "", .. } => {}, // Lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `..` from this binding
help: consider removing `..` from this binding
|
LL - A { a: 0, b: 0, c: "", .. } => {}, // Lint
LL + A { a: 0, b: 0, c: "", } => {}, // Lint
|

error: aborting due to 3 previous errors