]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/manual_unwrap_or.rs
Fix type checks for `manual_str_repeat`
[rust.git] / tests / ui / manual_unwrap_or.rs
index beca1de0ed1651861e9107fdfed16a9cc23bea8f..989adde1f5bbb7518e8c41a22e24df5fa0cbe82b 100644 (file)
@@ -1,5 +1,6 @@
 // run-rustfix
 #![allow(dead_code)]
+#![allow(unused_variables, clippy::unnecessary_wraps)]
 
 fn option_unwrap_or() {
     // int case
@@ -82,26 +83,52 @@ fn option_unwrap_or() {
 
 fn result_unwrap_or() {
     // int case
+    match Ok::<i32, &str>(1) {
+        Ok(i) => i,
+        Err(_) => 42,
+    };
+
+    // int case, scrutinee is a binding
+    let a = Ok::<i32, &str>(1);
+    match a {
+        Ok(i) => i,
+        Err(_) => 42,
+    };
+
+    // int case, suggestion must surround Result expr with parenthesis
     match Ok(1) as Result<i32, &str> {
         Ok(i) => i,
         Err(_) => 42,
     };
 
+    // method call case, suggestion must not surround Result expr `s.method()` with parenthesis
+    struct S {}
+    impl S {
+        fn method(self) -> Option<i32> {
+            Some(42)
+        }
+    }
+    let s = S {};
+    match s.method() {
+        Some(i) => i,
+        None => 42,
+    };
+
     // int case reversed
-    match Ok(1) as Result<i32, &str> {
+    match Ok::<i32, &str>(1) {
         Err(_) => 42,
         Ok(i) => i,
     };
 
     // richer none expr
-    match Ok(1) as Result<i32, &str> {
+    match Ok::<i32, &str>(1) {
         Ok(i) => i,
         Err(_) => 1 + 42,
     };
 
     // multiline case
     #[rustfmt::skip]
-    match Ok(1) as Result<i32, &str> {
+    match Ok::<i32, &str>(1) {
         Ok(i) => i,
         Err(_) => {
             42 + 42
@@ -111,30 +138,86 @@ fn result_unwrap_or() {
     };
 
     // string case
-    match Ok("Bob") as Result<&str, &str> {
+    match Ok::<&str, &str>("Bob") {
         Ok(i) => i,
         Err(_) => "Alice",
     };
 
     // don't lint
-    match Ok(1) as Result<i32, &str> {
+    match Ok::<i32, &str>(1) {
         Ok(i) => i + 2,
         Err(_) => 42,
     };
-    match Ok(1) as Result<i32, &str> {
+    match Ok::<i32, &str>(1) {
         Ok(i) => i,
         Err(_) => return,
     };
     for j in 0..4 {
-        match Ok(j) as Result<i32, &str> {
+        match Ok::<i32, &str>(j) {
             Ok(i) => i,
             Err(_) => continue,
         };
-        match Ok(j) as Result<i32, &str> {
+        match Ok::<i32, &str>(j) {
             Ok(i) => i,
             Err(_) => break,
         };
     }
+
+    // don't lint, Err value is used
+    match Ok::<&str, &str>("Alice") {
+        Ok(s) => s,
+        Err(s) => s,
+    };
+    // could lint, but unused_variables takes care of it
+    match Ok::<&str, &str>("Alice") {
+        Ok(s) => s,
+        Err(s) => "Bob",
+    };
+}
+
+// don't lint in const fn
+const fn const_fn_option_unwrap_or() {
+    match Some(1) {
+        Some(s) => s,
+        None => 0,
+    };
+}
+
+const fn const_fn_result_unwrap_or() {
+    match Ok::<&str, &str>("Alice") {
+        Ok(s) => s,
+        Err(_) => "Bob",
+    };
+}
+
+mod issue6965 {
+    macro_rules! some_macro {
+        () => {
+            if 1 > 2 { Some(1) } else { None }
+        };
+    }
+
+    fn test() {
+        let _ = match some_macro!() {
+            Some(val) => val,
+            None => 0,
+        };
+    }
+}
+
+use std::rc::Rc;
+fn format_name(name: Option<&Rc<str>>) -> &str {
+    match name {
+        None => "<anon>",
+        Some(name) => name,
+    }
+}
+
+fn implicit_deref_ref() {
+    let _: &str = match Some(&"bye") {
+        None => "hi",
+        Some(s) => s,
+    };
 }
 
 fn main() {}