Skip to content

Commit dc57e57

Browse files
authored
fix(manual_let_else): add trailing comma to struct patterns ending with .. (rust-lang#16442)
fixes rust-lang#16433 Adds a trailing comma to the last field of a struct pattern if it ends with a `..` to avoid an invalid suggestion. A test was added as well. changelog: [`manual_let_else`] fix suggestion for `..` patterns
2 parents f43f1d6 + 1ebafc5 commit dc57e57

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn replace_in_pattern(
301301
.collect::<Vec<_>>();
302302
let fields_string = fields.join(", ");
303303

304-
let dot_dot_str = if dot_dot.is_some() { " .." } else { "" };
304+
let dot_dot_str = if dot_dot.is_some() { ", .." } else { "" };
305305
let (sn_pth, _) = snippet_with_context(cx, path.span(), span.ctxt(), "", app);
306306
return format!("{sn_pth} {{ {fields_string}{dot_dot_str} }}");
307307
},

tests/ui/manual_let_else_match.fixed

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused_braces, unused_variables, dead_code)]
1+
#![allow(unused_braces, unused_variables, dead_code, irrefutable_let_patterns)]
22
#![allow(
33
clippy::collapsible_else_if,
44
clippy::let_unit_value,
@@ -182,3 +182,16 @@ fn issue9939b() {
182182
let Some(Issue9939b { earthquake: erosion, hurricane: _x }) = issue else { unreachable!("can't happen") };
183183
assert!(erosion);
184184
}
185+
186+
mod issue16433 {
187+
// https://github.com/rust-lang/rust-clippy/issues/16433
188+
struct A {
189+
a: u32,
190+
b: u32,
191+
}
192+
193+
fn foo() {
194+
let a = A { a: 1, b: 1 };
195+
let A { a: first_arg, .. } = a else { return };
196+
}
197+
}

tests/ui/manual_let_else_match.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused_braces, unused_variables, dead_code)]
1+
#![allow(unused_braces, unused_variables, dead_code, irrefutable_let_patterns)]
22
#![allow(
33
clippy::collapsible_else_if,
44
clippy::let_unit_value,
@@ -250,3 +250,20 @@ fn issue9939b() {
250250
};
251251
assert!(erosion);
252252
}
253+
254+
mod issue16433 {
255+
// https://github.com/rust-lang/rust-clippy/issues/16433
256+
struct A {
257+
a: u32,
258+
b: u32,
259+
}
260+
261+
fn foo() {
262+
let a = A { a: 1, b: 1 };
263+
let first_arg = match a {
264+
//~^ manual_let_else
265+
A { a, .. } => a,
266+
_ => return,
267+
};
268+
}
269+
}

tests/ui/manual_let_else_match.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,15 @@ LL | | None => unreachable!("can't happen"),
171171
LL | | };
172172
| |______^ help: consider writing: `let Some(Issue9939b { earthquake: erosion, hurricane: _x }) = issue else { unreachable!("can't happen") };`
173173

174-
error: aborting due to 17 previous errors
174+
error: this could be rewritten as `let...else`
175+
--> tests/ui/manual_let_else_match.rs:263:9
176+
|
177+
LL | / let first_arg = match a {
178+
LL | |
179+
LL | | A { a, .. } => a,
180+
LL | | _ => return,
181+
LL | | };
182+
| |__________^ help: consider writing: `let A { a: first_arg, .. } = a else { return };`
183+
184+
error: aborting due to 18 previous errors
175185

0 commit comments

Comments
 (0)