Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: fru-in-destructuring-assignment
//
// This diagnostic is triggered when a destructuring assignment contains functional record update
pub(crate) fn fru_in_destructuring_assignment(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::FruInDestructuringAssignment,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::SyntaxError,
        "functional record updates are not allowed in destructuring assignments",
        d.node.map(Into::into),
    )
    .stable()
}

#[cfg(test)]
mod tests {
    use crate::tests::{check_diagnostics, check_diagnostics_with_disabled};

    #[test]
    fn spread_variable() {
        check_diagnostics_with_disabled(
            r#"
struct Foo { bar: u32, baz: u32 }
fn test(f: Foo, g: Foo, mut bar: u32, mut baz: u32) {
    Foo { ..g } = f;
         // ^ error: functional record updates are not allowed in destructuring assignments
    Foo { bar, ..g } = f;
              // ^ error: functional record updates are not allowed in destructuring assignments
    Foo { bar, baz, ..g } = f;
                   // ^ error: functional record updates are not allowed in destructuring assignments
}
        "#,
            // We don't end up using neither `bar` nor `baz`
            &["unused_variables"],
        );
    }

    #[test]
    fn spread_default() {
        check_diagnostics(
            r#"
struct Foo { bar: u32, baz: u32 }
fn test(f: Foo) {
    Foo { ..Default::default() } = f;
         // ^^^^^^^^^^^^^^^^^^ error: functional record updates are not allowed in destructuring assignments
}
        "#,
        );
    }

    #[test]
    fn spread_struct() {
        check_diagnostics(
            r#"
struct Foo { bar: u32, baz: u32 }
fn test(f: Foo) {
    Foo { ..Foo { bar: 0, baz: 0 } } = f;
         // ^^^^^^^^^^^^^^^^^^^^^^ error: functional record updates are not allowed in destructuring assignments
}
        "#,
        );
    }
}