From 2c1777a2e264e58fccd5ace94b238c8a497ddbda Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 12 Jan 2021 15:51:02 +0100 Subject: Ensure uniqueness of file ids in reference search via hashmap --- .../src/handlers/extract_struct_from_enum_variant.rs | 6 +++--- crates/assists/src/handlers/inline_local_variable.rs | 14 +++++++------- crates/assists/src/handlers/remove_unused_param.rs | 18 ++++++++---------- 3 files changed, 18 insertions(+), 20 deletions(-) (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index 21b13977b..e3ef04932 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs @@ -8,7 +8,7 @@ use ide_db::{ insert_use::{insert_use, ImportScope}, mod_path_to_ast, }, - search::{FileReference, FileReferences}, + search::FileReference, RootDatabase, }; use rustc_hash::FxHashSet; @@ -63,10 +63,10 @@ pub(crate) fn extract_struct_from_enum_variant( let current_module = enum_hir.module(ctx.db()); visited_modules_set.insert(current_module); let mut def_rewriter = None; - for FileReferences { file_id, references: refs } in usages { + for (file_id, references) in usages { let mut rewriter = SyntaxRewriter::default(); let source_file = ctx.sema.parse(file_id); - for reference in refs { + for reference in references { update_reference( ctx, &mut rewriter, diff --git a/crates/assists/src/handlers/inline_local_variable.rs b/crates/assists/src/handlers/inline_local_variable.rs index 928df6825..dc798daaa 100644 --- a/crates/assists/src/handlers/inline_local_variable.rs +++ b/crates/assists/src/handlers/inline_local_variable.rs @@ -47,8 +47,8 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O let def = ctx.sema.to_def(&bind_pat)?; let def = Definition::Local(def); - let refs = def.usages(&ctx.sema).all(); - if refs.is_empty() { + let usages = def.usages(&ctx.sema).all(); + if usages.is_empty() { mark::hit!(test_not_applicable_if_variable_unused); return None; }; @@ -66,9 +66,10 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O let_stmt.syntax().text_range() }; - let wrap_in_parens = refs - .iter() - .flat_map(|refs| &refs.references) + let wrap_in_parens = usages + .references + .values() + .flatten() .map(|&FileReference { range, .. }| { let usage_node = ctx.covering_node_for_range(range).ancestors().find_map(ast::PathExpr::cast)?; @@ -115,8 +116,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O target, move |builder| { builder.delete(delete_range); - for (reference, should_wrap) in - refs.iter().flat_map(|refs| &refs.references).zip(wrap_in_parens) + for (reference, should_wrap) in usages.references.values().flatten().zip(wrap_in_parens) { let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() }; diff --git a/crates/assists/src/handlers/remove_unused_param.rs b/crates/assists/src/handlers/remove_unused_param.rs index 4f3b8ac46..c961680e2 100644 --- a/crates/assists/src/handlers/remove_unused_param.rs +++ b/crates/assists/src/handlers/remove_unused_param.rs @@ -1,7 +1,4 @@ -use ide_db::{ - defs::Definition, - search::{FileReference, FileReferences}, -}; +use ide_db::{base_db::FileId, defs::Definition, search::FileReference}; use syntax::{ algo::find_node_at_range, ast::{self, ArgListOwner}, @@ -61,8 +58,8 @@ pub(crate) fn remove_unused_param(acc: &mut Assists, ctx: &AssistContext) -> Opt param.syntax().text_range(), |builder| { builder.delete(range_to_remove(param.syntax())); - for usages in fn_def.usages(&ctx.sema).all() { - process_usages(ctx, builder, usages, param_position); + for (file_id, references) in fn_def.usages(&ctx.sema).all() { + process_usages(ctx, builder, file_id, references, param_position); } }, ) @@ -71,12 +68,13 @@ pub(crate) fn remove_unused_param(acc: &mut Assists, ctx: &AssistContext) -> Opt fn process_usages( ctx: &AssistContext, builder: &mut AssistBuilder, - usages: FileReferences, + file_id: FileId, + references: Vec, arg_to_remove: usize, ) { - let source_file = ctx.sema.parse(usages.file_id); - builder.edit_file(usages.file_id); - for usage in usages.references { + 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) { builder.delete(text_range); } -- cgit v1.2.3