fix: initializing struct multiple times

This commit is contained in:
Ryan Mehri 2023-09-08 07:45:23 -07:00 committed by Ryan Mehri
parent 83196fd4d9
commit 91ac1d6194

View file

@ -194,6 +194,7 @@ fn replace_usages(edit: &mut SourceChangeBuilder, usages: &UsageSearchResult) {
ast::NameLike::NameRef(name) => Some((*range, name)),
_ => None,
})
.rev()
.for_each(|(range, name_ref)| {
if let Some(initializer) = find_assignment_usage(name_ref) {
cov_mark::hit!(replaces_assignment);
@ -615,6 +616,46 @@ mod foo {
)
}
#[test]
fn field_multiple_initializations() {
check_assist(
bool_to_enum,
r#"
struct Foo {
$0bar: bool,
baz: bool,
}
fn main() {
let foo1 = Foo { bar: true, baz: false };
let foo2 = Foo { bar: false, baz: false };
if foo1.bar && foo2.bar {
println!("foo");
}
}
"#,
r#"
#[derive(PartialEq, Eq)]
enum $0Bool { True, False }
struct Foo {
bar: Bool,
baz: bool,
}
fn main() {
let foo1 = Foo { bar: Bool::True, baz: false };
let foo2 = Foo { bar: Bool::False, baz: false };
if foo1.bar == Bool::True && foo2.bar == Bool::True {
println!("foo");
}
}
"#,
)
}
#[test]
fn field_non_bool() {
cov_mark::check!(not_applicable_non_bool_field);