// run-rustfix #![warn(clippy::needless_question_mark)] #![allow(clippy::needless_return, clippy::unnecessary_unwrap, dead_code, unused_must_use)] #![feature(custom_inner_attributes)] struct TO { magic: Option, } struct TR { magic: Result, } fn simple_option_bad1(to: TO) -> Option { // return as a statement return Some(to.magic?); } // formatting will add a semi-colon, which would make // this identical to the test case above #[rustfmt::skip] fn simple_option_bad2(to: TO) -> Option { // return as an expression return Some(to.magic?) } fn simple_option_bad3(to: TO) -> Option { // block value "return" Some(to.magic?) } fn simple_option_bad4(to: Option) -> Option { // single line closure to.and_then(|t| Some(t.magic?)) } // formatting this will remove the block brackets, making // this test identical to the one above #[rustfmt::skip] fn simple_option_bad5(to: Option) -> Option { // closure with body to.and_then(|t| { Some(t.magic?) }) } fn simple_result_bad1(tr: TR) -> Result { return Ok(tr.magic?); } // formatting will add a semi-colon, which would make // this identical to the test case above #[rustfmt::skip] fn simple_result_bad2(tr: TR) -> Result { return Ok(tr.magic?) } fn simple_result_bad3(tr: TR) -> Result { Ok(tr.magic?) } fn simple_result_bad4(tr: Result) -> Result { tr.and_then(|t| Ok(t.magic?)) } // formatting this will remove the block brackets, making // this test identical to the one above #[rustfmt::skip] fn simple_result_bad5(tr: Result) -> Result { tr.and_then(|t| { Ok(t.magic?) }) } fn also_bad(tr: Result) -> Result { if tr.is_ok() { let t = tr.unwrap(); return Ok(t.magic?); } Err(false) } fn false_positive_test(x: Result<(), U>) -> Result<(), T> where T: From, { Ok(x?) } fn main() {} mod question_mark_none { #![clippy::msrv = "1.12.0"] fn needless_question_mark_option() -> Option { struct TO { magic: Option, } let to = TO { magic: None }; Some(to.magic?) // should not be triggered } fn needless_question_mark_result() -> Result { struct TO { magic: Result, } let to = TO { magic: Ok(1_usize) }; Ok(to.magic?) // should not be triggered } fn main() { needless_question_mark_option(); needless_question_mark_result(); } } mod question_mark_result { #![clippy::msrv = "1.21.0"] fn needless_question_mark_option() -> Option { struct TO { magic: Option, } let to = TO { magic: None }; Some(to.magic?) // should not be triggered } fn needless_question_mark_result() -> Result { struct TO { magic: Result, } let to = TO { magic: Ok(1_usize) }; Ok(to.magic?) // should be triggered } fn main() { needless_question_mark_option(); needless_question_mark_result(); } } mod question_mark_both { #![clippy::msrv = "1.22.0"] fn needless_question_mark_option() -> Option { struct TO { magic: Option, } let to = TO { magic: None }; Some(to.magic?) // should be triggered } fn needless_question_mark_result() -> Result { struct TO { magic: Result, } let to = TO { magic: Ok(1_usize) }; Ok(to.magic?) // should be triggered } fn main() { needless_question_mark_option(); needless_question_mark_result(); } }