]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/needless_question_mark.rs
fix `needless_question_mark` not considering async fn
[rust.git] / tests / ui / needless_question_mark.rs
index 60ac2c8d72eac920afe0a46b4253f8ac5594b316..3a6523e8fe872a94b172c851d1c997f9a1ed9350 100644 (file)
@@ -1,7 +1,13 @@
 // run-rustfix
 
 #![warn(clippy::needless_question_mark)]
-#![allow(clippy::needless_return, clippy::unnecessary_unwrap, dead_code, unused_must_use)]
+#![allow(
+    clippy::needless_return,
+    clippy::unnecessary_unwrap,
+    clippy::upper_case_acronyms,
+    dead_code,
+    unused_must_use
+)]
 #![feature(custom_inner_attributes)]
 
 struct TO {
@@ -88,76 +94,47 @@ fn false_positive_test<U, T>(x: Result<(), U>) -> Result<(), T>
     Ok(x?)
 }
 
-fn main() {}
-
-mod question_mark_none {
-    #![clippy::msrv = "1.12.0"]
-    fn needless_question_mark_option() -> Option<usize> {
-        struct TO {
-            magic: Option<usize>,
-        }
-        let to = TO { magic: None };
-        Some(to.magic?) // should not be triggered
-    }
+// not quite needless
+fn deref_ref(s: Option<&String>) -> Option<&str> {
+    Some(s?)
+}
 
-    fn needless_question_mark_result() -> Result<usize, bool> {
-        struct TO {
-            magic: Result<usize, bool>,
-        }
-        let to = TO { magic: Ok(1_usize) };
-        Ok(to.magic?) // should not be triggered
-    }
+fn main() {}
 
-    fn main() {
-        needless_question_mark_option();
-        needless_question_mark_result();
-    }
+// #6921 if a macro wraps an expr in Some(  ) and the ? is in the macro use,
+// the suggestion fails to apply; do not lint
+macro_rules! some_in_macro {
+    ($expr:expr) => {
+        || -> _ { Some($expr) }()
+    };
 }
 
-mod question_mark_result {
-    #![clippy::msrv = "1.21.0"]
-    fn needless_question_mark_option() -> Option<usize> {
-        struct TO {
-            magic: Option<usize>,
-        }
-        let to = TO { magic: None };
-        Some(to.magic?) // should not be triggered
-    }
+pub fn test1() {
+    let x = Some(3);
+    let _x = some_in_macro!(x?);
+}
 
-    fn needless_question_mark_result() -> Result<usize, bool> {
-        struct TO {
-            magic: Result<usize, bool>,
-        }
-        let to = TO { magic: Ok(1_usize) };
-        Ok(to.magic?) // should be triggered
-    }
+// this one is ok because both the ? and the Some are both inside the macro def
+macro_rules! some_and_qmark_in_macro {
+    ($expr:expr) => {
+        || -> Option<_> { Some(Some($expr)?) }()
+    };
+}
 
-    fn main() {
-        needless_question_mark_option();
-        needless_question_mark_result();
-    }
+pub fn test2() {
+    let x = Some(3);
+    let _x = some_and_qmark_in_macro!(x?);
 }
 
-mod question_mark_both {
-    #![clippy::msrv = "1.22.0"]
-    fn needless_question_mark_option() -> Option<usize> {
-        struct TO {
-            magic: Option<usize>,
-        }
-        let to = TO { magic: None };
-        Some(to.magic?) // should be triggered
-    }
+async fn async_option_bad(to: TO) -> Option<usize> {
+    let _ = Some(3);
+    Some(to.magic?)
+}
 
-    fn needless_question_mark_result() -> Result<usize, bool> {
-        struct TO {
-            magic: Result<usize, bool>,
-        }
-        let to = TO { magic: Ok(1_usize) };
-        Ok(to.magic?) // should be triggered
-    }
+async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
+    Some(s?)
+}
 
-    fn main() {
-        needless_question_mark_option();
-        needless_question_mark_result();
-    }
+async fn async_result_bad(s: TR) -> Result<usize, bool> {
+    Ok(s.magic?)
 }