@@ -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,11 +141,19 @@ 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
152
+ dbg ! ( & def) ;
153
+
144
154
for scope in scopes {
145
155
let mut search_non_import = |_, r : FileReference | {
156
+ dbg ! ( & r) ;
146
157
// The import itself is a use; we must skip that.
147
158
if !r. category . contains ( ReferenceCategory :: IMPORT ) {
148
159
found = true ;
@@ -151,7 +162,10 @@ fn used_once_in_scope(ctx: &AssistContext<'_>, def: Definition, scopes: &Vec<Sea
151
162
false
152
163
}
153
164
} ;
154
- def. usages ( & ctx. sema ) . in_scope ( scope) . search ( & mut search_non_import) ;
165
+ def. usages ( & ctx. sema )
166
+ . in_scope ( scope)
167
+ . with_rename ( rename. as_ref ( ) )
168
+ . search ( & mut search_non_import) ;
155
169
if found {
156
170
break ;
157
171
}
@@ -330,7 +344,7 @@ fn w() {
330
344
}
331
345
332
346
#[ test]
333
- fn ranamed_trait_item_use_is_use ( ) {
347
+ fn renamed_trait_item_use_is_use ( ) {
334
348
check_assist_not_applicable (
335
349
remove_unused_imports,
336
350
r#"
@@ -356,7 +370,7 @@ fn w() {
356
370
}
357
371
358
372
#[ test]
359
- fn ranamed_underscore_trait_item_use_is_use ( ) {
373
+ fn renamed_underscore_trait_item_use_is_use ( ) {
360
374
check_assist_not_applicable (
361
375
remove_unused_imports,
362
376
r#"
@@ -942,6 +956,62 @@ pub struct X();
942
956
mod z {
943
957
mod foo;
944
958
}
959
+ "# ,
960
+ ) ;
961
+ }
962
+
963
+ #[ test]
964
+ fn use_as_alias ( ) {
965
+ check_assist_not_applicable (
966
+ remove_unused_imports,
967
+ r#"
968
+ mod foo {
969
+ pub struct Foo {}
970
+ }
971
+
972
+ use foo::Foo as Bar$0;
973
+
974
+ fn test(_: Bar) {}
975
+ "# ,
976
+ ) ;
977
+
978
+ check_assist (
979
+ remove_unused_imports,
980
+ r#"
981
+ mod foo {
982
+ pub struct Foo {}
983
+ pub struct Bar {}
984
+ pub struct Qux {}
985
+ pub trait Quux {
986
+ fn quxx(&self) {}
987
+ }
988
+ impl<T> Quxx for T {}
989
+ }
990
+
991
+ use foo::{Foo as Bar, Bar as Baz, Qux as _, Quxx as _}$0;
992
+
993
+ fn test(_: Bar) {
994
+ let a = ();
995
+ a.quxx();
996
+ }
997
+ "# ,
998
+ r#"
999
+ mod foo {
1000
+ pub struct Foo {}
1001
+ pub struct Bar {}
1002
+ pub struct Qux {}
1003
+ pub trait Quux {
1004
+ fn quxx(&self) {}
1005
+ }
1006
+ impl<T> Quxx for T {}
1007
+ }
1008
+
1009
+ use foo::{Foo as Bar, Quxx as _};
1010
+
1011
+ fn test(_: Bar) {
1012
+ let a = ();
1013
+ a.quxx();
1014
+ }
945
1015
"# ,
946
1016
) ;
947
1017
}
0 commit comments