diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-02-22 08:56:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-22 08:56:34 +0000 |
| commit | 071186f8e29792f7211a0249b454db9ef40f6605 (patch) | |
| tree | 847183aaf3161dfd0a6a24a4a88497395bd28d1c | |
| parent | 81af96d77a4940a6783b714072f7484a8545cdc0 (diff) | |
| parent | 1c3d6725e28e3e85acb804799e7daca02daeae86 (diff) | |
| download | rust-071186f8e29792f7211a0249b454db9ef40f6605.tar.gz rust-071186f8e29792f7211a0249b454db9ef40f6605.zip | |
Merge #11525
11525: fix(assist): Drop generic args in path before insert use in `replace_qualified_name_with_use` r=Veykril a=tysg Fixes #11505. also removed an unnecessary `clone_for_update` call. Co-authored-by: Tianyi Song <42670338+tysg@users.noreply.github.com>
| -rw-r--r-- | crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs | 46 |
1 files changed, 44 insertions, 2 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 3121e229817..71c674a8dd7 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 @@ -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! { @@ -392,4 +403,35 @@ fn main() { ", ); } + + #[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); +} +", + ); + } } |
