Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/union_must_have_exactly_one_field.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/union_must_have_exactly_one_field.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/union_must_have_exactly_one_field.rs b/crates/ide-diagnostics/src/handlers/union_must_have_exactly_one_field.rs
new file mode 100644
index 0000000000..e9a3b4ba4d
--- /dev/null
+++ b/crates/ide-diagnostics/src/handlers/union_must_have_exactly_one_field.rs
@@ -0,0 +1,42 @@
+use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
+
+// Diagnostic: union-must-have-exactly-one-field
+//
+// A union expression does not have exactly one field.
+pub(crate) fn union_must_have_exactly_one_field(
+ ctx: &DiagnosticsContext<'_>,
+ d: &hir::UnionMustHaveExactlyOneField,
+) -> Diagnostic {
+ Diagnostic::new_with_syntax_node_ptr(
+ ctx,
+ DiagnosticCode::RustcHardError("E0784"),
+ "union expressions should have exactly one field",
+ d.expr.map(|it| it.into()),
+ )
+ .stable()
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::tests::check_diagnostics;
+
+ #[test]
+ fn union_must_have_exactly_one_field() {
+ check_diagnostics(
+ r#"
+union Bird {
+ pigeon: u8,
+ turtledove: u16,
+}
+
+fn main() {
+ let bird = Bird { pigeon: 0 };
+ let bird = Bird {};
+ // ^^^^^^^ error: union expressions should have exactly one field
+ let bird = Bird { pigeon: 0, turtledove: 1 };
+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: union expressions should have exactly one field
+}
+"#,
+ );
+ }
+}