From 59f195a3231d9cbc3baac2c6ca8e6400311deeb7 Mon Sep 17 00:00:00 2001 From: OptimalStrategy Date: Fri, 12 Jun 2020 01:11:54 -0400 Subject: Fix invalid shorthand initialization diagnostic for tuple structs --- crates/ra_ide/src/diagnostics.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index f44feaf69..9cd36ad35 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -187,7 +187,12 @@ fn check_struct_shorthand_initialization( if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { let field_name = name_ref.syntax().text().to_string(); let field_expr = expr.syntax().text().to_string(); - if field_name == field_expr { + let field_name_is_tup_index = name_ref + .syntax() + .first_token() + .map(|token| token.kind().is_literal()) + .unwrap_or(false); + if field_name == field_expr && !field_name_is_tup_index { let mut edit_builder = TextEditBuilder::default(); edit_builder.delete(record_field.syntax().text_range()); edit_builder.insert(record_field.syntax().text_range().start(), field_name); @@ -719,6 +724,18 @@ mod tests { "#, check_struct_shorthand_initialization, ); + check_not_applicable( + r#" + struct A(usize); + + fn main() { + A { + 0: 0 + } + } + "#, + check_struct_shorthand_initialization, + ); check_apply( r#" -- cgit v1.2.3 From 591b5ec2c15a83fd10da7049b5f3ea1a783d52ed Mon Sep 17 00:00:00 2001 From: OptimalStrategy <17456182+OptimalStrategy@users.noreply.github.com> Date: Fri, 12 Jun 2020 10:16:19 -0400 Subject: simplify determining whether the field is a tuple field --- crates/ra_ide/src/diagnostics.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 9cd36ad35..e1bfd72f9 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -187,11 +187,7 @@ fn check_struct_shorthand_initialization( if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { let field_name = name_ref.syntax().text().to_string(); let field_expr = expr.syntax().text().to_string(); - let field_name_is_tup_index = name_ref - .syntax() - .first_token() - .map(|token| token.kind().is_literal()) - .unwrap_or(false); + let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); if field_name == field_expr && !field_name_is_tup_index { let mut edit_builder = TextEditBuilder::default(); edit_builder.delete(record_field.syntax().text_range()); -- cgit v1.2.3