Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/expected_array_or_slice_pat.rs')
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/expected_array_or_slice_pat.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/expected_array_or_slice_pat.rs b/crates/ide-diagnostics/src/handlers/expected_array_or_slice_pat.rs new file mode 100644 index 0000000000..ab2c3ccd12 --- /dev/null +++ b/crates/ide-diagnostics/src/handlers/expected_array_or_slice_pat.rs @@ -0,0 +1,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 +} +"#, + ); + } +} |