]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/match_expr_like_matches_macro.rs
Move MSRV tests into the lint specific test files
[rust.git] / tests / ui / match_expr_like_matches_macro.rs
index fbae7c18b92392f2bf494a59d3efcb684983962a..b4e48499bd0fb193fca123c306f129d24543e904 100644 (file)
@@ -1,7 +1,8 @@
 // run-rustfix
 
+#![feature(custom_inner_attributes)]
 #![warn(clippy::match_like_matches_macro)]
-#![allow(unreachable_patterns)]
+#![allow(unreachable_patterns, dead_code, clippy::equatable_if_let)]
 
 fn main() {
     let x = Some(5);
@@ -45,4 +46,210 @@ fn main() {
         _ => true,
         None => false,
     };
+
+    enum E {
+        A(u32),
+        B(i32),
+        C,
+        D,
+    }
+    let x = E::A(2);
+    {
+        // lint
+        let _ans = match x {
+            E::A(_) => true,
+            E::B(_) => true,
+            _ => false,
+        };
+    }
+    {
+        // lint
+        // skip rustfmt to prevent removing block for first pattern
+        #[rustfmt::skip]
+        let _ans = match x {
+            E::A(_) => {
+                true
+            }
+            E::B(_) => true,
+            _ => false,
+        };
+    }
+    {
+        // lint
+        let _ans = match x {
+            E::B(_) => false,
+            E::C => false,
+            _ => true,
+        };
+    }
+    {
+        // 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 = match &z {
+            Some(3) => true,
+            _ => false,
+        };
+    }
+
+    {
+        // this could also print "z" in suggestion..?
+        let z = Some(3);
+        let _z = match &z {
+            Some(3) => true,
+            _ => false,
+        };
+    }
+
+    {
+        enum AnEnum {
+            X,
+            Y,
+        }
+
+        fn foo(_x: AnEnum) {}
+
+        fn main() {
+            let z = AnEnum::X;
+            // we can't remove the reference here!
+            let _ = match &z {
+                AnEnum::X => true,
+                _ => false,
+            };
+            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 = match &val {
+            &Some(ref _a) => true,
+            _ => false,
+        };
+        fun(val);
+    }
+
+    {
+        struct S(i32);
+
+        fn fun(_val: Option<S>) {}
+        let val = Some(S(42));
+        let _res = match &val {
+            &Some(ref _a) => true,
+            _ => false,
+        };
+        fun(val);
+    }
+
+    {
+        enum E {
+            A,
+            B,
+            C,
+        }
+
+        let _ = match E::A {
+            E::B => true,
+            #[cfg(feature = "foo")]
+            E::A => true,
+            _ => false,
+        };
+    }
+
+    let x = ' ';
+    // ignore if match block contains comment
+    let _line_comments = match x {
+        // numbers are bad!
+        '1' | '2' | '3' => true,
+        // spaces are very important to be true.
+        ' ' => true,
+        // as are dots
+        '.' => true,
+        _ => false,
+    };
+
+    let _block_comments = match x {
+        /* numbers are bad!
+         */
+        '1' | '2' | '3' => true,
+        /* spaces are very important to be true.
+         */
+        ' ' => true,
+        /* as are dots
+         */
+        '.' => true,
+        _ => false,
+    };
+}
+
+fn msrv_1_41() {
+    #![clippy::msrv = "1.41"]
+
+    let _y = match Some(5) {
+        Some(0) => true,
+        _ => false,
+    };
+}
+
+fn msrv_1_42() {
+    #![clippy::msrv = "1.42"]
+
+    let _y = match Some(5) {
+        Some(0) => true,
+        _ => false,
+    };
 }