Skip to content

Commit 3b89a67

Browse files
committed
Auto merge of rust-lang#6382 - giraffate:fix_fp_in_manual_range_contains_when_const_fn, r=llogiq
Fix FP of `manual_range_contains` in `const fn` Fix rust-lang#6373. changelog: Fix FP of `manual_range_contains` in `const fn`
2 parents 18c5ea4 + 26c61c7 commit 3b89a67

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

clippy_lints/src/ranges.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::cmp::Ordering;
1414

1515
use crate::utils::sugg::Sugg;
1616
use crate::utils::{
17-
get_parent_expr, is_integer_const, meets_msrv, single_segment_path, snippet, snippet_opt,
17+
get_parent_expr, in_constant, is_integer_const, meets_msrv, single_segment_path, snippet, snippet_opt,
1818
snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then,
1919
};
2020
use crate::utils::{higher, SpanlessEq};
@@ -190,7 +190,7 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
190190
},
191191
ExprKind::Binary(ref op, ref l, ref r) => {
192192
if meets_msrv(self.msrv.as_ref(), &MANUAL_RANGE_CONTAINS_MSRV) {
193-
check_possible_range_contains(cx, op.node, l, r, expr.span);
193+
check_possible_range_contains(cx, op.node, l, r, expr);
194194
}
195195
},
196196
_ => {},
@@ -203,7 +203,12 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
203203
extract_msrv_attr!(LateContext);
204204
}
205205

206-
fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'_>, r: &Expr<'_>, span: Span) {
206+
fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'_>, r: &Expr<'_>, expr: &Expr<'_>) {
207+
if in_constant(cx, expr.hir_id) {
208+
return;
209+
}
210+
211+
let span = expr.span;
207212
let combine_and = match op {
208213
BinOpKind::And | BinOpKind::BitAnd => true,
209214
BinOpKind::Or | BinOpKind::BitOr => false,

tests/ui/range_contains.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ fn main() {
4444
(0. ..1.).contains(&y);
4545
!(0. ..=1.).contains(&y);
4646
}
47+
48+
// Fix #6373
49+
pub const fn in_range(a: i32) -> bool {
50+
3 <= a && a <= 20
51+
}

tests/ui/range_contains.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ fn main() {
4444
y >= 0. && y < 1.;
4545
y < 0. || y > 1.;
4646
}
47+
48+
// Fix #6373
49+
pub const fn in_range(a: i32) -> bool {
50+
3 <= a && a <= 20
51+
}

0 commit comments

Comments
 (0)