]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/or_then_unwrap.rs
Auto merge of #94566 - yanganto:show-ignore-message, r=m-ou-se
[rust.git] / src / tools / clippy / tests / ui / or_then_unwrap.rs
1 // run-rustfix
2
3 #![warn(clippy::or_then_unwrap)]
4 #![allow(clippy::map_identity)]
5
6 struct SomeStruct {}
7 impl SomeStruct {
8     fn or(self, _: Option<Self>) -> Self {
9         self
10     }
11     fn unwrap(&self) {}
12 }
13
14 struct SomeOtherStruct {}
15 impl SomeOtherStruct {
16     fn or(self) -> Self {
17         self
18     }
19     fn unwrap(&self) {}
20 }
21
22 fn main() {
23     let option: Option<&str> = None;
24     let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
25
26     let result: Result<&str, &str> = Err("Error");
27     let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint
28
29     // as part of a method chain
30     let option: Option<&str> = None;
31     let _ = option.map(|v| v).or(Some("fallback")).unwrap().to_string().chars(); // should trigger lint
32
33     // Not Option/Result
34     let instance = SomeStruct {};
35     let _ = instance.or(Some(SomeStruct {})).unwrap(); // should not trigger lint
36
37     // or takes no argument
38     let instance = SomeOtherStruct {};
39     let _ = instance.or().unwrap(); // should not trigger lint and should not panic
40
41     // None in or
42     let option: Option<&str> = None;
43     let _ = option.or(None).unwrap(); // should not trigger lint
44
45     // Not Err in or
46     let result: Result<&str, &str> = Err("Error");
47     let _ = result.or::<&str>(Err("Other Error")).unwrap(); // should not trigger lint
48
49     // other function between
50     let option: Option<&str> = None;
51     let _ = option.or(Some("fallback")).map(|v| v).unwrap(); // should not trigger lint
52 }