Skip to main content

ide_diagnostics/handlers/
union_expr_must_have_exactly_one_field.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: union-expr-must-have-exactly-one-field
4//
5// A union expression does not have exactly one field.
6pub(crate) fn union_expr_must_have_exactly_one_field(
7    ctx: &DiagnosticsContext<'_, '_>,
8    d: &hir::UnionExprMustHaveExactlyOneField,
9) -> Diagnostic {
10    Diagnostic::new_with_syntax_node_ptr(
11        ctx,
12        DiagnosticCode::RustcHardError("E0784"),
13        "union expressions should have exactly one field",
14        d.expr.map(|it| it.into()),
15    )
16    .stable()
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::tests::check_diagnostics;
22
23    #[test]
24    fn union_expr_must_have_exactly_one_field() {
25        check_diagnostics(
26            r#"
27union Bird {
28    pigeon: u8,
29    turtledove: u16,
30}
31
32fn main() {
33    let bird = Bird { pigeon: 0 };
34    let bird = Bird {};
35            // ^^^^^^^ error: union expressions should have exactly one field
36    let bird = Bird { pigeon: 0, turtledove: 1 };
37            // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: union expressions should have exactly one field
38}
39"#,
40        );
41    }
42}