Unnamed repository; edit this file 'description' to name the repository.
fix: don't offer `replace_qualified_name_with_use` on an unqualified path
shulaoda 2 weeks ago
parent b5aa666 · commit b546cf0
-rw-r--r--crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs
index 0bd1ec12d0..d7f00bf669 100644
--- a/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs
+++ b/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs
@@ -40,6 +40,12 @@ pub(crate) fn replace_qualified_name_with_use(
let original_path = target_path(ctx, original_path)?;
+ // There is no qualifier to replace, so there is nothing for this assist to do
+ if original_path.qualifier().is_none() {
+ cov_mark::hit!(not_applicable_for_unqualified_path);
+ return None;
+ }
+
// then search for an import for the first path segment of what we want to replace
// that way it is less likely that we import the item from a different location due re-exports
let module = match ctx.sema.resolve_path(&original_path.first_qualifier_or_self())? {
@@ -251,6 +257,22 @@ fs::Path
}
#[test]
+ fn test_replace_not_applicable_for_unqualified_path() {
+ cov_mark::check!(not_applicable_for_unqualified_path);
+ check_assist_not_applicable(
+ replace_qualified_name_with_use,
+ r"
+//- /main.rs crate:main deps:sub
+fn main() {
+ su$0b();
+}
+//- /sub.rs crate:sub
+pub fn sub() {}
+",
+ );
+ }
+
+ #[test]
fn replaces_all_affected_paths() {
check_assist(
replace_qualified_name_with_use,