Skip to content

Commit

Permalink
[single_match] fix suggestion when match irrefutable
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiTheShinobi committed Aug 30, 2024
1 parent b86a202 commit de07605
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
7 changes: 7 additions & 0 deletions clippy_lints/src/matches/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
format!(" else {}", expr_block(cx, els, ctxt, "..", Some(expr.span), &mut app))
});

if snippet(cx, ex.span, "..") == snippet(cx, arm.pat.span, "..") {
let msg = "this pattern will always match, `match` is useless";
let sugg = expr_block(cx, arm.body, ctxt, "..", Some(expr.span), &mut app);
span_lint_and_sugg(cx, lint, expr.span, msg, "try", sugg, app);
return;
}

let (pat, pat_ref_count) = peel_hir_pat_refs(arm.pat);
let (msg, sugg) = if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind
&& let (ty, ty_ref_count) = peel_middle_ty_refs(cx.typeck_results().expr_ty(ex))
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/single_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,21 @@ fn issue11365() {

if let Some(A | B) = &Some(A) { println!() }
}

#[derive(Eq, PartialEq)]
pub struct Data([u8; 4]);

const DATA: Data = Data([1, 2, 3, 4]);
const CONST_I32: i32 = 1;

fn irrefutable_match() {
{ println!() }

{ println!() }

let i = 0;
{
let a = 1;
let b = 2;
}
}
27 changes: 27 additions & 0 deletions tests/ui/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,30 @@ fn issue11365() {
None | Some(_) => {},
}
}

#[derive(Eq, PartialEq)]
pub struct Data([u8; 4]);

const DATA: Data = Data([1, 2, 3, 4]);
const CONST_I32: i32 = 1;

fn irrefutable_match() {
match DATA {
DATA => println!(),
_ => {},
}

match CONST_I32 {
CONST_I32 => println!(),
_ => {},
}

let i = 0;
match i {
i => {
let a = 1;
let b = 2;
},
_ => {},
}
}
40 changes: 39 additions & 1 deletion tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,43 @@ LL | | None | Some(_) => {},
LL | | }
| |_____^ help: try: `if let Some(A | B) = &Some(A) { println!() }`

error: aborting due to 20 previous errors
error: this pattern will always match, `match` is useless
--> tests/ui/single_match.rs:371:5
|
LL | / match DATA {
LL | | DATA => println!(),
LL | | _ => {},
LL | | }
| |_____^ help: try: `{ println!() }`

error: this pattern will always match, `match` is useless
--> tests/ui/single_match.rs:376:5
|
LL | / match CONST_I32 {
LL | | CONST_I32 => println!(),
LL | | _ => {},
LL | | }
| |_____^ help: try: `{ println!() }`

error: this pattern will always match, `match` is useless
--> tests/ui/single_match.rs:382:5
|
LL | / match i {
LL | | i => {
LL | | let a = 1;
LL | | let b = 2;
LL | | },
LL | | _ => {},
LL | | }
| |_____^
|
help: try
|
LL ~ {
LL + let a = 1;
LL + let b = 2;
LL + }
|

error: aborting due to 23 previous errors

0 comments on commit de07605

Please sign in to comment.