// 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 {
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?)
}