about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-03-10 16:57:57 +0000
committerbors <bors@rust-lang.org>2023-03-10 16:57:57 +0000
commit8e1dd06918f405b9c19f8877109fad91d0c43099 (patch)
treefe6e2350eae0f9c377fa510ad2d6e5ff84a6688b
parentb0e2e7bdb4d7441fc8fd4f56795377ab85c82252 (diff)
parent797d8bff0807acbd36d079401fc99dbf042f71e9 (diff)
downloadrust-8e1dd06918f405b9c19f8877109fad91d0c43099.tar.gz
rust-8e1dd06918f405b9c19f8877109fad91d0c43099.zip
Auto merge of #10479 - Jarcho:issue_10474, r=flip1995
Don't lint `manual_clamp` in const contexts.

fixes #10474

Probably worth including in the sync.
r? `@flip1995`

changelog: [`manual_clamp`]: Don't lint in const contexts.
-rw-r--r--clippy_lints/src/manual_clamp.rs7
-rw-r--r--tests/ui/manual_clamp.rs19
2 files changed, 23 insertions, 3 deletions
diff --git a/clippy_lints/src/manual_clamp.rs b/clippy_lints/src/manual_clamp.rs
index f239736d38a..440362b96b4 100644
--- a/clippy_lints/src/manual_clamp.rs
+++ b/clippy_lints/src/manual_clamp.rs
@@ -6,7 +6,8 @@ use clippy_utils::ty::implements_trait;
 use clippy_utils::visitors::is_const_evaluatable;
 use clippy_utils::MaybePath;
 use clippy_utils::{
-    eq_expr_value, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
+    eq_expr_value, in_constant, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks,
+    peel_blocks_with_stmt,
 };
 use itertools::Itertools;
 use rustc_errors::Applicability;
@@ -117,7 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp {
         if !self.msrv.meets(msrvs::CLAMP) {
             return;
         }
-        if !expr.span.from_expansion() {
+        if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
             let suggestion = is_if_elseif_else_pattern(cx, expr)
                 .or_else(|| is_max_min_pattern(cx, expr))
                 .or_else(|| is_call_max_min_pattern(cx, expr))
@@ -130,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp {
     }
 
     fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
-        if !self.msrv.meets(msrvs::CLAMP) {
+        if !self.msrv.meets(msrvs::CLAMP) || in_constant(cx, block.hir_id) {
             return;
         }
         for suggestion in is_two_if_pattern(cx, block) {
diff --git a/tests/ui/manual_clamp.rs b/tests/ui/manual_clamp.rs
index f7902e6fd53..cdfd8e4c3fe 100644
--- a/tests/ui/manual_clamp.rs
+++ b/tests/ui/manual_clamp.rs
@@ -326,3 +326,22 @@ fn msrv_1_50() {
         input
     };
 }
+
+const fn _const() {
+    let (input, min, max) = (0, -1, 2);
+    let _ = if input < min {
+        min
+    } else if input > max {
+        max
+    } else {
+        input
+    };
+
+    let mut x = input;
+    if max < x {
+        let x = max;
+    }
+    if min > x {
+        x = min;
+    }
+}