Skip to content

Commit

Permalink
Auto merge of rust-lang#12789 - DorianListens:dscheidt/unused-param-o…
Browse files Browse the repository at this point in the history
…verlapping, 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 rust-lang#12784
  • Loading branch information
bors committed Jul 19, 2022
2 parents 7d20ff3 + ffb6b23 commit 30c4db1
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions crates/ide-assists/src/handlers/remove_unused_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
"#,
)
}
Expand Down

0 comments on commit 30c4db1

Please sign in to comment.