Skip to content

Commit 743405d

Browse files
authored
Do not replace match by if if any arm contains a binding (rust-lang#15352)
changelog: [`match_bool`]: do not replace `match` by `if` if any arm contains a binding Fixes rust-lang/rust-clippy#15351
2 parents 763420c + 7ed763b commit 743405d

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

clippy_lints/src/matches/match_bool.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ use super::MATCH_BOOL;
1212

1313
pub(crate) fn check(cx: &LateContext<'_>, scrutinee: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1414
// Type of expression is `bool`.
15-
if *cx.typeck_results().expr_ty(scrutinee).kind() == ty::Bool {
15+
if *cx.typeck_results().expr_ty(scrutinee).kind() == ty::Bool
16+
&& arms
17+
.iter()
18+
.all(|arm| arm.pat.walk_short(|p| !matches!(p.kind, PatKind::Binding(..))))
19+
{
1620
span_lint_and_then(
1721
cx,
1822
MATCH_BOOL,

tests/ui/match_bool.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,17 @@ fn issue14099() {
6161
} }
6262
}
6363

64+
fn issue15351() {
65+
let mut d = false;
66+
match d {
67+
false => println!("foo"),
68+
ref mut d => *d = false,
69+
}
70+
71+
match d {
72+
false => println!("foo"),
73+
e => println!("{e}"),
74+
}
75+
}
76+
6477
fn main() {}

tests/ui/match_bool.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,17 @@ fn issue14099() {
113113
}
114114
}
115115

116+
fn issue15351() {
117+
let mut d = false;
118+
match d {
119+
false => println!("foo"),
120+
ref mut d => *d = false,
121+
}
122+
123+
match d {
124+
false => println!("foo"),
125+
e => println!("{e}"),
126+
}
127+
}
128+
116129
fn main() {}

0 commit comments

Comments
 (0)