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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: duplicate-field
//
// This diagnostic is triggered when a record expression or pattern specifies
// the same field more than once.
pub(crate) fn duplicate_field(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::DuplicateField,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0062"),
        "field specified more than once",
        d.field.map(Into::into),
    )
    .stable()
}

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

    #[test]
    fn duplicate_field_in_struct_literal() {
        check_diagnostics(
            r#"
struct S { foo: i32, bar: i32 }
fn main() {
    let _ = S {
        foo: 1,
        bar: 2,
        foo: 3,
      //^^^^^^ error: field specified more than once
    };
}
"#,
        );
    }

    #[test]
    fn duplicate_field_in_enum_variant_literal() {
        check_diagnostics(
            r#"
enum E { V { foo: i32 } }
fn main() {
    let _ = E::V {
        foo: 1,
        foo: 2,
      //^^^^^^ error: field specified more than once
    };
}
"#,
        );
    }

    #[test]
    fn no_duplicate_when_each_field_specified_once() {
        check_diagnostics(
            r#"
struct S { foo: i32, bar: i32 }
fn main() {
    let _ = S { foo: 1, bar: 2 };
}
"#,
        );
    }

    #[test]
    fn no_duplicate_for_unknown_field_falls_through_to_no_such_field() {
        check_diagnostics(
            r#"
struct S { foo: i32 }
fn main() {
    let _ = S {
        foo: 1,
        bar: 2,
      //^^^^^^ 💡 error: no such field
    };
}
"#,
        );
    }
}