Skip to content

Commit 8e19251

Browse files
committed
Auto merge of #9738 - kraktus:bool_to_int_with_if, r=xFrednet
[`bool_to_int_with_if`] do not lint in const context changelog: [`bool_to_int_with_if`] do not lint in const context fix #9737
2 parents fdaa425 + fa6850d commit 8e19251

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::{Block, ExprKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55

6-
use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, is_integer_literal, sugg::Sugg};
6+
use clippy_utils::{diagnostics::span_lint_and_then, in_constant, is_else_clause, is_integer_literal, sugg::Sugg};
77
use rustc_errors::Applicability;
88

99
declare_clippy_lint! {
@@ -44,14 +44,14 @@ declare_clippy_lint! {
4444
declare_lint_pass!(BoolToIntWithIf => [BOOL_TO_INT_WITH_IF]);
4545

4646
impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
47-
fn check_expr(&mut self, ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
48-
if !expr.span.from_expansion() {
49-
check_if_else(ctx, expr);
47+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
48+
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
49+
check_if_else(cx, expr);
5050
}
5151
}
5252
}
5353

54-
fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
54+
fn check_if_else<'tcx>(cx: &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_)
@@ -66,17 +66,17 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
6666
};
6767
let mut applicability = Applicability::MachineApplicable;
6868
let snippet = {
69-
let mut sugg = Sugg::hir_with_applicability(ctx, check, "..", &mut applicability);
69+
let mut sugg = Sugg::hir_with_applicability(cx, check, "..", &mut applicability);
7070
if inverted {
7171
sugg = !sugg;
7272
}
7373
sugg
7474
};
7575

76-
let ty = ctx.typeck_results().expr_ty(then_lit); // then and else must be of same type
76+
let ty = cx.typeck_results().expr_ty(then_lit); // then and else must be of same type
7777

7878
let suggestion = {
79-
let wrap_in_curly = is_else_clause(ctx.tcx, expr);
79+
let wrap_in_curly = is_else_clause(cx.tcx, expr);
8080
let mut s = Sugg::NonParen(format!("{ty}::from({snippet})").into());
8181
if wrap_in_curly {
8282
s = s.blockify();
@@ -87,7 +87,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
8787
let into_snippet = snippet.clone().maybe_par();
8888
let as_snippet = snippet.as_ty(ty);
8989

90-
span_lint_and_then(ctx,
90+
span_lint_and_then(cx,
9191
BOOL_TO_INT_WITH_IF,
9292
expr.span,
9393
"boolean to int conversion using if",

tests/ui/bool_to_int_with_if.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ fn main() {
7676
123
7777
};
7878

79+
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
80+
7981
some_fn(a);
8082
}
8183

tests/ui/bool_to_int_with_if.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ fn main() {
108108
123
109109
};
110110

111+
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
112+
111113
some_fn(a);
112114
}
113115

tests/ui/bool_to_int_with_if.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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:116:5
101+
--> $DIR/bool_to_int_with_if.rs:118:5
102102
|
103103
LL | if a { 1 } else { 0 }
104104
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`

0 commit comments

Comments
 (0)