@@ -6,7 +6,10 @@ use ide_db::{
6
6
search:: { FileReference , ReferenceCategory , SearchScope } ,
7
7
FxHashMap , RootDatabase ,
8
8
} ;
9
- use syntax:: { ast, AstNode } ;
9
+ use syntax:: {
10
+ ast:: { self , Rename } ,
11
+ AstNode ,
12
+ } ;
10
13
use text_edit:: TextRange ;
11
14
12
15
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
@@ -100,19 +103,19 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>)
100
103
hir:: ScopeDef :: ModuleDef ( d) => Some ( Definition :: from ( * d) ) ,
101
104
_ => None ,
102
105
} )
103
- . any ( |d| used_once_in_scope ( ctx, d, scope) )
106
+ . any ( |d| used_once_in_scope ( ctx, d, u . rename ( ) , scope) )
104
107
{
105
108
return Some ( u) ;
106
109
}
107
110
} else if let Definition :: Trait ( ref t) = def {
108
111
// If the trait or any item is used.
109
- if !std:: iter:: once ( def)
110
- . chain ( t. items ( ctx. db ( ) ) . into_iter ( ) . map ( Definition :: from ) )
111
- . any ( |d | used_once_in_scope ( ctx, d, scope) )
112
+ if !std:: iter:: once ( ( def, u . rename ( ) ) )
113
+ . chain ( t. items ( ctx. db ( ) ) . into_iter ( ) . map ( |item| ( item . into ( ) , None ) ) )
114
+ . any ( |( d , rename ) | used_once_in_scope ( ctx, d, rename , scope) )
112
115
{
113
116
return Some ( u) ;
114
117
}
115
- } else if !used_once_in_scope ( ctx, def, scope) {
118
+ } else if !used_once_in_scope ( ctx, def, u . rename ( ) , scope) {
116
119
return Some ( u) ;
117
120
}
118
121
@@ -138,7 +141,12 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>)
138
141
}
139
142
}
140
143
141
- fn used_once_in_scope ( ctx : & AssistContext < ' _ > , def : Definition , scopes : & Vec < SearchScope > ) -> bool {
144
+ fn used_once_in_scope (
145
+ ctx : & AssistContext < ' _ > ,
146
+ def : Definition ,
147
+ rename : Option < Rename > ,
148
+ scopes : & Vec < SearchScope > ,
149
+ ) -> bool {
142
150
let mut found = false ;
143
151
144
152
for scope in scopes {
@@ -151,7 +159,10 @@ fn used_once_in_scope(ctx: &AssistContext<'_>, def: Definition, scopes: &Vec<Sea
151
159
false
152
160
}
153
161
} ;
154
- def. usages ( & ctx. sema ) . in_scope ( scope) . search ( & mut search_non_import) ;
162
+ def. usages ( & ctx. sema )
163
+ . in_scope ( scope)
164
+ . with_rename ( rename. as_ref ( ) )
165
+ . search ( & mut search_non_import) ;
155
166
if found {
156
167
break ;
157
168
}
@@ -330,7 +341,7 @@ fn w() {
330
341
}
331
342
332
343
#[ test]
333
- fn ranamed_trait_item_use_is_use ( ) {
344
+ fn renamed_trait_item_use_is_use ( ) {
334
345
check_assist_not_applicable (
335
346
remove_unused_imports,
336
347
r#"
@@ -356,7 +367,7 @@ fn w() {
356
367
}
357
368
358
369
#[ test]
359
- fn ranamed_underscore_trait_item_use_is_use ( ) {
370
+ fn renamed_underscore_trait_item_use_is_use ( ) {
360
371
check_assist_not_applicable (
361
372
remove_unused_imports,
362
373
r#"
@@ -942,6 +953,62 @@ pub struct X();
942
953
mod z {
943
954
mod foo;
944
955
}
956
+ "# ,
957
+ ) ;
958
+ }
959
+
960
+ #[ test]
961
+ fn use_as_alias ( ) {
962
+ check_assist_not_applicable (
963
+ remove_unused_imports,
964
+ r#"
965
+ mod foo {
966
+ pub struct Foo {}
967
+ }
968
+
969
+ use foo::Foo as Bar$0;
970
+
971
+ fn test(_: Bar) {}
972
+ "# ,
973
+ ) ;
974
+
975
+ check_assist (
976
+ remove_unused_imports,
977
+ r#"
978
+ mod foo {
979
+ pub struct Foo {}
980
+ pub struct Bar {}
981
+ pub struct Qux {}
982
+ pub trait Quux {
983
+ fn quxx(&self) {}
984
+ }
985
+ impl<T> Quxx for T {}
986
+ }
987
+
988
+ use foo::{Foo as Bar, Bar as Baz, Qux as _, Quxx as _}$0;
989
+
990
+ fn test(_: Bar) {
991
+ let a = ();
992
+ a.quxx();
993
+ }
994
+ "# ,
995
+ r#"
996
+ mod foo {
997
+ pub struct Foo {}
998
+ pub struct Bar {}
999
+ pub struct Qux {}
1000
+ pub trait Quux {
1001
+ fn quxx(&self) {}
1002
+ }
1003
+ impl<T> Quxx for T {}
1004
+ }
1005
+
1006
+ use foo::{Foo as Bar, Quxx as _};
1007
+
1008
+ fn test(_: Bar) {
1009
+ let a = ();
1010
+ a.quxx();
1011
+ }
945
1012
"# ,
946
1013
) ;
947
1014
}
0 commit comments