Unnamed repository; edit this file 'description' to name the repository.
Fix `expand_glob_import` assist to not include items from the current module when expanding a glob import, which can cause cyclic imports.
Kao-Wei Yeh 2 weeks ago
parent fb2288f · commit b42d2de
-rw-r--r--crates/ide-assists/src/handlers/expand_glob_import.rs33
1 files changed, 32 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/expand_glob_import.rs b/crates/ide-assists/src/handlers/expand_glob_import.rs
index 24ffc6787d..f5d2a6cfc5 100644
--- a/crates/ide-assists/src/handlers/expand_glob_import.rs
+++ b/crates/ide-assists/src/handlers/expand_glob_import.rs
@@ -162,7 +162,8 @@ fn build_expanded_import(
}
};
- let refs_in_target = find_refs_in_mod(ctx, target_module, visible_from, must_be_pub);
+ let refs_in_target =
+ find_refs_in_mod(ctx, target_module, current_module, visible_from, must_be_pub);
let imported_defs = find_imported_defs(ctx, use_item);
let filtered_defs =
@@ -303,6 +304,7 @@ impl Refs {
fn find_refs_in_mod(
ctx: &AssistContext<'_, '_>,
expandable: Expandable,
+ current_module: Module,
visible_from: Module,
must_be_pub: bool,
) -> Refs {
@@ -312,6 +314,10 @@ fn find_refs_in_mod(
let refs = module_scope
.into_iter()
.filter_map(|(n, d)| Ref::from_scope_def(ctx, n, d))
+ .filter(|r| match r.def {
+ Definition::Module(it) => it != current_module,
+ _ => r.def.module(ctx.db()).map_or(false, |it| it != current_module),
+ })
.filter(|r| !must_be_pub || r.is_pub)
.collect();
Refs(refs)
@@ -444,6 +450,31 @@ fn qux(bar: Bar, baz: Baz) {
}
#[test]
+ fn expanding_glob_import_on_cycle_import() {
+ check_assist(
+ expand_glob_import,
+ r"
+mod foo {
+ pub use crate::*$0;
+ pub struct Foo;
+ pub fn bar() -> Bar { _ = Foo; Bar }
+}
+pub use foo::*;
+pub struct Bar;
+",
+ r"
+mod foo {
+ pub use crate::Bar;
+ pub struct Foo;
+ pub fn bar() -> Bar { _ = Foo; Bar }
+}
+pub use foo::*;
+pub struct Bar;
+",
+ )
+ }
+
+ #[test]
fn expanding_glob_import_unused() {
check_assist(
expand_glob_import,