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