From ffb6b23c75add6192a133b15be946cc2199ed971 Mon Sep 17 00:00:00 2001 From: Dorian Scheidt Date: Sun, 17 Jul 2022 16:45:10 -0500 Subject: [PATCH] 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 --- .../src/handlers/remove_unused_param.rs | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/crates/ide-assists/src/handlers/remove_unused_param.rs b/crates/ide-assists/src/handlers/remove_unused_param.rs index 80e2ca918b94b..928a5a386eb11 100644 --- a/crates/ide-assists/src/handlers/remove_unused_param.rs +++ b/crates/ide-assists/src/handlers/remove_unused_param.rs @@ -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 = 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); +} "#, ) }