Skip to content

Commit b56ad14

Browse files
Merge #4857
4857: Fix invalid shorthand initialization diagnostic for tuple structs r=jonas-schievink a=OptimalStrategy Initializing tuple structs explicitly, like in the example below, produces a "Shorthand struct initialization" diagnostic that leads to a compilation error when applied: ```rust struct S(usize); fn main() { let s = S { 0: 0 }; // OK, but triggers the diagnostic // let s = S { 0 }; // Compilation error } ``` This PR adds a check that the field name is not a literal. Co-authored-by: OptimalStrategy <[email protected]> Co-authored-by: OptimalStrategy <[email protected]>
2 parents f3d7386 + 591b5ec commit b56ad14

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

crates/ra_ide/src/diagnostics.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ fn check_struct_shorthand_initialization(
187187
if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) {
188188
let field_name = name_ref.syntax().text().to_string();
189189
let field_expr = expr.syntax().text().to_string();
190-
if field_name == field_expr {
190+
let field_name_is_tup_index = name_ref.as_tuple_field().is_some();
191+
if field_name == field_expr && !field_name_is_tup_index {
191192
let mut edit_builder = TextEditBuilder::default();
192193
edit_builder.delete(record_field.syntax().text_range());
193194
edit_builder.insert(record_field.syntax().text_range().start(), field_name);
@@ -719,6 +720,18 @@ mod tests {
719720
"#,
720721
check_struct_shorthand_initialization,
721722
);
723+
check_not_applicable(
724+
r#"
725+
struct A(usize);
726+
727+
fn main() {
728+
A {
729+
0: 0
730+
}
731+
}
732+
"#,
733+
check_struct_shorthand_initialization,
734+
);
722735

723736
check_apply(
724737
r#"

0 commit comments

Comments
 (0)