1 #![warn(clippy::if_then_some_else_none)]
4 // Should issue an error.
12 // Should issue an error when macros are used.
13 let _ = if matches!(true, true) {
15 Some(matches!(true, false))
20 // Should issue an error. Binary expression `o < 32` should be parenthesized.
22 let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });
24 // Should issue an error. Unary expression `!x` should be parenthesized.
26 let _ = if !x { Some(0) } else { None };
28 // Should not issue an error since the `else` block has a statement besides `None`.
33 eprintln!("false...");
37 // Should not issue an error since there are more than 2 blocks in the if-else chain.
39 println!("foo true!");
42 println!("bar true!");
49 println!("foo true!");
53 println!("bar true!");
58 // Should not issue an error since the `then` block has `None`, not `Some`.
59 let _ = if foo() { None } else { Some("foo is false") };
61 // Should not issue an error since the `else` block doesn't use `None` directly.
62 let _ = if foo() { Some("foo is true") } else { into_none() };
64 // Should not issue an error since the `then` block doesn't use `Some` directly.
65 let _ = if foo() { into_some("foo") } else { None };
68 #[clippy::msrv = "1.49"]
70 // `bool::then` was stabilized in 1.50. Do not lint this
79 #[clippy::msrv = "1.50"]
97 fn into_some<T>(v: T) -> Option<T> {
101 fn into_none<T>() -> Option<T> {
106 fn f(b: bool, v: Option<()>) -> Option<()> {
108 v?; // This is a potential early return, is not equivalent with `bool::then`