about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkraktus <kraktus@users.noreply.github.com>2022-10-28 14:45:16 +0200
committerkraktus <kraktus@users.noreply.github.com>2022-10-28 14:45:51 +0200
commitfa6850d888ce44d3381451aa484743289029dccc (patch)
tree9076ac66addd9c561765379ac096b0eaa9c70034
parent33137dd612cab59026bc5c0469b8761b198e3cae (diff)
downloadrust-fa6850d888ce44d3381451aa484743289029dccc.tar.gz
rust-fa6850d888ce44d3381451aa484743289029dccc.zip
[`bool_to_int_with_if`] do not lint in const context
-rw-r--r--clippy_lints/src/bool_to_int_with_if.rs18
-rw-r--r--tests/ui/bool_to_int_with_if.fixed2
-rw-r--r--tests/ui/bool_to_int_with_if.rs2
-rw-r--r--tests/ui/bool_to_int_with_if.stderr2
4 files changed, 14 insertions, 10 deletions
diff --git a/clippy_lints/src/bool_to_int_with_if.rs b/clippy_lints/src/bool_to_int_with_if.rs
index 001d74c2605..88bdfefddf7 100644
--- a/clippy_lints/src/bool_to_int_with_if.rs
+++ b/clippy_lints/src/bool_to_int_with_if.rs
@@ -3,7 +3,7 @@ use rustc_hir::{Block, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
-use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, is_integer_literal, sugg::Sugg};
+use clippy_utils::{diagnostics::span_lint_and_then, in_constant, is_else_clause, is_integer_literal, sugg::Sugg};
 use rustc_errors::Applicability;
 
 declare_clippy_lint! {
@@ -44,14 +44,14 @@ declare_clippy_lint! {
 declare_lint_pass!(BoolToIntWithIf => [BOOL_TO_INT_WITH_IF]);
 
 impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
-    fn check_expr(&mut self, ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
-        if !expr.span.from_expansion() {
-            check_if_else(ctx, expr);
+    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
+        if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
+            check_if_else(cx, expr);
         }
     }
 }
 
-fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
+fn check_if_else<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
     if let ExprKind::If(check, then, Some(else_)) = expr.kind
         && let Some(then_lit) = int_literal(then)
         && 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
         };
         let mut applicability = Applicability::MachineApplicable;
         let snippet = {
-            let mut sugg = Sugg::hir_with_applicability(ctx, check, "..", &mut applicability);
+            let mut sugg = Sugg::hir_with_applicability(cx, check, "..", &mut applicability);
             if inverted {
                 sugg = !sugg;
             }
             sugg
         };
 
-        let ty = ctx.typeck_results().expr_ty(then_lit); // then and else must be of same type
+        let ty = cx.typeck_results().expr_ty(then_lit); // then and else must be of same type
 
         let suggestion = {
-            let wrap_in_curly = is_else_clause(ctx.tcx, expr);
+            let wrap_in_curly = is_else_clause(cx.tcx, expr);
             let mut s = Sugg::NonParen(format!("{ty}::from({snippet})").into());
             if wrap_in_curly {
                 s = s.blockify();
@@ -87,7 +87,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
         let into_snippet = snippet.clone().maybe_par();
         let as_snippet = snippet.as_ty(ty);
 
-        span_lint_and_then(ctx,
+        span_lint_and_then(cx,
             BOOL_TO_INT_WITH_IF,
             expr.span,
             "boolean to int conversion using if",
diff --git a/tests/ui/bool_to_int_with_if.fixed b/tests/ui/bool_to_int_with_if.fixed
index 2c8339cdd7f..6e0ff053ff2 100644
--- a/tests/ui/bool_to_int_with_if.fixed
+++ b/tests/ui/bool_to_int_with_if.fixed
@@ -76,6 +76,8 @@ fn main() {
         123
     };
 
+    pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
+
     some_fn(a);
 }
 
diff --git a/tests/ui/bool_to_int_with_if.rs b/tests/ui/bool_to_int_with_if.rs
index 5d9496f0177..90ad5aa64da 100644
--- a/tests/ui/bool_to_int_with_if.rs
+++ b/tests/ui/bool_to_int_with_if.rs
@@ -108,6 +108,8 @@ fn main() {
         123
     };
 
+    pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
+
     some_fn(a);
 }
 
diff --git a/tests/ui/bool_to_int_with_if.stderr b/tests/ui/bool_to_int_with_if.stderr
index 4cb5531bef6..1f447c9bfd1 100644
--- a/tests/ui/bool_to_int_with_if.stderr
+++ b/tests/ui/bool_to_int_with_if.stderr
@@ -98,7 +98,7 @@ LL | |     };
    = note: `!b as i32` or `(!b).into()` can also be valid options
 
 error: boolean to int conversion using if
-  --> $DIR/bool_to_int_with_if.rs:116:5
+  --> $DIR/bool_to_int_with_if.rs:118:5
    |
 LL |     if a { 1 } else { 0 }
    |     ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`