Unnamed repository; edit this file 'description' to name the repository.
handle trait in function
Signed-off-by: Hayashi Mikihiro <[email protected]>
Hayashi Mikihiro 12 months ago
parent 55dd211 · commit 48027bf
-rw-r--r--crates/ide-assists/src/handlers/remove_unused_imports.rs44
1 files 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<SearchScope>,
+ 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,