Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/duplicate_field.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/duplicate_field.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/duplicate_field.rs b/crates/ide-diagnostics/src/handlers/duplicate_field.rs
new file mode 100644
index 0000000000..13d594066d
--- /dev/null
+++ b/crates/ide-diagnostics/src/handlers/duplicate_field.rs
@@ -0,0 +1,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
+ };
+}
+"#,
+ );
+ }
+}