diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-06-12 15:28:40 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-12 15:28:40 +0100 |
commit | b56ad148db0c69eb279c225f45d324b4e80e7367 (patch) | |
tree | e900043096a53b92b96ea4eec77d158bc4991717 /crates | |
parent | f3d73865d6ce6989018b3ebee8e199853253bfd7 (diff) | |
parent | 591b5ec2c15a83fd10da7049b5f3ea1a783d52ed (diff) |
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]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index f44feaf69..e1bfd72f9 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs | |||
@@ -187,7 +187,8 @@ fn check_struct_shorthand_initialization( | |||
187 | if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { | 187 | if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) { |
188 | let field_name = name_ref.syntax().text().to_string(); | 188 | let field_name = name_ref.syntax().text().to_string(); |
189 | let field_expr = expr.syntax().text().to_string(); | 189 | 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 { | ||
191 | let mut edit_builder = TextEditBuilder::default(); | 192 | let mut edit_builder = TextEditBuilder::default(); |
192 | edit_builder.delete(record_field.syntax().text_range()); | 193 | edit_builder.delete(record_field.syntax().text_range()); |
193 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); | 194 | edit_builder.insert(record_field.syntax().text_range().start(), field_name); |
@@ -719,6 +720,18 @@ mod tests { | |||
719 | "#, | 720 | "#, |
720 | check_struct_shorthand_initialization, | 721 | check_struct_shorthand_initialization, |
721 | ); | 722 | ); |
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 | ); | ||
722 | 735 | ||
723 | check_apply( | 736 | check_apply( |
724 | r#" | 737 | r#" |