Unnamed repository; edit this file 'description' to name the repository.
| -rw-r--r-- | crates/ide-assists/src/handlers/generate_new.rs | 91 | ||||
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/missing_fields.rs | 86 |
2 files changed, 177 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/generate_new.rs b/crates/ide-assists/src/handlers/generate_new.rs index 4ed241f1af..5f4715bb53 100644 --- a/crates/ide-assists/src/handlers/generate_new.rs +++ b/crates/ide-assists/src/handlers/generate_new.rs @@ -124,6 +124,97 @@ mod tests { use super::*; #[test] + fn test_generate_new_with_zst_fields() { + check_assist( + generate_new, + r#" +struct Empty; + +struct Foo { empty: Empty $0} +"#, + r#" +struct Empty; + +struct Foo { empty: Empty } + +impl Foo { + fn $0new() -> Self { Self { empty: Empty } } +} +"#, + ); + check_assist( + generate_new, + r#" +struct Empty; + +struct Foo { baz: String, empty: Empty $0} +"#, + r#" +struct Empty; + +struct Foo { baz: String, empty: Empty } + +impl Foo { + fn $0new(baz: String) -> Self { Self { baz, empty: Empty } } +} +"#, + ); + check_assist( + generate_new, + r#" +enum Empty { Bar } + +struct Foo { empty: Empty $0} +"#, + r#" +enum Empty { Bar } + +struct Foo { empty: Empty } + +impl Foo { + fn $0new() -> Self { Self { empty: Empty::Bar } } +} +"#, + ); + + // make sure the assist only works on unit variants + check_assist( + generate_new, + r#" +struct Empty {} + +struct Foo { empty: Empty $0} +"#, + r#" +struct Empty {} + +struct Foo { empty: Empty } + +impl Foo { + fn $0new(empty: Empty) -> Self { Self { empty } } +} +"#, + ); + check_assist( + generate_new, + r#" +enum Empty { Bar {} } + +struct Foo { empty: Empty $0} +"#, + r#" +enum Empty { Bar {} } + +struct Foo { empty: Empty } + +impl Foo { + fn $0new(empty: Empty) -> Self { Self { empty } } +} +"#, + ); + } + + #[test] fn test_generate_new() { check_assist( generate_new, diff --git a/crates/ide-diagnostics/src/handlers/missing_fields.rs b/crates/ide-diagnostics/src/handlers/missing_fields.rs index 828d922982..30f903af50 100644 --- a/crates/ide-diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide-diagnostics/src/handlers/missing_fields.rs @@ -346,6 +346,92 @@ fn test_fn() { } #[test] + fn test_fill_struct_zst_fields() { + check_fix( + r#" +struct Empty; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct {$0}; +} +"#, + r#" +struct Empty; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct { one: 0, two: Empty }; +} +"#, + ); + check_fix( + r#" +enum Empty { Foo }; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct {$0}; +} +"#, + r#" +enum Empty { Foo }; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct { one: 0, two: Empty::Foo }; +} +"#, + ); + + // make sure the assist doesn't fill non Unit variants + check_fix( + r#" +struct Empty {}; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct {$0}; +} +"#, + r#" +struct Empty {}; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct { one: 0, two: todo!() }; +} +"#, + ); + check_fix( + r#" +enum Empty { Foo {} }; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct {$0}; +} +"#, + r#" +enum Empty { Foo {} }; + +struct TestStruct { one: i32, two: Empty } + +fn test_fn() { + let s = TestStruct { one: 0, two: todo!() }; +} +"#, + ); + } + + #[test] fn test_fill_struct_fields_self() { check_fix( r#" |