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
use hir::HirDisplay;

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

// Diagnostic: expected-array-or-slice-pat
//
// This diagnostic is triggered when an array or slice pattern is matched
// against a type that is neither an array nor a slice.
pub(crate) fn expected_array_or_slice_pat(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::ExpectedArrayOrSlicePat<'_>,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0529"),
        format!(
            "expected an array or slice, found {}",
            d.found.display(ctx.sema.db, ctx.display_target)
        ),
        d.pat.map(Into::into),
    )
    .stable()
}

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

    #[test]
    fn expected_array_or_slice() {
        check_diagnostics(
            r#"
fn f([_a, _b]: i32) {}
   //^^^^^^^^ error: expected an array or slice, found i32
"#,
        );
    }

    #[test]
    fn expected_array_or_slice_let_pattern() {
        check_diagnostics(
            r#"
fn f(x: i32) {
    let [_a, _b] = x;
      //^^^^^^^^ error: expected an array or slice, found i32
}
"#,
        );
    }
}