Auto merge of #12789 - DorianListens:dscheidt/unused-param-overlapping, r=DorianListens

fix: Prevent panic in Remove Unused Parameter assist

Instead of calling `builder.delete` for every text range we find with
`process_usage`, we now ensure that the ranges do not overlap before removing
them. If a range is fully contained by a prior one, it is dropped.

fixes #12784
This commit is contained in:
bors 2022-07-19 22:36:08 +00:00
commit 30c4db10ab

View File

@ -96,12 +96,20 @@ fn process_usages(
) {
let source_file = ctx.sema.parse(file_id);
builder.edit_file(file_id);
for usage in references {
if let Some(text_range) = process_usage(&source_file, usage, arg_to_remove, is_self_present)
{
builder.delete(text_range);
let possible_ranges = references
.into_iter()
.filter_map(|usage| process_usage(&source_file, usage, arg_to_remove, is_self_present));
let mut ranges_to_delete: Vec<TextRange> = vec![];
for range in possible_ranges {
if !ranges_to_delete.iter().any(|it| it.contains_range(range)) {
ranges_to_delete.push(range)
}
}
for range in ranges_to_delete {
builder.delete(range)
}
}
fn process_usage(
@ -370,6 +378,31 @@ fn main() {
S.f(92);
S::f(&S);
}
"#,
)
}
#[test]
fn nested_call() {
check_assist(
remove_unused_param,
r#"
fn foo(x: i32, $0y: i32) -> i32 {
x
}
fn bar() {
foo(1, foo(2, 3));
}
"#,
r#"
fn foo(x: i32) -> i32 {
x
}
fn bar() {
foo(1);
}
"#,
)
}