]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/wildcard_enum_match_arm.rs
Rollup merge of #92849 - flip1995:clippyup, r=Manishearth
[rust.git] / tests / ui / wildcard_enum_match_arm.rs
index 86d4c7f28c4e46fe0317d9ce991a2ff64abd6058..46886550453308a340353c5a8c9c8b143a0877a9 100644 (file)
-#![warn(clippy::wildcard_enum_match_arm)]
+// run-rustfix
+// aux-build:non-exhaustive-enum.rs
 
-#[derive(Debug)]
-enum Maybe<T> {
-    Some(T),
-    Probably(T),
-    None,
-}
+#![deny(clippy::wildcard_enum_match_arm)]
+#![allow(
+    unreachable_code,
+    unused_variables,
+    dead_code,
+    clippy::single_match,
+    clippy::wildcard_in_or_patterns,
+    clippy::unnested_or_patterns,
+    clippy::diverging_sub_expression
+)]
 
-fn is_it_wildcard<T>(m: Maybe<T>) -> &'static str {
-    match m {
-        Maybe::Some(_) => "Some",
-        _ => "Could be",
-    }
-}
+extern crate non_exhaustive_enum;
 
-fn is_it_bound<T>(m: Maybe<T>) -> &'static str {
-    match m {
-        Maybe::None => "None",
-        _other => "Could be",
-    }
-}
+use non_exhaustive_enum::ErrorKind;
 
-fn is_it_binding(m: Maybe<u32>) -> String {
-    match m {
-        Maybe::Some(v) => "Large".to_string(),
-        n => format!("{:?}", n),
-    }
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+enum Color {
+    Red,
+    Green,
+    Blue,
+    Rgb(u8, u8, u8),
+    Cyan,
 }
 
-fn is_it_binding_exhaustive(m: Maybe<u32>) -> String {
-    match m {
-        Maybe::Some(v) => "Large".to_string(),
-        n @ Maybe::Probably(_) | n @ Maybe::None => format!("{:?}", n),
+impl Color {
+    fn is_monochrome(self) -> bool {
+        match self {
+            Color::Red | Color::Green | Color::Blue => true,
+            Color::Rgb(r, g, b) => r | g == 0 || r | b == 0 || g | b == 0,
+            Color::Cyan => false,
+        }
     }
 }
 
-fn is_it_with_guard(m: Maybe<u32>) -> &'static str {
-    match m {
-        Maybe::Some(v) if v > 100 => "Large",
-        _ => "Who knows",
+fn main() {
+    let color = Color::Rgb(0, 0, 127);
+    match color {
+        Color::Red => println!("Red"),
+        _ => eprintln!("Not red"),
+    };
+    match color {
+        Color::Red => println!("Red"),
+        _not_red => eprintln!("Not red"),
+    };
+    let _str = match color {
+        Color::Red => "Red".to_owned(),
+        not_red => format!("{:?}", not_red),
+    };
+    match color {
+        Color::Red => {},
+        Color::Green => {},
+        Color::Blue => {},
+        Color::Cyan => {},
+        c if c.is_monochrome() => {},
+        Color::Rgb(_, _, _) => {},
+    };
+    let _str = match color {
+        Color::Red => "Red",
+        c @ Color::Green | c @ Color::Blue | c @ Color::Rgb(_, _, _) | c @ Color::Cyan => "Not red",
+    };
+    match color {
+        Color::Rgb(r, _, _) if r > 0 => "Some red",
+        _ => "No red",
+    };
+    match color {
+        Color::Red | Color::Green | Color::Blue | Color::Cyan => {},
+        Color::Rgb(..) => {},
+    };
+    let x: u8 = unimplemented!();
+    match x {
+        0 => {},
+        140 => {},
+        _ => {},
+    };
+    // We need to use an enum not defined in this test because non_exhaustive is ignored for the
+    // purposes of dead code analysis within a crate.
+    let error_kind = ErrorKind::NotFound;
+    match error_kind {
+        ErrorKind::NotFound => {},
+        _ => {},
     }
-}
-
-fn is_it_exhaustive<T>(m: Maybe<T>) -> &'static str {
-    match m {
-        Maybe::None => "None",
-        Maybe::Some(_) | Maybe::Probably(..) => "Could be",
+    match error_kind {
+        ErrorKind::NotFound => {},
+        ErrorKind::PermissionDenied => {},
+        _ => {},
     }
-}
 
-fn is_one_or_three(i: i32) -> bool {
-    match i {
-        1 | 3 => true,
-        _ => false,
+    {
+        #![allow(clippy::manual_non_exhaustive)]
+        pub enum Enum {
+            A,
+            B,
+            #[doc(hidden)]
+            __Private,
+        }
+        match Enum::A {
+            Enum::A => (),
+            _ => (),
+        }
     }
 }
-
-fn main() {
-    println!("{}", is_it_wildcard(Maybe::Some("foo")));
-
-    println!("{}", is_it_bound(Maybe::Some("foo")));
-
-    println!("{}", is_it_binding(Maybe::Some(1)));
-
-    println!("{}", is_it_binding_exhaustive(Maybe::Some(1)));
-
-    println!("{}", is_it_with_guard(Maybe::Some(1)));
-
-    println!("{}", is_it_exhaustive(Maybe::Some("foo")));
-
-    println!("{}", is_one_or_three(2));
-}