]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/if_then_some_else_none.rs
Auto merge of #88354 - Jmc18134:hint-space-pauth-opt, r=nagisa
[rust.git] / src / tools / clippy / tests / ui / if_then_some_else_none.rs
1 #![warn(clippy::if_then_some_else_none)]
2 #![feature(custom_inner_attributes)]
3
4 fn main() {
5     // Should issue an error.
6     let _ = if foo() {
7         println!("true!");
8         Some("foo")
9     } else {
10         None
11     };
12
13     // Should issue an error when macros are used.
14     let _ = if matches!(true, true) {
15         println!("true!");
16         Some(matches!(true, false))
17     } else {
18         None
19     };
20
21     // Should issue an error. Binary expression `o < 32` should be parenthesized.
22     let x = Some(5);
23     let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });
24
25     // Should issue an error. Unary expression `!x` should be parenthesized.
26     let x = true;
27     let _ = if !x { Some(0) } else { None };
28
29     // Should not issue an error since the `else` block has a statement besides `None`.
30     let _ = if foo() {
31         println!("true!");
32         Some("foo")
33     } else {
34         eprintln!("false...");
35         None
36     };
37
38     // Should not issue an error since there are more than 2 blocks in the if-else chain.
39     let _ = if foo() {
40         println!("foo true!");
41         Some("foo")
42     } else if bar() {
43         println!("bar true!");
44         Some("bar")
45     } else {
46         None
47     };
48
49     let _ = if foo() {
50         println!("foo true!");
51         Some("foo")
52     } else {
53         bar().then(|| {
54             println!("bar true!");
55             "bar"
56         })
57     };
58
59     // Should not issue an error since the `then` block has `None`, not `Some`.
60     let _ = if foo() { None } else { Some("foo is false") };
61
62     // Should not issue an error since the `else` block doesn't use `None` directly.
63     let _ = if foo() { Some("foo is true") } else { into_none() };
64
65     // Should not issue an error since the `then` block doesn't use `Some` directly.
66     let _ = if foo() { into_some("foo") } else { None };
67 }
68
69 fn _msrv_1_49() {
70     #![clippy::msrv = "1.49"]
71     // `bool::then` was stabilized in 1.50. Do not lint this
72     let _ = if foo() {
73         println!("true!");
74         Some(149)
75     } else {
76         None
77     };
78 }
79
80 fn _msrv_1_50() {
81     #![clippy::msrv = "1.50"]
82     let _ = if foo() {
83         println!("true!");
84         Some(150)
85     } else {
86         None
87     };
88 }
89
90 fn foo() -> bool {
91     unimplemented!()
92 }
93
94 fn bar() -> bool {
95     unimplemented!()
96 }
97
98 fn into_some<T>(v: T) -> Option<T> {
99     Some(v)
100 }
101
102 fn into_none<T>() -> Option<T> {
103     None
104 }
105
106 // Should not warn
107 fn f(b: bool, v: Option<()>) -> Option<()> {
108     if b {
109         v?; // This is a potential early return, is not equivalent with `bool::then`
110
111         Some(())
112     } else {
113         None
114     }
115 }