Skip to content

Commit d457260

Browse files
suggestion for rest_pat_in_fully_bound_structs (#15648)
changelog: [`rest_pat_in_fully_bound_structs`]: add suggestion
2 parents 55570c9 + 8f84324 commit d457260

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ use super::REST_PAT_IN_FULLY_BOUND_STRUCTS;
77

88
pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
99
if !pat.span.from_expansion()
10-
&& let PatKind::Struct(QPath::Resolved(_, path), fields, Some(_)) = pat.kind
10+
&& let PatKind::Struct(QPath::Resolved(_, path), fields, Some(dotdot)) = pat.kind
1111
&& let Some(def_id) = path.res.opt_def_id()
1212
&& let ty = cx.tcx.type_of(def_id).instantiate_identity()
1313
&& let ty::Adt(def, _) = ty.kind()
1414
&& (def.is_struct() || def.is_union())
1515
&& fields.len() == def.non_enum_variant().fields.len()
1616
&& !def.non_enum_variant().is_field_list_non_exhaustive()
1717
{
18-
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
1918
span_lint_and_then(
2019
cx,
2120
REST_PAT_IN_FULLY_BOUND_STRUCTS,
2221
pat.span,
2322
"unnecessary use of `..` pattern in struct binding. All fields were already bound",
2423
|diag| {
25-
diag.help("consider removing `..` from this binding");
24+
diag.span_suggestion_verbose(
25+
dotdot,
26+
"consider removing `..` from this binding",
27+
"",
28+
rustc_errors::Applicability::MachineApplicable,
29+
);
2630
},
2731
);
2832
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#![warn(clippy::rest_pat_in_fully_bound_structs)]
2+
#![allow(clippy::struct_field_names)]
3+
4+
struct A {
5+
a: i32,
6+
b: i64,
7+
c: &'static str,
8+
}
9+
10+
macro_rules! foo {
11+
($param:expr) => {
12+
match $param {
13+
A { a: 0, b: 0, c: "", .. } => {},
14+
_ => {},
15+
}
16+
};
17+
}
18+
19+
fn main() {
20+
let a_struct = A { a: 5, b: 42, c: "A" };
21+
22+
match a_struct {
23+
A { a: 5, b: 42, c: "", } => {}, // Lint
24+
//~^ rest_pat_in_fully_bound_structs
25+
A { a: 0, b: 0, c: "", } => {}, // Lint
26+
//~^ rest_pat_in_fully_bound_structs
27+
_ => {},
28+
}
29+
30+
match a_struct {
31+
A { a: 5, b: 42, .. } => {},
32+
A { a: 0, b: 0, c: "", } => {}, // Lint
33+
//~^ rest_pat_in_fully_bound_structs
34+
_ => {},
35+
}
36+
37+
// No lint
38+
match a_struct {
39+
A { a: 5, .. } => {},
40+
A { a: 0, b: 0, .. } => {},
41+
_ => {},
42+
}
43+
44+
// No lint
45+
foo!(a_struct);
46+
47+
#[non_exhaustive]
48+
struct B {
49+
a: u32,
50+
b: u32,
51+
c: u64,
52+
}
53+
54+
let b_struct = B { a: 5, b: 42, c: 342 };
55+
56+
match b_struct {
57+
B { a: 5, b: 42, .. } => {},
58+
B { a: 0, b: 0, c: 128, .. } => {}, // No Lint
59+
_ => {},
60+
}
61+
}

tests/ui/rest_pat_in_fully_bound_structs.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@ error: unnecessary use of `..` pattern in struct binding. All fields were alread
44
LL | A { a: 5, b: 42, c: "", .. } => {}, // Lint
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: consider removing `..` from this binding
87
= note: `-D clippy::rest-pat-in-fully-bound-structs` implied by `-D warnings`
98
= help: to override `-D warnings` add `#[allow(clippy::rest_pat_in_fully_bound_structs)]`
9+
help: consider removing `..` from this binding
10+
|
11+
LL - A { a: 5, b: 42, c: "", .. } => {}, // Lint
12+
LL + A { a: 5, b: 42, c: "", } => {}, // Lint
13+
|
1014

1115
error: unnecessary use of `..` pattern in struct binding. All fields were already bound
1216
--> tests/ui/rest_pat_in_fully_bound_structs.rs:25:9
1317
|
1418
LL | A { a: 0, b: 0, c: "", .. } => {}, // Lint
1519
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1620
|
17-
= help: consider removing `..` from this binding
21+
help: consider removing `..` from this binding
22+
|
23+
LL - A { a: 0, b: 0, c: "", .. } => {}, // Lint
24+
LL + A { a: 0, b: 0, c: "", } => {}, // Lint
25+
|
1826

1927
error: unnecessary use of `..` pattern in struct binding. All fields were already bound
2028
--> tests/ui/rest_pat_in_fully_bound_structs.rs:32:9
2129
|
2230
LL | A { a: 0, b: 0, c: "", .. } => {}, // Lint
2331
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2432
|
25-
= help: consider removing `..` from this binding
33+
help: consider removing `..` from this binding
34+
|
35+
LL - A { a: 0, b: 0, c: "", .. } => {}, // Lint
36+
LL + A { a: 0, b: 0, c: "", } => {}, // Lint
37+
|
2638

2739
error: aborting due to 3 previous errors
2840

0 commit comments

Comments
 (0)