]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/question_mark.rs
Move MSRV tests into the lint specific test files
[rust.git] / tests / ui / question_mark.rs
index fc68a42e46f21d5f151467d207fcd9ed540912d1..9ae0d88829af55cb91d387cadd0bc84a83a119e8 100644 (file)
@@ -1,10 +1,8 @@
 // run-rustfix
 #![allow(unreachable_code)]
+#![allow(dead_code)]
 #![allow(clippy::unnecessary_wraps)]
 
-use std::env;
-use std::path::PathBuf;
-
 fn some_func(a: Option<u32>) -> Option<u32> {
     if a.is_none() {
         return None;
@@ -137,11 +135,11 @@ fn f() -> Option<String> {
     Some(0)
 }
 
-fn func_returning_result() -> Result<i32, String> {
+fn func_returning_result() -> Result<i32, i32> {
     Ok(1)
 }
 
-fn result_func(x: Result<i32, String>) -> Result<i32, String> {
+fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
     let _ = if let Ok(x) = x { x } else { return x };
 
     if x.is_err() {
@@ -152,7 +150,7 @@ fn result_func(x: Result<i32, String>) -> Result<i32, String> {
     let y = if let Ok(x) = x {
         x
     } else {
-        return Err("some error".to_string());
+        return Err(0);
     };
 
     // issue #7859
@@ -160,9 +158,10 @@ fn result_func(x: Result<i32, String>) -> Result<i32, String> {
     let _ = if let Ok(x) = func_returning_result() {
         x
     } else {
-        return Err("some error".to_string());
+        return Err(0);
     };
 
+    // no warning
     if func_returning_result().is_err() {
         return func_returning_result();
     }
@@ -170,25 +169,102 @@ fn result_func(x: Result<i32, String>) -> Result<i32, String> {
     Ok(y)
 }
 
-fn main() {
-    some_func(Some(42));
-    some_func(None);
-    some_other_func(Some(42));
+// see issue #8019
+pub enum NotOption {
+    None,
+    First,
+    AfterFirst,
+}
+
+fn obj(_: i32) -> Result<(), NotOption> {
+    Err(NotOption::First)
+}
 
-    let copy_struct = CopyStruct { opt: Some(54) };
-    copy_struct.func();
+fn f() -> NotOption {
+    if obj(2).is_err() {
+        return NotOption::None;
+    }
+    NotOption::First
+}
 
-    let move_struct = MoveStruct {
-        opt: Some(vec![42, 1337]),
-    };
-    move_struct.ref_func();
-    move_struct.clone().mov_func_reuse();
-    move_struct.mov_func_no_use();
+fn do_something() {}
+
+fn err_immediate_return() -> Result<i32, i32> {
+    if let Err(err) = func_returning_result() {
+        return Err(err);
+    }
+    Ok(1)
+}
+
+fn err_immediate_return_and_do_something() -> Result<i32, i32> {
+    if let Err(err) = func_returning_result() {
+        return Err(err);
+    }
+    do_something();
+    Ok(1)
+}
+
+// No warning
+fn no_immediate_return() -> Result<i32, i32> {
+    if let Err(err) = func_returning_result() {
+        do_something();
+        return Err(err);
+    }
+    Ok(1)
+}
+
+// No warning
+fn mixed_result_and_option() -> Option<i32> {
+    if let Err(err) = func_returning_result() {
+        return Some(err);
+    }
+    None
+}
+
+// No warning
+fn else_if_check() -> Result<i32, i32> {
+    if true {
+        Ok(1)
+    } else if let Err(e) = func_returning_result() {
+        Err(e)
+    } else {
+        Err(-1)
+    }
+}
+
+// No warning
+#[allow(clippy::manual_map)]
+#[rustfmt::skip]
+fn option_map() -> Option<bool> {
+    if let Some(a) = Some(false) {
+        Some(!a)
+    } else {
+        None
+    }
+}
+
+pub struct PatternedError {
+    flag: bool,
+}
+
+// No warning
+fn pattern() -> Result<(), PatternedError> {
+    let res = Ok(());
 
-    let so = SeemsOption::Some(45);
-    returns_something_similar_to_option(so);
+    if let Err(err @ PatternedError { flag: true }) = res {
+        return Err(err);
+    }
+
+    res
+}
 
-    func();
+fn main() {}
 
-    let _ = result_func(Ok(42));
+// should not lint, `?` operator not available in const context
+const fn issue9175(option: Option<()>) -> Option<()> {
+    if option.is_none() {
+        return None;
+    }
+    //stuff
+    Some(())
 }