summaryrefslogtreecommitdiff
path: root/src/tools/clippy/tests/ui/option_if_let_else.rs
blob: 19f9f70451796914a3eccc967b4beba39c4fde17 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//@run-rustfix
#![warn(clippy::option_if_let_else)]
#![allow(
    unused_tuple_struct_fields,
    clippy::redundant_closure,
    clippy::ref_option_ref,
    clippy::equatable_if_let,
    clippy::let_unit_value
)]

fn bad1(string: Option<&str>) -> (bool, &str) {
    if let Some(x) = string {
        (true, x)
    } else {
        (false, "hello")
    }
}

fn else_if_option(string: Option<&str>) -> Option<(bool, &str)> {
    if string.is_none() {
        None
    } else if let Some(x) = string {
        Some((true, x))
    } else {
        Some((false, ""))
    }
}

fn unop_bad(string: &Option<&str>, mut num: Option<i32>) {
    let _ = if let Some(s) = *string { s.len() } else { 0 };
    let _ = if let Some(s) = &num { s } else { &0 };
    let _ = if let Some(s) = &mut num {
        *s += 1;
        s
    } else {
        &0
    };
    let _ = if let Some(ref s) = num { s } else { &0 };
    let _ = if let Some(mut s) = num {
        s += 1;
        s
    } else {
        0
    };
    let _ = if let Some(ref mut s) = num {
        *s += 1;
        s
    } else {
        &0
    };
}

fn longer_body(arg: Option<u32>) -> u32 {
    if let Some(x) = arg {
        let y = x * x;
        y * y
    } else {
        13
    }
}

fn impure_else(arg: Option<i32>) {
    let side_effect = || {
        println!("return 1");
        1
    };
    let _ = if let Some(x) = arg {
        x
    } else {
        // map_or_else must be suggested
        side_effect()
    };
}

fn test_map_or_else(arg: Option<u32>) {
    let _ = if let Some(x) = arg {
        x * x * x * x
    } else {
        let mut y = 1;
        y = (y + 2 / y) / 2;
        y = (y + 2 / y) / 2;
        y
    };
}

fn negative_tests(arg: Option<u32>) -> u32 {
    let _ = if let Some(13) = arg { "unlucky" } else { "lucky" };
    for _ in 0..10 {
        let _ = if let Some(x) = arg {
            x
        } else {
            continue;
        };
    }
    let _ = if let Some(x) = arg {
        return x;
    } else {
        5
    };
    7
}

// #7973
fn pattern_to_vec(pattern: &str) -> Vec<String> {
    pattern
        .trim_matches('/')
        .split('/')
        .flat_map(|s| {
            if let Some(idx) = s.find('.') {
                vec![s[..idx].to_string(), s[idx..].to_string()]
            } else {
                vec![s.to_string()]
            }
        })
        .collect::<Vec<_>>()
}

enum DummyEnum {
    One(u8),
    Two,
}

// should not warn since there is a complex subpat
// see #7991
fn complex_subpat() -> DummyEnum {
    let x = Some(DummyEnum::One(1));
    let _ = if let Some(_one @ DummyEnum::One(..)) = x { 1 } else { 2 };
    DummyEnum::Two
}

fn main() {
    let optional = Some(5);
    let _ = if let Some(x) = optional { x + 2 } else { 5 };
    let _ = bad1(None);
    let _ = else_if_option(None);
    unop_bad(&None, None);
    let _ = longer_body(None);
    test_map_or_else(None);
    let _ = negative_tests(None);
    let _ = impure_else(None);

    let _ = if let Some(x) = Some(0) {
        loop {
            if x == 0 {
                break x;
            }
        }
    } else {
        0
    };

    // #7576
    const fn _f(x: Option<u32>) -> u32 {
        // Don't lint, `map_or` isn't const
        if let Some(x) = x { x } else { 10 }
    }

    // #5822
    let s = String::new();
    // Don't lint, `Some` branch consumes `s`, but else branch uses `s`
    let _ = if let Some(x) = Some(0) {
        let s = s;
        s.len() + x
    } else {
        s.len()
    };

    let s = String::new();
    // Lint, both branches immutably borrow `s`.
    let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };

    let s = String::new();
    // Lint, `Some` branch consumes `s`, but else branch doesn't use `s`.
    let _ = if let Some(x) = Some(0) {
        let s = s;
        s.len() + x
    } else {
        1
    };

    let s = Some(String::new());
    // Don't lint, `Some` branch borrows `s`, but else branch consumes `s`
    let _ = if let Some(x) = &s {
        x.len()
    } else {
        let _s = s;
        10
    };

    let mut s = Some(String::new());
    // Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows  `s`
    let _ = if let Some(x) = &mut s {
        x.push_str("test");
        x.len()
    } else {
        let _s = &s;
        10
    };

    async fn _f1(x: u32) -> u32 {
        x
    }

    async fn _f2() {
        // Don't lint. `await` can't be moved into a closure.
        let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
    }

    let _ = pattern_to_vec("hello world");
    let _ = complex_subpat();

    // issue #8492
    let _ = match s {
        Some(string) => string.len(),
        None => 1,
    };
    let _ = match Some(10) {
        Some(a) => a + 1,
        None => 5,
    };

    let res: Result<i32, i32> = Ok(5);
    let _ = match res {
        Ok(a) => a + 1,
        _ => 1,
    };
    let _ = match res {
        Err(_) => 1,
        Ok(a) => a + 1,
    };
    let _ = if let Ok(a) = res { a + 1 } else { 5 };
}

#[allow(dead_code)]
fn issue9742() -> Option<&'static str> {
    // should not lint because of guards
    match Some("foo  ") {
        Some(name) if name.starts_with("foo") => Some(name.trim()),
        _ => None,
    }
}