Skip to content

Commit 063185e

Browse files
committed
stacked_if_match done
1 parent 05c38db commit 063185e

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

clippy_lints/src/stacked_if_match.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
12
use clippy_utils::source::snippet;
2-
use rustc_middle::lint::in_external_macro;
3+
use clippy_utils::visitors::{for_each_expr, Descend};
4+
use rustc_errors::Applicability;
35
use rustc_hir::*;
46
use rustc_lint::{LateContext, LateLintPass, LintContext};
7+
use rustc_middle::lint::in_external_macro;
58
use rustc_session::declare_lint_pass;
6-
use rustc_errors::Applicability;
7-
use clippy_utils::visitors::{for_each_expr, Descend};
8-
use clippy_utils::diagnostics::span_lint_and_sugg;
99
use std::ops::ControlFlow;
1010

1111
declare_clippy_lint! {
@@ -47,7 +47,7 @@ declare_clippy_lint! {
4747
declare_lint_pass!(StackedIfMatch => [STACKED_IF_MATCH]);
4848

4949
impl<'tcx> LateLintPass<'tcx> for StackedIfMatch {
50-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
50+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
5151
if expr.span.from_expansion() || in_external_macro(cx.sess(), expr.span) {
5252
return;
5353
}
@@ -74,15 +74,20 @@ impl<'tcx> LateLintPass<'tcx> for StackedIfMatch {
7474
return ControlFlow::Continue(Descend::No);
7575
}
7676

77-
if (keyword == "if" && matches!(sub_expr.kind, ExprKind::If(..)))
78-
|| (keyword == "match" && matches!(sub_expr.kind, ExprKind::Match(.., MatchSource::Normal))) {
77+
let sub_keyword = match sub_expr.kind {
78+
ExprKind::If(..) => "if",
79+
ExprKind::Match(.., MatchSource::Normal) => "match",
80+
_ => "",
81+
};
82+
83+
if keyword == sub_keyword {
7984
let inner_snippet = snippet(cx, sub_expr.span, "..");
8085
span_lint_and_sugg(
8186
cx,
8287
STACKED_IF_MATCH,
8388
expr.span.with_hi(sub_expr.span.hi()),
84-
format!("avoid using `{keyword} {keyword}`"),
85-
format!("try binding inner `{keyword}` with `let`"),
89+
format!("avoid using `{keyword} {keyword}` by binding inner `{keyword}` with `let`"),
90+
format!("try"),
8691
format!("let result = {inner_snippet}; {keyword} result"),
8792
Applicability::MachineApplicable,
8893
);

tests/ui/stacked_if_match.rs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
#![warn(clippy::stacked_if_match)]
22
#![allow(unused)]
33

4-
54
fn stacked_if() {
65
let x = 0;
7-
if if x == 1 {
8-
x == 2
9-
} else {
10-
x == 3
11-
} {
6+
if if x == 1 { x == 2 } else { x == 3 } {
7+
println!("true");
8+
}
9+
10+
if if x == 1 { 2 } else { 3 } == 4 {
1211
println!("true");
1312
}
1413

1514
if if x == 1 {
16-
2
15+
let y = 2;
16+
y == 2
1717
} else {
18-
3
19-
} == 4 {
18+
let z = 3;
19+
z == 3
20+
} {
2021
println!("true");
2122
}
2223
}
@@ -35,7 +36,8 @@ fn stacked_match() {
3536
match match x {
3637
1 => 2,
3738
_ => 3,
38-
} + 1 {
39+
} + 1
40+
{
3941
1 => {},
4042
2 => {},
4143
_ => {},
@@ -45,19 +47,11 @@ fn stacked_match() {
4547
fn if_no_lint() {
4648
let x = 0;
4749

48-
if (if x == 1 {
49-
x == 2
50-
} else {
51-
x == 3
52-
}) {
50+
if (if x == 1 { x == 2 } else { x == 3 }) {
5351
println!("true");
5452
}
5553

56-
if 1 == if x == 1 {
57-
1
58-
} else {
59-
2
60-
} {
54+
if 1 == if x == 1 { 1 } else { 2 } {
6155
println!("true");
6256
}
6357
}
@@ -85,11 +79,7 @@ fn match_no_lint() {
8579

8680
macro_rules! if_macro {
8781
($var:ident) => {
88-
if $var == 1 {
89-
true
90-
} else {
91-
false
92-
}
82+
if $var == 1 { true } else { false }
9383
};
9484
}
9585

@@ -104,11 +94,7 @@ macro_rules! match_macro {
10494

10595
macro_rules! if_if_macro {
10696
() => {
107-
if if true {
108-
true
109-
} else {
110-
false
111-
} {
97+
if if true { true } else { false } {
11298
println!("true");
11399
}
114100
};

0 commit comments

Comments
 (0)