Skip to content

Commit cca9938

Browse files
committed
Auto merge of rust-lang#9714 - Alexendoo:bool-to-int-if-let, r=xFrednet
Fix `bool_to_int_with_if` false positive with `if let` Fixes rust-lang#9706 changelog: FP: [`bool_to_int_with_if`]: Now ignores `if let` statements
2 parents 8e19251 + ad5dfcd commit cca9938

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clippy_utils::higher::If;
12
use rustc_ast::LitKind;
23
use rustc_hir::{Block, ExprKind};
34
use rustc_lint::{LateContext, LateLintPass};
@@ -52,9 +53,9 @@ impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
5253
}
5354

5455
fn check_if_else<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
55-
if let ExprKind::If(check, then, Some(else_)) = expr.kind
56+
if let Some(If { cond, then, r#else: Some(r#else) }) = If::hir(expr)
5657
&& let Some(then_lit) = int_literal(then)
57-
&& let Some(else_lit) = int_literal(else_)
58+
&& let Some(else_lit) = int_literal(r#else)
5859
{
5960
let inverted = if is_integer_literal(then_lit, 1) && is_integer_literal(else_lit, 0) {
6061
false
@@ -66,7 +67,7 @@ fn check_if_else<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>
6667
};
6768
let mut applicability = Applicability::MachineApplicable;
6869
let snippet = {
69-
let mut sugg = Sugg::hir_with_applicability(cx, check, "..", &mut applicability);
70+
let mut sugg = Sugg::hir_with_applicability(cx, cond, "..", &mut applicability);
7071
if inverted {
7172
sugg = !sugg;
7273
}

tests/ui/bool_to_int_with_if.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22

3+
#![feature(let_chains)]
34
#![warn(clippy::bool_to_int_with_if)]
45
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
56

@@ -91,3 +92,22 @@ fn side_effect() {}
9192
fn cond(a: bool, b: bool) -> bool {
9293
a || b
9394
}
95+
96+
enum Enum {
97+
A,
98+
B,
99+
}
100+
101+
fn if_let(a: Enum, b: Enum) {
102+
if let Enum::A = a {
103+
1
104+
} else {
105+
0
106+
};
107+
108+
if let Enum::A = a && let Enum::B = b {
109+
1
110+
} else {
111+
0
112+
};
113+
}

tests/ui/bool_to_int_with_if.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22

3+
#![feature(let_chains)]
34
#![warn(clippy::bool_to_int_with_if)]
45
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
56

@@ -123,3 +124,22 @@ fn side_effect() {}
123124
fn cond(a: bool, b: bool) -> bool {
124125
a || b
125126
}
127+
128+
enum Enum {
129+
A,
130+
B,
131+
}
132+
133+
fn if_let(a: Enum, b: Enum) {
134+
if let Enum::A = a {
135+
1
136+
} else {
137+
0
138+
};
139+
140+
if let Enum::A = a && let Enum::B = b {
141+
1
142+
} else {
143+
0
144+
};
145+
}

tests/ui/bool_to_int_with_if.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: boolean to int conversion using if
2-
--> $DIR/bool_to_int_with_if.rs:15:5
2+
--> $DIR/bool_to_int_with_if.rs:16:5
33
|
44
LL | / if a {
55
LL | | 1
@@ -12,7 +12,7 @@ LL | | };
1212
= note: `-D clippy::bool-to-int-with-if` implied by `-D warnings`
1313

1414
error: boolean to int conversion using if
15-
--> $DIR/bool_to_int_with_if.rs:20:5
15+
--> $DIR/bool_to_int_with_if.rs:21:5
1616
|
1717
LL | / if a {
1818
LL | | 0
@@ -24,7 +24,7 @@ LL | | };
2424
= note: `!a as i32` or `(!a).into()` can also be valid options
2525

2626
error: boolean to int conversion using if
27-
--> $DIR/bool_to_int_with_if.rs:25:5
27+
--> $DIR/bool_to_int_with_if.rs:26:5
2828
|
2929
LL | / if !a {
3030
LL | | 1
@@ -36,7 +36,7 @@ LL | | };
3636
= note: `!a as i32` or `(!a).into()` can also be valid options
3737

3838
error: boolean to int conversion using if
39-
--> $DIR/bool_to_int_with_if.rs:30:5
39+
--> $DIR/bool_to_int_with_if.rs:31:5
4040
|
4141
LL | / if a || b {
4242
LL | | 1
@@ -48,7 +48,7 @@ LL | | };
4848
= note: `(a || b) as i32` or `(a || b).into()` can also be valid options
4949

5050
error: boolean to int conversion using if
51-
--> $DIR/bool_to_int_with_if.rs:35:5
51+
--> $DIR/bool_to_int_with_if.rs:36:5
5252
|
5353
LL | / if cond(a, b) {
5454
LL | | 1
@@ -60,7 +60,7 @@ LL | | };
6060
= note: `cond(a, b) as i32` or `cond(a, b).into()` can also be valid options
6161

6262
error: boolean to int conversion using if
63-
--> $DIR/bool_to_int_with_if.rs:40:5
63+
--> $DIR/bool_to_int_with_if.rs:41:5
6464
|
6565
LL | / if x + y < 4 {
6666
LL | | 1
@@ -72,7 +72,7 @@ LL | | };
7272
= note: `(x + y < 4) as i32` or `(x + y < 4).into()` can also be valid options
7373

7474
error: boolean to int conversion using if
75-
--> $DIR/bool_to_int_with_if.rs:49:12
75+
--> $DIR/bool_to_int_with_if.rs:50:12
7676
|
7777
LL | } else if b {
7878
| ____________^
@@ -85,7 +85,7 @@ LL | | };
8585
= note: `b as i32` or `b.into()` can also be valid options
8686

8787
error: boolean to int conversion using if
88-
--> $DIR/bool_to_int_with_if.rs:58:12
88+
--> $DIR/bool_to_int_with_if.rs:59:12
8989
|
9090
LL | } else if b {
9191
| ____________^
@@ -98,7 +98,7 @@ LL | | };
9898
= note: `!b as i32` or `(!b).into()` can also be valid options
9999

100100
error: boolean to int conversion using if
101-
--> $DIR/bool_to_int_with_if.rs:118:5
101+
--> $DIR/bool_to_int_with_if.rs:119:5
102102
|
103103
LL | if a { 1 } else { 0 }
104104
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`

0 commit comments

Comments
 (0)