Skip to content

Commit 04d70d0

Browse files
committed
[single_match, single_match_else] fix suggestion when match irrefutable
1 parent b86a202 commit 04d70d0

7 files changed

+118
-2
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 is irrefutable, `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 is irrefutable, `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 is irrefutable, `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 is irrefutable, `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

tests/ui/single_match_else.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,7 @@ fn issue_10808(bar: Option<i32>) {
171171
},
172172
}
173173
}
174+
175+
fn irrefutable_match() -> Option<&'static ExprNode> {
176+
{ Some(&NODE) }
177+
}

tests/ui/single_match_else.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,13 @@ fn issue_10808(bar: Option<i32>) {
199199
},
200200
}
201201
}
202+
203+
fn irrefutable_match() -> Option<&'static ExprNode> {
204+
match ExprNode::Butterflies {
205+
ExprNode::Butterflies => Some(&NODE),
206+
_ => {
207+
let x = 5;
208+
None
209+
},
210+
}
211+
}

tests/ui/single_match_else.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,17 @@ LL + println!("None");
197197
LL + }
198198
|
199199

200-
error: aborting due to 9 previous errors
200+
error: this pattern is irrefutable, `match` is useless
201+
--> tests/ui/single_match_else.rs:204:5
202+
|
203+
LL | / match ExprNode::Butterflies {
204+
LL | | ExprNode::Butterflies => Some(&NODE),
205+
LL | | _ => {
206+
LL | | let x = 5;
207+
LL | | None
208+
LL | | },
209+
LL | | }
210+
| |_____^ help: try: `{ Some(&NODE) }`
211+
212+
error: aborting due to 10 previous errors
201213

0 commit comments

Comments
 (0)