Skip to content

Commit

Permalink
fix single_match suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiTheShinobi committed Sep 6, 2024
1 parent 04d70d0 commit e3ca249
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
24 changes: 20 additions & 4 deletions clippy_lints/src/matches/single_match.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{expr_block, snippet, SpanRangeExt};
use clippy_utils::source::{expr_block, snippet, snippet_block_with_context, SpanRangeExt};
use clippy_utils::ty::implements_trait;
use clippy_utils::{
is_lint_allowed, is_unit_expr, peel_blocks, peel_hir_pat_refs, peel_middle_ty_refs, peel_n_hir_expr_refs,
Expand All @@ -9,7 +9,7 @@ use rustc_arena::DroplessArena;
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{walk_pat, Visitor};
use rustc_hir::{Arm, Expr, ExprKind, HirId, Pat, PatKind, QPath};
use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, AdtDef, ParamEnv, TyCtxt, TypeckResults, VariantDef};
use rustc_span::{sym, Span};
Expand Down Expand Up @@ -93,8 +93,24 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp

if snippet(cx, ex.span, "..") == snippet(cx, arm.pat.span, "..") {
let msg = "this pattern is irrefutable, `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);
let (sugg, help) = if is_unit_expr(arm.body) {
(String::new(), "`match` expression can be removed")
} else {
let mut sugg = snippet_block_with_context(cx, arm.body.span, ctxt, "..", Some(expr.span), &mut app)
.0
.to_string();
if let Node::Stmt(stmt) = cx.tcx.parent_hir_node(expr.hir_id)
&& let StmtKind::Expr(_) = stmt.kind
&& match arm.body.kind {
ExprKind::Block(block, _) => block.span.from_expansion(),
_ => true,
}
{
sugg.push(';');
}
(sugg, "try")
};
span_lint_and_sugg(cx, lint, expr.span, msg, help, sugg.to_string(), app);
return;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/ui/single_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,19 @@ const DATA: Data = Data([1, 2, 3, 4]);
const CONST_I32: i32 = 1;

fn irrefutable_match() {
{ println!() }
println!();

{ println!() }
println!();

let i = 0;
{
let a = 1;
let b = 2;
}





println!()
}
15 changes: 15 additions & 0 deletions tests/ui/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,19 @@ fn irrefutable_match() {
},
_ => {},
}

match i {
i => {},
_ => {},
}

match i {
i => (),
_ => (),
}

match CONST_I32 {
CONST_I32 => println!(),
_ => {},
}
}
33 changes: 30 additions & 3 deletions tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ LL | / match DATA {
LL | | DATA => println!(),
LL | | _ => {},
LL | | }
| |_____^ help: try: `{ println!() }`
| |_____^ help: try: `println!();`

error: this pattern is irrefutable, `match` is useless
--> tests/ui/single_match.rs:376:5
Expand All @@ -232,7 +232,7 @@ LL | / match CONST_I32 {
LL | | CONST_I32 => println!(),
LL | | _ => {},
LL | | }
| |_____^ help: try: `{ println!() }`
| |_____^ help: try: `println!();`

error: this pattern is irrefutable, `match` is useless
--> tests/ui/single_match.rs:382:5
Expand All @@ -254,5 +254,32 @@ LL + let b = 2;
LL + }
|

error: aborting due to 23 previous errors
error: this pattern is irrefutable, `match` is useless
--> tests/ui/single_match.rs:390:5
|
LL | / match i {
LL | | i => {},
LL | | _ => {},
LL | | }
| |_____^ help: `match` expression can be removed

error: this pattern is irrefutable, `match` is useless
--> tests/ui/single_match.rs:395:5
|
LL | / match i {
LL | | i => (),
LL | | _ => (),
LL | | }
| |_____^ help: `match` expression can be removed

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

error: aborting due to 26 previous errors

2 changes: 1 addition & 1 deletion tests/ui/single_match_else.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ fn issue_10808(bar: Option<i32>) {
}

fn irrefutable_match() -> Option<&'static ExprNode> {
{ Some(&NODE) }
Some(&NODE)
}
2 changes: 1 addition & 1 deletion tests/ui/single_match_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ LL | | let x = 5;
LL | | None
LL | | },
LL | | }
| |_____^ help: try: `{ Some(&NODE) }`
| |_____^ help: try: `Some(&NODE)`

error: aborting due to 10 previous errors

0 comments on commit e3ca249

Please sign in to comment.