]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/match_expr_like_matches_macro.fixed
match_like_matches_macro: strip refs in suggestion
[rust.git] / tests / ui / match_expr_like_matches_macro.fixed
index f3e19092480ad12483c9d6a91ddd36abaaa2ff8e..319299862a7003462cbaffe429edb6235c28f482 100644 (file)
@@ -1,7 +1,7 @@
 // run-rustfix
 
 #![warn(clippy::match_like_matches_macro)]
-#![allow(unreachable_patterns)]
+#![allow(unreachable_patterns, dead_code)]
 
 fn main() {
     let x = Some(5);
@@ -33,4 +33,117 @@ fn main() {
         _ => true,
         None => false,
     };
+
+    enum E {
+        A(u32),
+        B(i32),
+        C,
+        D,
+    }
+    let x = E::A(2);
+    {
+        // lint
+        let _ans = matches!(x, E::A(_) | E::B(_));
+    }
+    {
+        // lint
+        let _ans = !matches!(x, E::B(_) | E::C);
+    }
+    {
+        // no lint
+        let _ans = match x {
+            E::A(_) => false,
+            E::B(_) => false,
+            E::C => true,
+            _ => true,
+        };
+    }
+    {
+        // no lint
+        let _ans = match x {
+            E::A(_) => true,
+            E::B(_) => false,
+            E::C => false,
+            _ => true,
+        };
+    }
+    {
+        // no lint
+        let _ans = match x {
+            E::A(a) if a < 10 => false,
+            E::B(a) if a < 10 => false,
+            _ => true,
+        };
+    }
+    {
+        // no lint
+        let _ans = match x {
+            E::A(_) => false,
+            E::B(a) if a < 10 => false,
+            _ => true,
+        };
+    }
+    {
+        // no lint
+        let _ans = match x {
+            E::A(a) => a == 10,
+            E::B(_) => false,
+            _ => true,
+        };
+    }
+    {
+        // no lint
+        let _ans = match x {
+            E::A(_) => false,
+            E::B(_) => true,
+            _ => false,
+        };
+    }
+
+    {
+        // should print "z" in suggestion (#6503)
+        let z = &Some(3);
+        let _z = matches!(z, Some(3));
+    }
+
+    {
+        // this could also print "z" in suggestion..?
+        let z = Some(3);
+        let _z = matches!(&z, Some(3));
+    }
+
+    {
+        enum AnEnum {
+            X,
+            Y,
+        }
+
+        fn foo(_x: AnEnum) {}
+
+        fn main() {
+            let z = AnEnum::X;
+            // we can't remove the reference here!
+            let _ = matches!(&z, AnEnum::X);
+            foo(z);
+        }
+    }
+
+    {
+        struct S(i32);
+
+        fn fun(_val: Option<S>) {}
+        let val = Some(S(42));
+        // we need the reference here because later val is consumed by fun()
+        let _res = matches!(&val, &Some(ref _a));
+        fun(val);
+    }
+
+    {
+        struct S(i32);
+
+        fn fun(_val: Option<S>) {}
+        let val = Some(S(42));
+        let _res = matches!(&val, &Some(ref _a));
+        fun(val);
+    }
 }