Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs b/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs
new file mode 100644
index 0000000000..1a4d2877ef
--- /dev/null
+++ b/crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs
@@ -0,0 +1,47 @@
+use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
+
+// 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),
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::tests::check_diagnostics;
+
+ #[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);
+}
+"#,
+ );
+ }
+}