]> git.lizzy.rs Git - rust.git/commitdiff
Don't lint `manual_map` in const functions
authorJason Newcomb <jsnewcomb@pm.me>
Fri, 26 Mar 2021 02:48:27 +0000 (22:48 -0400)
committerJason Newcomb <jsnewcomb@pm.me>
Tue, 30 Mar 2021 14:56:08 +0000 (10:56 -0400)
clippy_lints/src/manual_map.rs
tests/ui/manual_map_option.fixed
tests/ui/manual_map_option.rs

index d6ef3aa1e778961bcc3899740b87538495315490..8c9e3af62f482568fc5382df4e9b6878410ee94f 100644 (file)
@@ -2,7 +2,7 @@
 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::{
 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)
index 5e26958041d383dd925b643c8ffed0d8fbb8c902..ee0158457778673751f097c518d285560900de7a 100644 (file)
@@ -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,
+        };
+    }
 }
index 33eb81561050036d62459426bcdfe3a5ecf22149..29509bddfd94d260a10dc919a24d01273d521577 100644 (file)
@@ -204,4 +204,12 @@ async fn f3() {
     } else {
         None
     };
+
+    // #6967
+    const fn f4() {
+        match Some(0) {
+            Some(x) => Some(x + 1),
+            None => None,
+        };
+    }
 }