Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/array_pattern_without_fixed_length.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/array_pattern_without_fixed_length.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/array_pattern_without_fixed_length.rs b/crates/ide-diagnostics/src/handlers/array_pattern_without_fixed_length.rs
new file mode 100644
index 0000000000..e7d0868350
--- /dev/null
+++ b/crates/ide-diagnostics/src/handlers/array_pattern_without_fixed_length.rs
@@ -0,0 +1,46 @@
+use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
+
+// Diagnostic: array-pattern-without-fixed-length
+//
+// This diagnostic is triggered when a rest array pattern is matched against an
+// array with a non-constant length.
+pub(crate) fn array_pattern_without_fixed_length(
+ ctx: &DiagnosticsContext<'_, '_>,
+ d: &hir::ArrayPatternWithoutFixedLength,
+) -> Diagnostic {
+ Diagnostic::new_with_syntax_node_ptr(
+ ctx,
+ DiagnosticCode::RustcHardError("E0730"),
+ "cannot pattern-match on an array without a fixed length",
+ d.pat.map(Into::into),
+ )
+ .stable()
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::tests::check_diagnostics;
+
+ #[test]
+ fn array_pattern_without_fixed_length() {
+ check_diagnostics(
+ r#"
+fn f<const N: usize>(arr: [u8; N]) {
+ let [_head, _tail @ ..] = arr;
+ //^^^^^^^^^^^^^^^^^^^ error: cannot pattern-match on an array without a fixed length
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn fixed_length_array_pattern_is_ok() {
+ check_diagnostics(
+ r#"
+fn f(arr: [u8; 3]) {
+ let [_head, _tail @ ..] = arr;
+}
+"#,
+ );
+ }
+}