Skip to main content

ide_diagnostics/handlers/
expected_array_or_slice_pat.rs

1use hir::HirDisplay;
2
3use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
4
5// Diagnostic: expected-array-or-slice-pat
6//
7// This diagnostic is triggered when an array or slice pattern is matched
8// against a type that is neither an array nor a slice.
9pub(crate) fn expected_array_or_slice_pat(
10    ctx: &DiagnosticsContext<'_, '_>,
11    d: &hir::ExpectedArrayOrSlicePat<'_>,
12) -> Diagnostic {
13    Diagnostic::new_with_syntax_node_ptr(
14        ctx,
15        DiagnosticCode::RustcHardError("E0529"),
16        format!(
17            "expected an array or slice, found {}",
18            d.found.display(ctx.sema.db, ctx.display_target)
19        ),
20        d.pat.map(Into::into),
21    )
22    .stable()
23}
24
25#[cfg(test)]
26mod tests {
27    use crate::tests::check_diagnostics;
28
29    #[test]
30    fn expected_array_or_slice() {
31        check_diagnostics(
32            r#"
33fn f([_a, _b]: i32) {}
34   //^^^^^^^^ error: expected an array or slice, found i32
35"#,
36        );
37    }
38
39    #[test]
40    fn expected_array_or_slice_let_pattern() {
41        check_diagnostics(
42            r#"
43fn f(x: i32) {
44    let [_a, _b] = x;
45      //^^^^^^^^ error: expected an array or slice, found i32
46}
47"#,
48        );
49    }
50}