about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/manual_map_option_2.rs
blob: a47dc950760e2a167100cecc310e011df282720b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![warn(clippy::manual_map)]
#![allow(clippy::toplevel_ref_arg)]

fn main() {
    // Lint. `y` is declared within the arm, so it isn't captured by the map closure
    let _ = match Some(0) {
        //~^ manual_map
        Some(x) => Some({
            let y = (String::new(), String::new());
            (x, y.0)
        }),
        None => None,
    };

    // Don't lint. `s` is borrowed until partway through the arm, but needs to be captured by the map
    // closure
    let s = Some(String::new());
    let _ = match &s {
        Some(x) => Some((x.clone(), s)),
        None => None,
    };

    // Don't lint. `s` is borrowed until partway through the arm, but needs to be captured by the map
    // closure
    let s = Some(String::new());
    let _ = match &s {
        Some(x) => Some({
            let clone = x.clone();
            let s = || s;
            (clone, s())
        }),
        None => None,
    };

    // Don't lint. `s` is borrowed until partway through the arm, but needs to be captured as a mutable
    // reference by the map closure
    let mut s = Some(String::new());
    let _ = match &s {
        Some(x) => Some({
            let clone = x.clone();
            let ref mut s = s;
            (clone, s)
        }),
        None => None,
    };

    let s = Some(String::new());
    // Lint. `s` is captured by reference, so no lifetime issues.
    let _ = match &s {
        //~^ manual_map
        Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
        None => None,
    };
    // Don't lint this, type of `s` is coercioned from `&String` to `&str`
    let x: Option<(String, &str)> = match &s {
        Some(x) => Some({ if let Some(ref s) = s { (x.clone(), s) } else { panic!() } }),
        None => None,
    };

    // Issue #7820
    unsafe fn f(x: u32) -> u32 {
        x
    }
    unsafe {
        let _ = match Some(0) {
            //~^ manual_map
            Some(x) => Some(f(x)),
            None => None,
        };
    }
    let _ = match Some(0) {
        //~^ manual_map
        Some(x) => unsafe { Some(f(x)) },
        None => None,
    };
    let _ = match Some(0) {
        //~^ manual_map
        Some(x) => Some(unsafe { f(x) }),
        None => None,
    };
}

// issue #12659
mod with_type_coercion {
    trait DummyTrait {}

    fn foo<T: DummyTrait, F: Fn() -> Result<T, ()>>(f: F) {
        // Don't lint
        let _: Option<Result<Box<dyn DummyTrait>, ()>> = match Some(0) {
            Some(_) => Some(match f() {
                Ok(res) => Ok(Box::new(res)),
                _ => Err(()),
            }),
            None => None,
        };

        let _: Option<Box<&[u8]>> = match Some(()) {
            Some(_) => Some(Box::new(b"1234")),
            None => None,
        };

        let x = String::new();
        let _: Option<Box<&str>> = match Some(()) {
            Some(_) => Some(Box::new(&x)),
            None => None,
        };

        let _: Option<&str> = match Some(()) {
            Some(_) => Some(&x),
            None => None,
        };

        let _ = match Some(0) {
            //~^ manual_map
            Some(_) => Some(match f() {
                Ok(res) => Ok(Box::new(res)),
                _ => Err(()),
            }),
            None => None,
        };
    }

    #[allow(clippy::redundant_allocation)]
    fn bar() {
        fn f(_: Option<Box<&[u8]>>) {}
        fn g(b: &[u8]) -> Box<&[u8]> {
            Box::new(b)
        }

        let x: &[u8; 4] = b"1234";
        f(match Some(()) {
            Some(_) => Some(Box::new(x)),
            None => None,
        });

        let _: Option<Box<&[u8]>> = match Some(0) {
            //~^ manual_map
            Some(_) => Some(g(x)),
            None => None,
        };
    }

    fn with_fn_ret(s: &Option<String>) -> Option<(String, &str)> {
        // Don't lint, `map` doesn't work as the return type is adjusted.
        match s {
            Some(x) => Some({ if let Some(s) = s { (x.clone(), s) } else { panic!() } }),
            None => None,
        }
    }

    fn with_fn_ret_2(s: &Option<String>) -> Option<(String, &str)> {
        if true {
            // Don't lint, `map` doesn't work as the return type is adjusted.
            return match s {
                Some(x) => Some({ if let Some(s) = s { (x.clone(), s) } else { panic!() } }),
                None => None,
            };
        }
        None
    }

    #[allow(clippy::needless_late_init)]
    fn with_fn_ret_3<'a>(s: &'a Option<String>) -> Option<(String, &'a str)> {
        let x: Option<(String, &'a str)>;
        x = {
            match s {
                Some(x) => Some({ if let Some(s) = s { (x.clone(), s) } else { panic!() } }),
                None => None,
            }
        };
        x
    }
}