]> git.lizzy.rs Git - rust.git/commitdiff
Drop generic args in path before insert use
authorTianyi Song <42670338+tysg@users.noreply.github.com>
Fri, 28 Jan 2022 08:15:23 +0000 (16:15 +0800)
committerTianyi Song <42670338+tysg@users.noreply.github.com>
Tue, 22 Feb 2022 07:41:26 +0000 (15:41 +0800)
crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs

index 3121e2298178068fbf5d041f986c81cf1d237c01..71c674a8dd745fdad9641239d8290f6941c9c90c 100644 (file)
@@ -84,7 +84,8 @@ pub(crate) fn replace_qualified_name_with_use(
                 ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
                 ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
             };
-            shorten_paths(scope.as_syntax_node(), &path.clone_for_update());
+            shorten_paths(scope.as_syntax_node(), &path);
+            let path = drop_generic_args(&path);
             // stick the found import in front of the to be replaced path
             let path = match path_to_qualifier.and_then(|it| mod_path_to_ast(&it).qualifier()) {
                 Some(qualifier) => make::path_concat(qualifier, path),
@@ -95,7 +96,17 @@ pub(crate) fn replace_qualified_name_with_use(
     )
 }
 
-/// Adds replacements to `re` that shorten `path` in all descendants of `node`.
+fn drop_generic_args(path: &ast::Path) -> ast::Path {
+    let path = path.clone_for_update();
+    if let Some(segment) = path.segment() {
+        if let Some(generic_args) = segment.generic_arg_list() {
+            ted::remove(generic_args.syntax());
+        }
+    }
+    path
+}
+
+/// Mutates `node` to shorten `path` in all descendants of `node`.
 fn shorten_paths(node: &SyntaxNode, path: &ast::Path) {
     for child in node.children() {
         match_ast! {
@@ -389,6 +400,37 @@ pub fn drop<T>(_: T) {}
 fn main() {
     drop(0);
 }
+",
+        );
+    }
+
+    #[test]
+    fn replace_should_drop_generic_args_in_use() {
+        check_assist(
+            replace_qualified_name_with_use,
+            r"
+mod std {
+    pub mod mem {
+        pub fn drop<T>(_: T) {}
+    }
+}
+
+fn main() {
+    std::mem::drop::<usize>$0(0);
+}
+",
+            r"
+use std::mem::drop;
+
+mod std {
+    pub mod mem {
+        pub fn drop<T>(_: T) {}
+    }
+}
+
+fn main() {
+    drop::<usize>(0);
+}
 ",
         );
     }