Skip to content

Commit 8e1dd06

Browse files
committed
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.
2 parents b0e2e7b + 797d8bf commit 8e1dd06

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Diff for: clippy_lints/src/manual_clamp.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use clippy_utils::ty::implements_trait;
66
use clippy_utils::visitors::is_const_evaluatable;
77
use clippy_utils::MaybePath;
88
use clippy_utils::{
9-
eq_expr_value, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
9+
eq_expr_value, in_constant, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks,
10+
peel_blocks_with_stmt,
1011
};
1112
use itertools::Itertools;
1213
use rustc_errors::Applicability;
@@ -117,7 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp {
117118
if !self.msrv.meets(msrvs::CLAMP) {
118119
return;
119120
}
120-
if !expr.span.from_expansion() {
121+
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
121122
let suggestion = is_if_elseif_else_pattern(cx, expr)
122123
.or_else(|| is_max_min_pattern(cx, expr))
123124
.or_else(|| is_call_max_min_pattern(cx, expr))
@@ -130,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp {
130131
}
131132

132133
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
133-
if !self.msrv.meets(msrvs::CLAMP) {
134+
if !self.msrv.meets(msrvs::CLAMP) || in_constant(cx, block.hir_id) {
134135
return;
135136
}
136137
for suggestion in is_two_if_pattern(cx, block) {

Diff for: tests/ui/manual_clamp.rs

+19
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,22 @@ fn msrv_1_50() {
326326
input
327327
};
328328
}
329+
330+
const fn _const() {
331+
let (input, min, max) = (0, -1, 2);
332+
let _ = if input < min {
333+
min
334+
} else if input > max {
335+
max
336+
} else {
337+
input
338+
};
339+
340+
let mut x = input;
341+
if max < x {
342+
let x = max;
343+
}
344+
if min > x {
345+
x = min;
346+
}
347+
}

0 commit comments

Comments
 (0)