]> git.lizzy.rs Git - rust.git/blob - tests/ui/if_then_some_else_none.rs
Output help instead of suggestion in `if_then_some_else_none` diagnose
[rust.git] / 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 not issue an error since the `else` block has a statement besides `None`.
14     let _ = if foo() {
15         println!("true!");
16         Some("foo")
17     } else {
18         eprintln!("false...");
19         None
20     };
21
22     // Should not issue an error since there are more than 2 blocks in the if-else chain.
23     let _ = if foo() {
24         println!("foo true!");
25         Some("foo")
26     } else if bar() {
27         println!("bar true!");
28         Some("bar")
29     } else {
30         None
31     };
32
33     let _ = if foo() {
34         println!("foo true!");
35         Some("foo")
36     } else {
37         bar().then(|| {
38             println!("bar true!");
39             "bar"
40         })
41     };
42
43     // Should not issue an error since the `then` block has `None`, not `Some`.
44     let _ = if foo() { None } else { Some("foo is false") };
45
46     // Should not issue an error since the `else` block doesn't use `None` directly.
47     let _ = if foo() { Some("foo is true") } else { into_none() };
48
49     // Should not issue an error since the `then` block doesn't use `Some` directly.
50     let _ = if foo() { into_some("foo") } else { None };
51 }
52
53 fn _msrv_1_49() {
54     #![clippy::msrv = "1.49"]
55     // `bool::then` was stabilized in 1.50. Do not lint this
56     let _ = if foo() {
57         println!("true!");
58         Some(149)
59     } else {
60         None
61     };
62 }
63
64 fn _msrv_1_50() {
65     #![clippy::msrv = "1.50"]
66     let _ = if foo() {
67         println!("true!");
68         Some(150)
69     } else {
70         None
71     };
72 }
73
74 fn foo() -> bool {
75     unimplemented!()
76 }
77
78 fn bar() -> bool {
79     unimplemented!()
80 }
81
82 fn into_some<T>(v: T) -> Option<T> {
83     Some(v)
84 }
85
86 fn into_none<T>() -> Option<T> {
87     None
88 }