Skip to content

Commit e3ca249

Browse files
committed
fix single_match suggestion
1 parent 04d70d0 commit e3ca249

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

clippy_lints/src/matches/single_match.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::{expr_block, snippet, SpanRangeExt};
2+
use clippy_utils::source::{expr_block, snippet, snippet_block_with_context, SpanRangeExt};
33
use clippy_utils::ty::implements_trait;
44
use clippy_utils::{
55
is_lint_allowed, is_unit_expr, peel_blocks, peel_hir_pat_refs, peel_middle_ty_refs, peel_n_hir_expr_refs,
@@ -9,7 +9,7 @@ use rustc_arena::DroplessArena;
99
use rustc_errors::Applicability;
1010
use rustc_hir::def::{DefKind, Res};
1111
use rustc_hir::intravisit::{walk_pat, Visitor};
12-
use rustc_hir::{Arm, Expr, ExprKind, HirId, Pat, PatKind, QPath};
12+
use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind};
1313
use rustc_lint::LateContext;
1414
use rustc_middle::ty::{self, AdtDef, ParamEnv, TyCtxt, TypeckResults, VariantDef};
1515
use rustc_span::{sym, Span};
@@ -93,8 +93,24 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
9393

9494
if snippet(cx, ex.span, "..") == snippet(cx, arm.pat.span, "..") {
9595
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);
96+
let (sugg, help) = if is_unit_expr(arm.body) {
97+
(String::new(), "`match` expression can be removed")
98+
} else {
99+
let mut sugg = snippet_block_with_context(cx, arm.body.span, ctxt, "..", Some(expr.span), &mut app)
100+
.0
101+
.to_string();
102+
if let Node::Stmt(stmt) = cx.tcx.parent_hir_node(expr.hir_id)
103+
&& let StmtKind::Expr(_) = stmt.kind
104+
&& match arm.body.kind {
105+
ExprKind::Block(block, _) => block.span.from_expansion(),
106+
_ => true,
107+
}
108+
{
109+
sugg.push(';');
110+
}
111+
(sugg, "try")
112+
};
113+
span_lint_and_sugg(cx, lint, expr.span, msg, help, sugg.to_string(), app);
98114
return;
99115
}
100116

tests/ui/single_match.fixed

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,19 @@ const DATA: Data = Data([1, 2, 3, 4]);
304304
const CONST_I32: i32 = 1;
305305

306306
fn irrefutable_match() {
307-
{ println!() }
307+
println!();
308308

309-
{ println!() }
309+
println!();
310310

311311
let i = 0;
312312
{
313313
let a = 1;
314314
let b = 2;
315315
}
316+
317+
318+
319+
320+
321+
println!()
316322
}

tests/ui/single_match.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,19 @@ fn irrefutable_match() {
386386
},
387387
_ => {},
388388
}
389+
390+
match i {
391+
i => {},
392+
_ => {},
393+
}
394+
395+
match i {
396+
i => (),
397+
_ => (),
398+
}
399+
400+
match CONST_I32 {
401+
CONST_I32 => println!(),
402+
_ => {},
403+
}
389404
}

tests/ui/single_match.stderr

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ LL | / match DATA {
223223
LL | | DATA => println!(),
224224
LL | | _ => {},
225225
LL | | }
226-
| |_____^ help: try: `{ println!() }`
226+
| |_____^ help: try: `println!();`
227227

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

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

257-
error: aborting due to 23 previous errors
257+
error: this pattern is irrefutable, `match` is useless
258+
--> tests/ui/single_match.rs:390:5
259+
|
260+
LL | / match i {
261+
LL | | i => {},
262+
LL | | _ => {},
263+
LL | | }
264+
| |_____^ help: `match` expression can be removed
265+
266+
error: this pattern is irrefutable, `match` is useless
267+
--> tests/ui/single_match.rs:395:5
268+
|
269+
LL | / match i {
270+
LL | | i => (),
271+
LL | | _ => (),
272+
LL | | }
273+
| |_____^ help: `match` expression can be removed
274+
275+
error: this pattern is irrefutable, `match` is useless
276+
--> tests/ui/single_match.rs:400:5
277+
|
278+
LL | / match CONST_I32 {
279+
LL | | CONST_I32 => println!(),
280+
LL | | _ => {},
281+
LL | | }
282+
| |_____^ help: try: `println!()`
283+
284+
error: aborting due to 26 previous errors
258285

tests/ui/single_match_else.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,5 @@ fn issue_10808(bar: Option<i32>) {
173173
}
174174

175175
fn irrefutable_match() -> Option<&'static ExprNode> {
176-
{ Some(&NODE) }
176+
Some(&NODE)
177177
}

tests/ui/single_match_else.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ LL | | let x = 5;
207207
LL | | None
208208
LL | | },
209209
LL | | }
210-
| |_____^ help: try: `{ Some(&NODE) }`
210+
| |_____^ help: try: `Some(&NODE)`
211211

212212
error: aborting due to 10 previous errors
213213

0 commit comments

Comments
 (0)