Skip to main content

ide_diagnostics/handlers/
fru_in_destructuring_assignment.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: fru-in-destructuring-assignment
4//
5// This diagnostic is triggered when a destructuring assignment contains functional record update
6pub(crate) fn fru_in_destructuring_assignment(
7    ctx: &DiagnosticsContext<'_, '_>,
8    d: &hir::FruInDestructuringAssignment,
9) -> Diagnostic {
10    Diagnostic::new_with_syntax_node_ptr(
11        ctx,
12        DiagnosticCode::SyntaxError,
13        "functional record updates are not allowed in destructuring assignments",
14        d.node.map(Into::into),
15    )
16    .stable()
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::tests::{check_diagnostics, check_diagnostics_with_disabled};
22
23    #[test]
24    fn spread_variable() {
25        check_diagnostics_with_disabled(
26            r#"
27struct Foo { bar: u32, baz: u32 }
28fn test(f: Foo, g: Foo, mut bar: u32, mut baz: u32) {
29    Foo { ..g } = f;
30         // ^ error: functional record updates are not allowed in destructuring assignments
31    Foo { bar, ..g } = f;
32              // ^ error: functional record updates are not allowed in destructuring assignments
33    Foo { bar, baz, ..g } = f;
34                   // ^ error: functional record updates are not allowed in destructuring assignments
35}
36        "#,
37            // We don't end up using neither `bar` nor `baz`
38            &["unused_variables"],
39        );
40    }
41
42    #[test]
43    fn spread_default() {
44        check_diagnostics(
45            r#"
46struct Foo { bar: u32, baz: u32 }
47fn test(f: Foo) {
48    Foo { ..Default::default() } = f;
49         // ^^^^^^^^^^^^^^^^^^ error: functional record updates are not allowed in destructuring assignments
50}
51        "#,
52        );
53    }
54
55    #[test]
56    fn spread_struct() {
57        check_diagnostics(
58            r#"
59struct Foo { bar: u32, baz: u32 }
60fn test(f: Foo) {
61    Foo { ..Foo { bar: 0, baz: 0 } } = f;
62         // ^^^^^^^^^^^^^^^^^^^^^^ error: functional record updates are not allowed in destructuring assignments
63}
64        "#,
65        );
66    }
67}