Skip to content

Commit cf90e4f

Browse files
committed
Simplify the procedure
fix: remove unused import
1 parent 4a7f5ca commit cf90e4f

File tree

1 file changed

+6
-49
lines changed

1 file changed

+6
-49
lines changed

crates/ide-assists/src/handlers/unnecessary_async.rs

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use hir::AssocItem;
21
use ide_db::{
32
assists::{AssistId, AssistKind},
43
base_db::FileId,
54
defs::Definition,
65
search::FileReference,
76
syntax_helpers::node_ext::full_path_of_name_ref,
8-
traits::resolve_target_trait,
97
};
108
use syntax::{
11-
ast::{self, HasName, NameLike, NameRef},
9+
ast::{self, NameLike, NameRef},
1210
AstNode, SyntaxKind, TextRange,
1311
};
1412

@@ -46,16 +44,13 @@ pub(crate) fn unnecessary_async(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
4644
if function.body()?.syntax().descendants().find_map(ast::AwaitExpr::cast).is_some() {
4745
return None;
4846
}
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;
5751
}
5852
}
53+
5954
// Remove the `async` keyword plus whitespace after it, if any.
6055
let async_range = {
6156
let async_token = function.async_token()?;
@@ -99,23 +94,6 @@ pub(crate) fn unnecessary_async(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
9994
)
10095
}
10196

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-
11997
fn find_all_references(
12098
ctx: &AssistContext<'_>,
12199
def: &Definition,
@@ -283,27 +261,6 @@ pub async fn f(s: &S) { s.f2() }"#,
283261
check_assist_not_applicable(unnecessary_async, "pub async fn f() { $0f2() }")
284262
}
285263

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-
307264
#[test]
308265
fn does_not_apply_on_async_trait_method() {
309266
check_assist_not_applicable(

0 commit comments

Comments
 (0)