Skip to content

Commit

Permalink
Auto merge of #10479 - Jarcho:issue_10474, r=flip1995
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Mar 10, 2023
2 parents b0e2e7b + 797d8bf commit 8e1dd06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions clippy_lints/src/manual_clamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/manual_clamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 8e1dd06

Please sign in to comment.