Skip to content

Commit de07605

Browse files
committed
[single_match] fix suggestion when match irrefutable
1 parent b86a202 commit de07605

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

clippy_lints/src/matches/single_match.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
9191
format!(" else {}", expr_block(cx, els, ctxt, "..", Some(expr.span), &mut app))
9292
});
9393

94+
if snippet(cx, ex.span, "..") == snippet(cx, arm.pat.span, "..") {
95+
let msg = "this pattern will always match, `match` is useless";
96+
let sugg = expr_block(cx, arm.body, ctxt, "..", Some(expr.span), &mut app);
97+
span_lint_and_sugg(cx, lint, expr.span, msg, "try", sugg, app);
98+
return;
99+
}
100+
94101
let (pat, pat_ref_count) = peel_hir_pat_refs(arm.pat);
95102
let (msg, sugg) = if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind
96103
&& let (ty, ty_ref_count) = peel_middle_ty_refs(cx.typeck_results().expr_ty(ex))

tests/ui/single_match.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,21 @@ fn issue11365() {
296296

297297
if let Some(A | B) = &Some(A) { println!() }
298298
}
299+
300+
#[derive(Eq, PartialEq)]
301+
pub struct Data([u8; 4]);
302+
303+
const DATA: Data = Data([1, 2, 3, 4]);
304+
const CONST_I32: i32 = 1;
305+
306+
fn irrefutable_match() {
307+
{ println!() }
308+
309+
{ println!() }
310+
311+
let i = 0;
312+
{
313+
let a = 1;
314+
let b = 2;
315+
}
316+
}

tests/ui/single_match.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,30 @@ fn issue11365() {
360360
None | Some(_) => {},
361361
}
362362
}
363+
364+
#[derive(Eq, PartialEq)]
365+
pub struct Data([u8; 4]);
366+
367+
const DATA: Data = Data([1, 2, 3, 4]);
368+
const CONST_I32: i32 = 1;
369+
370+
fn irrefutable_match() {
371+
match DATA {
372+
DATA => println!(),
373+
_ => {},
374+
}
375+
376+
match CONST_I32 {
377+
CONST_I32 => println!(),
378+
_ => {},
379+
}
380+
381+
let i = 0;
382+
match i {
383+
i => {
384+
let a = 1;
385+
let b = 2;
386+
},
387+
_ => {},
388+
}
389+
}

tests/ui/single_match.stderr

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,43 @@ LL | | None | Some(_) => {},
216216
LL | | }
217217
| |_____^ help: try: `if let Some(A | B) = &Some(A) { println!() }`
218218

219-
error: aborting due to 20 previous errors
219+
error: this pattern will always match, `match` is useless
220+
--> tests/ui/single_match.rs:371:5
221+
|
222+
LL | / match DATA {
223+
LL | | DATA => println!(),
224+
LL | | _ => {},
225+
LL | | }
226+
| |_____^ help: try: `{ println!() }`
227+
228+
error: this pattern will always match, `match` is useless
229+
--> tests/ui/single_match.rs:376:5
230+
|
231+
LL | / match CONST_I32 {
232+
LL | | CONST_I32 => println!(),
233+
LL | | _ => {},
234+
LL | | }
235+
| |_____^ help: try: `{ println!() }`
236+
237+
error: this pattern will always match, `match` is useless
238+
--> tests/ui/single_match.rs:382:5
239+
|
240+
LL | / match i {
241+
LL | | i => {
242+
LL | | let a = 1;
243+
LL | | let b = 2;
244+
LL | | },
245+
LL | | _ => {},
246+
LL | | }
247+
| |_____^
248+
|
249+
help: try
250+
|
251+
LL ~ {
252+
LL + let a = 1;
253+
LL + let b = 2;
254+
LL + }
255+
|
256+
257+
error: aborting due to 23 previous errors
220258

0 commit comments

Comments
 (0)