|
1 | | -use hir::AssocItem; |
2 | 1 | use ide_db::{ |
3 | 2 | assists::{AssistId, AssistKind}, |
4 | 3 | base_db::FileId, |
5 | 4 | defs::Definition, |
6 | 5 | search::FileReference, |
7 | 6 | syntax_helpers::node_ext::full_path_of_name_ref, |
8 | | - traits::resolve_target_trait, |
9 | 7 | }; |
10 | 8 | use syntax::{ |
11 | | - ast::{self, HasName, NameLike, NameRef}, |
| 9 | + ast::{self, NameLike, NameRef}, |
12 | 10 | AstNode, SyntaxKind, TextRange, |
13 | 11 | }; |
14 | 12 |
|
@@ -46,16 +44,13 @@ pub(crate) fn unnecessary_async(acc: &mut Assists, ctx: &AssistContext<'_>) -> O |
46 | 44 | if function.body()?.syntax().descendants().find_map(ast::AwaitExpr::cast).is_some() { |
47 | 45 | return None; |
48 | 46 | } |
49 | | - // Do nothing if the method is an async member of trait. |
50 | | - if let Some(fname) = function.name() { |
51 | | - if let Some(trait_item) = find_corresponding_trait_member(ctx, fname.to_string()) { |
52 | | - if let AssocItem::Function(method) = trait_item { |
53 | | - if method.is_async(ctx.db()) { |
54 | | - return None; |
55 | | - } |
56 | | - } |
| 47 | + // Do nothing if the method is a member of trait. |
| 48 | + if let Some(impl_) = function.syntax().ancestors().nth(2).and_then(ast::Impl::cast) { |
| 49 | + if let Some(_) = impl_.trait_() { |
| 50 | + return None; |
57 | 51 | } |
58 | 52 | } |
| 53 | + |
59 | 54 | // Remove the `async` keyword plus whitespace after it, if any. |
60 | 55 | let async_range = { |
61 | 56 | let async_token = function.async_token()?; |
@@ -99,23 +94,6 @@ pub(crate) fn unnecessary_async(acc: &mut Assists, ctx: &AssistContext<'_>) -> O |
99 | 94 | ) |
100 | 95 | } |
101 | 96 |
|
102 | | -fn find_corresponding_trait_member( |
103 | | - ctx: &AssistContext<'_>, |
104 | | - function_name: String, |
105 | | -) -> Option<AssocItem> { |
106 | | - let impl_ = ctx.find_node_at_offset::<ast::Impl>()?; |
107 | | - let trait_ = resolve_target_trait(&ctx.sema, &impl_)?; |
108 | | - |
109 | | - trait_ |
110 | | - .items(ctx.db()) |
111 | | - .iter() |
112 | | - .find(|item| match item.name(ctx.db()) { |
113 | | - Some(method_name) => method_name.to_string() == function_name, |
114 | | - _ => false, |
115 | | - }) |
116 | | - .cloned() |
117 | | -} |
118 | | - |
119 | 97 | fn find_all_references( |
120 | 98 | ctx: &AssistContext<'_>, |
121 | 99 | def: &Definition, |
@@ -283,27 +261,6 @@ pub async fn f(s: &S) { s.f2() }"#, |
283 | 261 | check_assist_not_applicable(unnecessary_async, "pub async fn f() { $0f2() }") |
284 | 262 | } |
285 | 263 |
|
286 | | - #[test] |
287 | | - fn applies_on_unnecessary_async_on_trait_method() { |
288 | | - check_assist( |
289 | | - unnecessary_async, |
290 | | - r#" |
291 | | -trait Trait { |
292 | | - fn foo(); |
293 | | -} |
294 | | -impl Trait for () { |
295 | | - $0async fn foo() {} |
296 | | -}"#, |
297 | | - r#" |
298 | | -trait Trait { |
299 | | - fn foo(); |
300 | | -} |
301 | | -impl Trait for () { |
302 | | - fn foo() {} |
303 | | -}"#, |
304 | | - ); |
305 | | - } |
306 | | - |
307 | 264 | #[test] |
308 | 265 | fn does_not_apply_on_async_trait_method() { |
309 | 266 | check_assist_not_applicable( |
|
0 commit comments