Skip to content

Commit 1eb4180

Browse files
committed
bool_to_int_with_if inverse case patch
1 parent 2ddbc86 commit 1eb4180

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,43 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
5555
if let ExprKind::If(check, then, Some(else_)) = expr.kind
5656
&& let Some(then_lit) = int_literal(then)
5757
&& let Some(else_lit) = int_literal(else_)
58-
&& check_int_literal_equals_val(then_lit, 1)
59-
&& check_int_literal_equals_val(else_lit, 0)
6058
{
59+
let inverted = if
60+
check_int_literal_equals_val(then_lit, 1)
61+
&& check_int_literal_equals_val(else_lit, 0) {
62+
false
63+
} else if
64+
check_int_literal_equals_val(then_lit, 0)
65+
&& check_int_literal_equals_val(else_lit, 1) {
66+
true
67+
} else {
68+
// Expression isn't boolean, exit
69+
return;
70+
};
6171
let mut applicability = Applicability::MachineApplicable;
6272
let snippet = snippet_block_with_applicability(ctx, check.span, "..", None, &mut applicability);
73+
74+
let invert = if inverted { "!" } else { "" };
75+
let need_parens = should_have_parentheses(check);
76+
6377
let snippet_with_braces = {
64-
let need_parens = should_have_parentheses(check);
6578
let (left_paren, right_paren) = if need_parens {("(", ")")} else {("", "")};
66-
format!("{left_paren}{snippet}{right_paren}")
79+
format!("{invert}{left_paren}{snippet}{right_paren}")
6780
};
6881

6982
let ty = ctx.typeck_results().expr_ty(then_lit); // then and else must be of same type
7083

7184
let suggestion = {
7285
let wrap_in_curly = is_else_clause(ctx.tcx, expr);
7386
let (left_curly, right_curly) = if wrap_in_curly {("{", "}")} else {("", "")};
87+
let (left_paren, right_paren) = if inverted && need_parens {("(", ")")} else {("", "")};
7488
format!(
75-
"{left_curly}{ty}::from({snippet}){right_curly}"
89+
"{left_curly}{ty}::from({invert}{left_paren}{snippet}{right_paren}){right_curly}"
7690
)
7791
}; // when used in else clause if statement should be wrapped in curly braces
7892

93+
let (inverted_left_paren, inverted_right_paren) = if inverted {("(", ")")} else {("", "")};
94+
7995
span_lint_and_then(ctx,
8096
BOOL_TO_INT_WITH_IF,
8197
expr.span,
@@ -87,7 +103,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
87103
suggestion,
88104
applicability,
89105
);
90-
diag.note(format!("`{snippet_with_braces} as {ty}` or `{snippet_with_braces}.into()` can also be valid options"));
106+
diag.note(format!("`{snippet_with_braces} as {ty}` or `{inverted_left_paren}{snippet_with_braces}{inverted_right_paren}.into()` can also be valid options"));
91107
});
92108
};
93109
}

tests/ui/bool_to_int_with_if.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
// precedence
1515
i32::from(a);
1616
i32::from(!a);
17+
i32::from(!a);
1718
i32::from(a || b);
1819
i32::from(cond(a, b));
1920
i32::from(x + y < 4);
@@ -23,6 +24,11 @@ fn main() {
2324
123
2425
} else {i32::from(b)};
2526

27+
// if else if inverted
28+
if a {
29+
123
30+
} else {i32::from(!b)};
31+
2632
// Shouldn't lint
2733

2834
if a {

tests/ui/bool_to_int_with_if.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ fn main() {
1717
} else {
1818
0
1919
};
20+
if a {
21+
0
22+
} else {
23+
1
24+
};
2025
if !a {
2126
1
2227
} else {
@@ -47,6 +52,15 @@ fn main() {
4752
0
4853
};
4954

55+
// if else if inverted
56+
if a {
57+
123
58+
} else if b {
59+
0
60+
} else {
61+
1
62+
};
63+
5064
// Shouldn't lint
5165

5266
if a {

tests/ui/bool_to_int_with_if.stderr

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ LL | | };
1414
error: boolean to int conversion using if
1515
--> $DIR/bool_to_int_with_if.rs:20:5
1616
|
17+
LL | / if a {
18+
LL | | 0
19+
LL | | } else {
20+
LL | | 1
21+
LL | | };
22+
| |_____^ help: replace with from: `i32::from(!a)`
23+
|
24+
= note: `!a as i32` or `(!a).into()` can also be valid options
25+
26+
error: boolean to int conversion using if
27+
--> $DIR/bool_to_int_with_if.rs:25:5
28+
|
1729
LL | / if !a {
1830
LL | | 1
1931
LL | | } else {
@@ -24,7 +36,7 @@ LL | | };
2436
= note: `!a as i32` or `!a.into()` can also be valid options
2537

2638
error: boolean to int conversion using if
27-
--> $DIR/bool_to_int_with_if.rs:25:5
39+
--> $DIR/bool_to_int_with_if.rs:30:5
2840
|
2941
LL | / if a || b {
3042
LL | | 1
@@ -36,7 +48,7 @@ LL | | };
3648
= note: `(a || b) as i32` or `(a || b).into()` can also be valid options
3749

3850
error: boolean to int conversion using if
39-
--> $DIR/bool_to_int_with_if.rs:30:5
51+
--> $DIR/bool_to_int_with_if.rs:35:5
4052
|
4153
LL | / if cond(a, b) {
4254
LL | | 1
@@ -48,7 +60,7 @@ LL | | };
4860
= note: `cond(a, b) as i32` or `cond(a, b).into()` can also be valid options
4961

5062
error: boolean to int conversion using if
51-
--> $DIR/bool_to_int_with_if.rs:35:5
63+
--> $DIR/bool_to_int_with_if.rs:40:5
5264
|
5365
LL | / if x + y < 4 {
5466
LL | | 1
@@ -60,7 +72,7 @@ LL | | };
6072
= note: `(x + y < 4) as i32` or `(x + y < 4).into()` can also be valid options
6173

6274
error: boolean to int conversion using if
63-
--> $DIR/bool_to_int_with_if.rs:44:12
75+
--> $DIR/bool_to_int_with_if.rs:49:12
6476
|
6577
LL | } else if b {
6678
| ____________^
@@ -73,12 +85,25 @@ LL | | };
7385
= note: `b as i32` or `b.into()` can also be valid options
7486

7587
error: boolean to int conversion using if
76-
--> $DIR/bool_to_int_with_if.rs:102:5
88+
--> $DIR/bool_to_int_with_if.rs:58:12
89+
|
90+
LL | } else if b {
91+
| ____________^
92+
LL | | 0
93+
LL | | } else {
94+
LL | | 1
95+
LL | | };
96+
| |_____^ help: replace with from: `{i32::from(!b)}`
97+
|
98+
= note: `!b as i32` or `(!b).into()` can also be valid options
99+
100+
error: boolean to int conversion using if
101+
--> $DIR/bool_to_int_with_if.rs:116:5
77102
|
78103
LL | if a { 1 } else { 0 }
79104
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`
80105
|
81106
= note: `a as u8` or `a.into()` can also be valid options
82107

83-
error: aborting due to 7 previous errors
108+
error: aborting due to 9 previous errors
84109

0 commit comments

Comments
 (0)