From 48027bfc197790a28ee857925512bf7b4a8d18d2 Mon Sep 17 00:00:00 2001 From: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> Date: Thu, 15 May 2025 15:12:31 +0900 Subject: [PATCH] handle trait in function Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com> --- .../src/handlers/remove_unused_imports.rs | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/crates/ide-assists/src/handlers/remove_unused_imports.rs b/crates/ide-assists/src/handlers/remove_unused_imports.rs index 994e7c446c..16debc4d72 100644 --- a/crates/ide-assists/src/handlers/remove_unused_imports.rs +++ b/crates/ide-assists/src/handlers/remove_unused_imports.rs @@ -103,29 +103,12 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>) }) .any(|d| used_once_in_scope(ctx, d, u.rename(), scope)) { - return Some(u); + Some(u) } else { - return None; - } - } - match res { - PathResolutionPerNs { - type_ns: Some(PathResolution::Def(ModuleDef::Trait(ref t))), - value_ns, - macro_ns, - } => { - // If the trait or any item is used. - if is_trait_unused_in_scope(ctx, &u, scope, t) { - let path = [value_ns, macro_ns]; - is_path_unused_in_scope(ctx, &u, scope, &path).then_some(u) - } else { - None - } - } - PathResolutionPerNs { type_ns, value_ns, macro_ns } => { - let path = [type_ns, value_ns, macro_ns]; - is_path_unused_in_scope(ctx, &u, scope, &path).then_some(u) + None } + } else { + is_path_per_ns_unused_in_scope(ctx, &u, scope, &res).then_some(u) } }) .peekable(); @@ -148,6 +131,25 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>) } } +fn is_path_per_ns_unused_in_scope( + ctx: &AssistContext<'_>, + u: &ast::UseTree, + scope: &mut Vec, + path: &PathResolutionPerNs, +) -> bool { + if let Some(PathResolution::Def(ModuleDef::Trait(ref t))) = path.type_ns { + if is_trait_unused_in_scope(ctx, u, scope, t) { + let path = [path.value_ns, path.macro_ns]; + is_path_unused_in_scope(ctx, u, scope, &path) + } else { + false + } + } else { + let path = [path.type_ns, path.value_ns, path.macro_ns]; + is_path_unused_in_scope(ctx, u, scope, &path) + } +} + fn is_path_unused_in_scope( ctx: &AssistContext<'_>, u: &ast::UseTree,