Unnamed repository; edit this file 'description' to name the repository.
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use either::Either;
use hir::Semantics;
use ide_db::text_edit::TextEdit;
use ide_db::ty_filter::TryEnum;
use ide_db::{RootDatabase, source_change::SourceChange};
use syntax::{AstNode, ast};

use crate::{Assist, Diagnostic, DiagnosticCode, DiagnosticsContext, fix};

// Diagnostic: non-exhaustive-let
//
// This diagnostic is triggered if a `let` statement without an `else` branch has a non-exhaustive
// pattern.
pub(crate) fn non_exhaustive_let(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::NonExhaustiveLet,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0005"),
        format!("non-exhaustive pattern: {}", d.uncovered_patterns),
        d.pat.map(Into::into),
    )
    .stable()
    .with_fixes(fixes(&ctx.sema, d))
}

fn fixes(sema: &Semantics<'_, RootDatabase>, d: &hir::NonExhaustiveLet) -> Option<Vec<Assist>> {
    let root = sema.parse_or_expand(d.pat.file_id);
    let pat = d.pat.value.to_node(&root);
    let let_stmt = ast::LetStmt::cast(pat.syntax().parent()?)?;
    let early_node =
        sema.ancestors_with_macros(let_stmt.syntax().clone()).find_map(AstNode::cast)?;
    let early_text = early_text(sema, &early_node);

    if let_stmt.let_else().is_some() {
        return None;
    }
    let hir::FileRangeWrapper { file_id, range } = sema.original_range_opt(let_stmt.syntax())?;
    let insert_offset = if let Some(semicolon) = let_stmt.semicolon_token()
        && let Some(token) = sema.parse(file_id).syntax().token_at_offset(range.end()).left_biased()
        && token.kind() == semicolon.kind()
    {
        token.text_range().start()
    } else {
        range.end()
    };
    let semicolon = if let_stmt.semicolon_token().is_none() { ";" } else { "" };
    let else_block = format!(" else {{ {early_text} }}{semicolon}");
    let file_id = file_id.file_id(sema.db);

    let source_change =
        SourceChange::from_text_edit(file_id, TextEdit::insert(insert_offset, else_block));
    let target = sema.original_range(let_stmt.syntax()).range;
    Some(vec![fix("add_let_else_block", "Add let-else block", source_change, target)])
}

fn early_text(
    sema: &Semantics<'_, RootDatabase>,
    early_node: &Either<ast::AnyHasLoopBody, Either<ast::Fn, ast::ClosureExpr>>,
) -> &'static str {
    match early_node {
        Either::Left(_any_loop) => "continue",
        Either::Right(Either::Left(fn_)) => sema
            .to_def(fn_)
            .map(|fn_def| fn_def.ret_type(sema.db))
            .map(|ty| return_text(&ty, sema))
            .unwrap_or("return"),
        Either::Right(Either::Right(closure)) => closure
            .body()
            .and_then(|expr| sema.type_of_expr(&expr))
            .map(|ty| return_text(&ty.adjusted(), sema))
            .unwrap_or("return"),
    }
}

fn return_text(ty: &hir::Type<'_>, sema: &Semantics<'_, RootDatabase>) -> &'static str {
    if ty.is_unit() {
        "return"
    } else if let Some(try_enum) = TryEnum::from_ty(sema, ty) {
        match try_enum {
            TryEnum::Option => "return None",
            TryEnum::Result => "return Err($0)",
        }
    } else {
        "return $0"
    }
}

#[cfg(test)]
mod tests {
    use crate::tests::{check_diagnostics, check_fix};

    #[test]
    fn option_nonexhaustive() {
        check_diagnostics(
            r#"
//- minicore: option
fn main() {
    let None = Some(5);
      //^^^^ 💡 error: non-exhaustive pattern: `Some(_)` not covered
}
"#,
        );
    }

    #[test]
    fn option_exhaustive() {
        check_diagnostics(
            r#"
//- minicore: option
fn main() {
    let Some(_) | None = Some(5);
}
"#,
        );
    }

    #[test]
    fn option_nonexhaustive_inside_blocks() {
        check_diagnostics(
            r#"
//- minicore: option
fn main() {
    '_a: {
        let None = Some(5);
          //^^^^ 💡 error: non-exhaustive pattern: `Some(_)` not covered
    }
}
"#,
        );

        check_diagnostics(
            r#"
//- minicore: future, option
fn main() {
    let _ = async {
        let None = Some(5);
          //^^^^ 💡 error: non-exhaustive pattern: `Some(_)` not covered
    };
}
"#,
        );

        check_diagnostics(
            r#"
//- minicore: option
fn main() {
    unsafe {
        let None = Some(5);
          //^^^^ 💡 error: non-exhaustive pattern: `Some(_)` not covered
    }
}
"#,
        );
    }

    #[test]
    fn min_exhaustive() {
        check_diagnostics(
            r#"
//- minicore: result
fn test(x: Result<i32, !>) {
    let Ok(_y) = x;
}
"#,
        );

        check_diagnostics(
            r#"
//- minicore: result
fn test(x: Result<i32, &'static !>) {
    let Ok(_y) = x;
      //^^^^^^ 💡 error: non-exhaustive pattern: `Err(_)` not covered
}
"#,
        );
    }

    #[test]
    fn empty_patterns_normalize() {
        check_diagnostics(
            r#"
enum Infallible {}

trait Foo {
    type Assoc;
}
enum Enum<T: Foo> {
    A,
    B(T::Assoc),
}

impl Foo for () {
    type Assoc = Infallible;
}

fn foo(v: Enum<()>) {
    let Enum::A = v;
}
        "#,
        );
    }

    #[test]
    fn fix_return_in_loop() {
        check_fix(
            r#"
//- minicore: option
fn foo() {
    while cond {
        let None$0 = Some(5);
    }
}
"#,
            r#"
fn foo() {
    while cond {
        let None = Some(5) else { continue };
    }
}
"#,
        );
    }

    #[test]
    fn fix_return_in_fn() {
        check_fix(
            r#"
//- minicore: option
fn foo() {
    let None$0 = Some(5);
}
"#,
            r#"
fn foo() {
    let None = Some(5) else { return };
}
"#,
        );
    }

    #[test]
    fn fix_return_in_macro_expanded() {
        check_fix(
            r#"
//- minicore: option
macro_rules! identity { ($($t:tt)*) => { $($t)* }; }
fn foo() {
    identity! {
        let None$0 = Some(5);
    }
}
"#,
            r#"
macro_rules! identity { ($($t:tt)*) => { $($t)* }; }
fn foo() {
    identity! {
        let None = Some(5) else { return };
    }
}
"#,
        );
    }

    #[test]
    fn fix_return_in_incomplete_let() {
        check_fix(
            r#"
//- minicore: option
fn foo() {
    let None$0 = Some(5)
}
"#,
            r#"
fn foo() {
    let None = Some(5) else { return };
}
"#,
        );
    }

    #[test]
    fn fix_return_in_closure() {
        check_fix(
            r#"
//- minicore: option
fn foo() -> Option<()> {
    let _f = || {
        let None$0 = Some(5);
    };
}
"#,
            r#"
fn foo() -> Option<()> {
    let _f = || {
        let None = Some(5) else { return };
    };
}
"#,
        );
    }

    #[test]
    fn fix_return_try_in_fn() {
        check_fix(
            r#"
//- minicore: option
fn foo() -> Option<()> {
    let None$0 = Some(5);
}
"#,
            r#"
fn foo() -> Option<()> {
    let None = Some(5) else { return None };
}
"#,
        );

        check_fix(
            r#"
//- minicore: option, result
fn foo() -> Result<(), i32> {
    let None$0 = Some(5);
}
"#,
            r#"
fn foo() -> Result<(), i32> {
    let None = Some(5) else { return Err($0) };
}
"#,
        );
    }

    #[test]
    fn regression_20259() {
        check_diagnostics(
            r#"
//- minicore: deref
use core::ops::Deref;

struct Foo<T>(T);

impl<T> Deref for Foo<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn test(x: Foo<(i32, bool)>) {
    let (_a, _b): &(i32, bool) = &x;
}
"#,
        );
    }

    #[test]
    fn uninhabited_variants() {
        check_diagnostics(
            r#"
//- minicore: result
enum Infallible {}

trait Foo {
    type Bar;
}

struct Wrapper<T> {
    error: T,
}

struct FooWrapper<T: Foo> {
    error: T::Bar,
}

fn foo<T: Foo<Bar = Infallible>>(result: Result<T, T::Bar>) -> T {
    let Ok(ok) = result;
    ok
}

fn bar<T: Foo<Bar = Infallible>>(result: Result<T, (T::Bar,)>) -> T {
    let Ok(ok) = result;
    ok
}

fn baz<T: Foo<Bar = Infallible>>(result: Result<T, Wrapper<T::Bar>>) -> T {
    let Ok(ok) = result;
    ok
}

fn qux<T: Foo<Bar = Infallible>>(result: Result<T, FooWrapper<T>>) -> T {
    let Ok(ok) = result;
    ok
}

fn quux<T: Foo<Bar = Infallible>>(result: Result<T, [T::Bar; 1]>) -> T {
    let Ok(ok) = result;
    ok
}

fn corge<T: Foo<Bar = Infallible>>(result: Result<T, (i32, T::Bar)>) -> T {
    let Ok(ok) = result;
    ok
}
"#,
        );
    }
}