about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Nakata <f.seasons017@gmail.com>2020-11-25 17:07:50 +0900
committerTakayuki Nakata <f.seasons017@gmail.com>2020-12-11 23:07:52 +0900
commit26c61c7e49b173c8ae7c54c3a4c90b60cd9f71b8 (patch)
treec19a27689d1f9de372bd2142c97f67b59b68e240
parent27fd6ed58145a6cf9789ef82deb3be8182c92915 (diff)
downloadrust-26c61c7e49b173c8ae7c54c3a4c90b60cd9f71b8.tar.gz
rust-26c61c7e49b173c8ae7c54c3a4c90b60cd9f71b8.zip
Fix FP of `manual_range_contains` in `const fn`
-rw-r--r--clippy_lints/src/ranges.rs11
-rw-r--r--tests/ui/range_contains.fixed5
-rw-r--r--tests/ui/range_contains.rs5
3 files changed, 18 insertions, 3 deletions
diff --git a/clippy_lints/src/ranges.rs b/clippy_lints/src/ranges.rs
index f9173808089..3e454eecd97 100644
--- a/clippy_lints/src/ranges.rs
+++ b/clippy_lints/src/ranges.rs
@@ -14,7 +14,7 @@ use std::cmp::Ordering;
 
 use crate::utils::sugg::Sugg;
 use crate::utils::{
-    get_parent_expr, is_integer_const, meets_msrv, single_segment_path, snippet, snippet_opt,
+    get_parent_expr, in_constant, is_integer_const, meets_msrv, single_segment_path, snippet, snippet_opt,
     snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then,
 };
 use crate::utils::{higher, SpanlessEq};
@@ -190,7 +190,7 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
             },
             ExprKind::Binary(ref op, ref l, ref r) => {
                 if meets_msrv(self.msrv.as_ref(), &MANUAL_RANGE_CONTAINS_MSRV) {
-                    check_possible_range_contains(cx, op.node, l, r, expr.span);
+                    check_possible_range_contains(cx, op.node, l, r, expr);
                 }
             },
             _ => {},
@@ -203,7 +203,12 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
     extract_msrv_attr!(LateContext);
 }
 
-fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'_>, r: &Expr<'_>, span: Span) {
+fn check_possible_range_contains(cx: &LateContext<'_>, op: BinOpKind, l: &Expr<'_>, r: &Expr<'_>, expr: &Expr<'_>) {
+    if in_constant(cx, expr.hir_id) {
+        return;
+    }
+
+    let span = expr.span;
     let combine_and = match op {
         BinOpKind::And | BinOpKind::BitAnd => true,
         BinOpKind::Or | BinOpKind::BitOr => false,
diff --git a/tests/ui/range_contains.fixed b/tests/ui/range_contains.fixed
index 048874a7f82..47c974e614b 100644
--- a/tests/ui/range_contains.fixed
+++ b/tests/ui/range_contains.fixed
@@ -44,3 +44,8 @@ fn main() {
     (0. ..1.).contains(&y);
     !(0. ..=1.).contains(&y);
 }
+
+// Fix #6373
+pub const fn in_range(a: i32) -> bool {
+    3 <= a && a <= 20
+}
diff --git a/tests/ui/range_contains.rs b/tests/ui/range_contains.rs
index 60ad259f404..835deced5e4 100644
--- a/tests/ui/range_contains.rs
+++ b/tests/ui/range_contains.rs
@@ -44,3 +44,8 @@ fn main() {
     y >= 0. && y < 1.;
     y < 0. || y > 1.;
 }
+
+// Fix #6373
+pub const fn in_range(a: i32) -> bool {
+    3 <= a && a <= 20
+}