about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/manual_ok_err.fixed
blob: 9b70ce0df43a7848834c3e130e75d9722a10e06a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![warn(clippy::manual_ok_err)]

fn funcall() -> Result<u32, &'static str> {
    todo!()
}

fn main() {
    let _ = funcall().ok();

    let _ = funcall().ok();

    let _ = funcall().err();

    let _ = funcall().err();

    let _ = funcall().ok();

    let _ = funcall().err();

    #[allow(clippy::redundant_pattern)]
    let _ = funcall().ok();

    struct S;

    impl std::ops::Neg for S {
        type Output = Result<u32, &'static str>;

        fn neg(self) -> Self::Output {
            funcall()
        }
    }

    // Suggestion should be properly parenthesized
    let _ = (-S).ok();

    no_lint();
}

fn no_lint() {
    let _ = match funcall() {
        Ok(v) if v > 3 => Some(v),
        _ => None,
    };

    let _ = match funcall() {
        Err(_) => None,
        Ok(3) => None,
        Ok(v) => Some(v),
    };

    let _ = match funcall() {
        _ => None,
        Ok(v) => Some(v),
    };

    let _ = match funcall() {
        Err(_) | Ok(3) => None,
        Ok(v) => Some(v),
    };

    #[expect(clippy::redundant_pattern)]
    let _ = match funcall() {
        _v @ _ => None,
        Ok(v) => Some(v),
    };

    // Content of `Option` and matching content of `Result` do
    // not have the same type.
    let _: Option<&dyn std::any::Any> = match Ok::<_, ()>(&1) {
        Ok(v) => Some(v),
        _ => None,
    };

    let _ = match Ok::<_, ()>(&1) {
        _x => None,
        Ok(v) => Some(v),
    };

    let _ = match Ok::<_, std::convert::Infallible>(1) {
        Ok(3) => None,
        Ok(v) => Some(v),
    };

    let _ = match funcall() {
        Ok(v @ 1..) => Some(v),
        _ => None,
    };
}

const fn cf(x: Result<u32, &'static str>) -> Option<u32> {
    // Do not lint in const code
    match x {
        Ok(v) => Some(v),
        Err(_) => None,
    }
}

fn issue14239() {
    let _ = if false {
        None
    } else {
        "1".parse::<u8>().ok()
    };
    //~^^^^^ manual_ok_err
}

mod issue15051 {
    struct Container {
        field: Result<bool, ()>,
    }

    #[allow(clippy::needless_borrow)]
    fn with_addr_of(x: &Container) -> Option<&bool> {
        (&x.field).as_ref().ok()
    }

    fn from_fn(x: &Container) -> Option<&bool> {
        let result_with_ref = || &x.field;
        result_with_ref().as_ref().ok()
    }

    fn result_with_ref_mut(x: &mut Container) -> &mut Result<bool, ()> {
        &mut x.field
    }

    fn from_fn_mut(x: &mut Container) -> Option<&mut bool> {
        result_with_ref_mut(x).as_mut().ok()
    }
}