about summary refs log tree commit diff
diff options
context:
space:
mode:
authortamaron <tamaron1203@gmail.com>2022-08-11 12:00:46 +0900
committertamaron <tamaron1203@gmail.com>2022-08-11 15:07:39 +0900
commit45084eeefb4edb981ce437c7fee6a07e61ff224b (patch)
tree407d832b48e0e30ab9087885ca0b80c21d82071f
parent459821b1914248180cac691128dd35d945bfde88 (diff)
downloadrust-45084eeefb4edb981ce437c7fee6a07e61ff224b.tar.gz
rust-45084eeefb4edb981ce437c7fee6a07e61ff224b.zip
give up when gurad has side effects
-rw-r--r--clippy_lints/src/matches/needless_match.rs18
-rw-r--r--tests/ui/needless_match.fixed15
-rw-r--r--tests/ui/needless_match.rs15
3 files changed, 45 insertions, 3 deletions
diff --git a/clippy_lints/src/matches/needless_match.rs b/clippy_lints/src/matches/needless_match.rs
index 53a4a91d0e7..6f037339ec7 100644
--- a/clippy_lints/src/matches/needless_match.rs
+++ b/clippy_lints/src/matches/needless_match.rs
@@ -8,7 +8,7 @@ use clippy_utils::{
 };
 use rustc_errors::Applicability;
 use rustc_hir::LangItem::OptionNone;
-use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, FnRetTy, Node, Pat, PatKind, Path, QPath};
+use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, FnRetTy, Guard, Node, Pat, PatKind, Path, QPath};
 use rustc_lint::LateContext;
 use rustc_span::sym;
 use rustc_typeck::hir_ty_to_ty;
@@ -65,6 +65,22 @@ pub(crate) fn check_if_let<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'_>, if_let:
 fn check_all_arms(cx: &LateContext<'_>, match_expr: &Expr<'_>, arms: &[Arm<'_>]) -> bool {
     for arm in arms {
         let arm_expr = peel_blocks_with_stmt(arm.body);
+
+        if let Some(guard_expr) = &arm.guard {
+            match guard_expr {
+                // gives up if `pat if expr` can have side effects
+                Guard::If(if_cond) => {
+                    if if_cond.can_have_side_effects() {
+                        return false;
+                    }
+                },
+                // gives up `pat if let ...` arm
+                Guard::IfLet(_) => {
+                    return false;
+                },
+            };
+        }
+
         if let PatKind::Wild = arm.pat.kind {
             if !eq_expr_value(cx, match_expr, strip_return(arm_expr)) {
                 return false;
diff --git a/tests/ui/needless_match.fixed b/tests/ui/needless_match.fixed
index 9d4427f1df2..7e47406798c 100644
--- a/tests/ui/needless_match.fixed
+++ b/tests/ui/needless_match.fixed
@@ -209,7 +209,7 @@ impl Tr for Result<i32, i32> {
 
 mod issue9084 {
     fn wildcard_if() {
-        let some_bool = true;
+        let mut some_bool = true;
         let e = Some(1);
 
         // should lint
@@ -230,6 +230,19 @@ mod issue9084 {
             _ if some_bool => e,
             _ => e,
         };
+
+        // should not lint (guard has side effects)
+        let _ = match e {
+            Some(i) => Some(i),
+            _ if {
+                some_bool = false;
+                some_bool
+            } =>
+            {
+                e
+            },
+            _ => e,
+        };
     }
 }
 
diff --git a/tests/ui/needless_match.rs b/tests/ui/needless_match.rs
index cae850fb059..809c694bf40 100644
--- a/tests/ui/needless_match.rs
+++ b/tests/ui/needless_match.rs
@@ -246,7 +246,7 @@ impl Tr for Result<i32, i32> {
 
 mod issue9084 {
     fn wildcard_if() {
-        let some_bool = true;
+        let mut some_bool = true;
         let e = Some(1);
 
         // should lint
@@ -274,6 +274,19 @@ mod issue9084 {
             _ if some_bool => e,
             _ => e,
         };
+
+        // should not lint (guard has side effects)
+        let _ = match e {
+            Some(i) => Some(i),
+            _ if {
+                some_bool = false;
+                some_bool
+            } =>
+            {
+                e
+            },
+            _ => e,
+        };
     }
 }