Unnamed repository; edit this file 'description' to name the repository.
fix: wrap ty-anchor in non-path type constuctor
Example
---
```rust
struct TestStruct { r: &'static [i32] }
fn test_fn() {
let s = TestStruct{ $0 };
}
```
**Before this PR**
```rust
struct TestStruct { r: &'static [i32] }
fn test_fn() {
let s = TestStruct{ r: &'static };
}
```
**After this PR**
```rust
struct TestStruct { r: &'static [i32] }
fn test_fn() {
let s = TestStruct{ r: <&'static [i32]>::default() };
}
```
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/missing_fields.rs | 8 | ||||
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 12 | ||||
| -rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 4 |
3 files changed, 18 insertions, 6 deletions
diff --git a/crates/ide-diagnostics/src/handlers/missing_fields.rs b/crates/ide-diagnostics/src/handlers/missing_fields.rs index d5f25dfaf2..050d5477f6 100644 --- a/crates/ide-diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide-diagnostics/src/handlers/missing_fields.rs @@ -588,14 +588,14 @@ fn test_fn() { fn test_fill_struct_fields_default() { check_fix( r#" -//- minicore: default, option +//- minicore: default, option, slice struct TestWithDefault(usize); impl Default for TestWithDefault { pub fn default() -> Self { Self(0) } } -struct TestStruct { one: i32, two: TestWithDefault } +struct TestStruct { one: i32, two: TestWithDefault, r: &'static [i32] } fn test_fn() { let s = TestStruct{ $0 }; @@ -608,10 +608,10 @@ impl Default for TestWithDefault { Self(0) } } -struct TestStruct { one: i32, two: TestWithDefault } +struct TestStruct { one: i32, two: TestWithDefault, r: &'static [i32] } fn test_fn() { - let s = TestStruct{ one: 0, two: TestWithDefault::default() }; + let s = TestStruct{ one: 0, two: TestWithDefault::default(), r: <&'static [i32]>::default() }; } ", ); diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index f5d1d009a5..00971569a2 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -74,10 +74,18 @@ pub mod ext { expr_from_text("_") } pub fn expr_ty_default(ty: &ast::Type) -> ast::Expr { - expr_from_text(&format!("{ty}::default()")) + if !ty.needs_angles_in_path() { + expr_from_text(&format!("{ty}::default()")) + } else { + expr_from_text(&format!("<{ty}>::default()")) + } } pub fn expr_ty_new(ty: &ast::Type) -> ast::Expr { - expr_from_text(&format!("{ty}::new()")) + if !ty.needs_angles_in_path() { + expr_from_text(&format!("{ty}::new()")) + } else { + expr_from_text(&format!("<{ty}>::new()")) + } } pub fn expr_self() -> ast::Expr { expr_from_text("self") diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 76cfea9d5b..63e4608d0f 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -717,6 +717,10 @@ impl ast::Type { None } } + + pub fn needs_angles_in_path(&self) -> bool { + !matches!(self, ast::Type::PathType(_)) || self.generic_arg_list().is_some() + } } #[derive(Debug, Clone, PartialEq, Eq)] |