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
@@ -96,11 +96,19 @@ 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)
}
}
@@ -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);
+}
"#,
)
}
