about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/or_then_unwrap.fixed
blob: 9660b82fe7d619387c547b2e6762ff0e6408eedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![warn(clippy::or_then_unwrap)]
#![allow(clippy::map_identity, clippy::let_unit_value, clippy::unnecessary_literal_unwrap)]

struct SomeStruct;
impl SomeStruct {
    fn or(self, _: Option<Self>) -> Self {
        self
    }
    fn unwrap(&self) {}
}

struct SomeOtherStruct;
impl SomeOtherStruct {
    fn or(self) -> Self {
        self
    }
    fn unwrap(&self) {}
}

fn main() {
    let option: Option<&str> = None;
    let _ = option.unwrap_or("fallback"); // should trigger lint
    //
    //~^^ or_then_unwrap

    let result: Result<&str, &str> = Err("Error");
    let _ = result.unwrap_or("fallback"); // should trigger lint
    //
    //~^^ or_then_unwrap

    // Call with macro should preserve the macro call rather than expand it
    let option: Option<Vec<&str>> = None;
    let _ = option.unwrap_or(vec!["fallback"]); // should trigger lint
    //
    //~^^ or_then_unwrap

    // as part of a method chain
    let option: Option<&str> = None;
    let _ = option.map(|v| v).unwrap_or("fallback").to_string().chars(); // should trigger lint
    //
    //~^^ or_then_unwrap

    // Not Option/Result
    let instance = SomeStruct {};
    let _ = instance.or(Some(SomeStruct {})).unwrap(); // should not trigger lint

    // or takes no argument
    let instance = SomeOtherStruct {};
    let _ = instance.or().unwrap(); // should not trigger lint and should not panic

    // None in or
    let option: Option<&str> = None;
    let _ = option.or(None).unwrap(); // should not trigger lint

    // Not Err in or
    let result: Result<&str, &str> = Err("Error");
    let _ = result.or::<&str>(Err("Other Error")).unwrap(); // should not trigger lint

    // other function between
    let option: Option<&str> = None;
    let _ = option.or(Some("fallback")).map(|v| v).unwrap(); // should not trigger lint
}