about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2021-03-25 22:48:27 -0400
committerJason Newcomb <jsnewcomb@pm.me>2021-03-30 10:56:08 -0400
commitcc7f1daab2e8104d8ae5952e28f22dcac920d244 (patch)
treef536d38a4671ad2b85da9498a053850224c02728
parent44bf60f62d0036c12d7e2c8ecddf6fced973663f (diff)
downloadrust-cc7f1daab2e8104d8ae5952e28f22dcac920d244.tar.gz
rust-cc7f1daab2e8104d8ae5952e28f22dcac920d244.zip
Don't lint `manual_map` in const functions
-rw-r--r--clippy_lints/src/manual_map.rs10
-rw-r--r--tests/ui/manual_map_option.fixed8
-rw-r--r--tests/ui/manual_map_option.rs8
3 files changed, 21 insertions, 5 deletions
diff --git a/clippy_lints/src/manual_map.rs b/clippy_lints/src/manual_map.rs
index d6ef3aa1e77..8c9e3af62f4 100644
--- a/clippy_lints/src/manual_map.rs
+++ b/clippy_lints/src/manual_map.rs
@@ -2,7 +2,7 @@ use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF};
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
 use clippy_utils::ty::{can_partially_move_ty, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable};
-use clippy_utils::{is_allowed, is_else_clause, match_def_path, match_var, paths, peel_hir_expr_refs};
+use clippy_utils::{in_constant, is_allowed, is_else_clause, match_def_path, match_var, paths, peel_hir_expr_refs};
 use rustc_ast::util::parser::PREC_POSTFIX;
 use rustc_errors::Applicability;
 use rustc_hir::{
@@ -47,16 +47,16 @@ declare_lint_pass!(ManualMap => [MANUAL_MAP]);
 impl LateLintPass<'_> for ManualMap {
     #[allow(clippy::too_many_lines)]
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
-        if in_external_macro(cx.sess(), expr.span) {
-            return;
-        }
-
         if let ExprKind::Match(
             scrutinee,
             [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }],
             match_kind,
         ) = expr.kind
         {
+            if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) {
+                return;
+            }
+
             let (scrutinee_ty, ty_ref_count, ty_mutability) =
                 peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
             if !(is_type_diagnostic_item(cx, scrutinee_ty, sym::option_type)
diff --git a/tests/ui/manual_map_option.fixed b/tests/ui/manual_map_option.fixed
index 5e26958041d..ee015845777 100644
--- a/tests/ui/manual_map_option.fixed
+++ b/tests/ui/manual_map_option.fixed
@@ -138,4 +138,12 @@ fn main() {
     if true {
         Some(0)
     } else { Some(0).map(|x| x + 1) };
+
+    // #6967
+    const fn f4() {
+        match Some(0) {
+            Some(x) => Some(x + 1),
+            None => None,
+        };
+    }
 }
diff --git a/tests/ui/manual_map_option.rs b/tests/ui/manual_map_option.rs
index 33eb8156105..29509bddfd9 100644
--- a/tests/ui/manual_map_option.rs
+++ b/tests/ui/manual_map_option.rs
@@ -204,4 +204,12 @@ fn main() {
     } else {
         None
     };
+
+    // #6967
+    const fn f4() {
+        match Some(0) {
+            Some(x) => Some(x + 1),
+            None => None,
+        };
+    }
 }