Skip to content

fix: async trait method for unnecessary_async#13508

Merged
bors merged 2 commits into
rust-lang:masterfrom
koka831:fix/13492
Nov 1, 2022
Merged

fix: async trait method for unnecessary_async#13508
bors merged 2 commits into
rust-lang:masterfrom
koka831:fix/13492

Conversation

@koka831

@koka831 koka831 commented Oct 28, 2022

Copy link
Copy Markdown
Contributor

Fix #13492

Comment on lines +49 to +58
// Do nothing if the method is an async member of trait.
if let Some(fname) = function.name() {
if let Some(trait_item) = find_corresponding_trait_member(ctx, fname.to_string()) {
if let AssocItem::Function(method) = trait_item {
if method.is_async(ctx.db()) {
return None;
}
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify this a bit via

// Check if this node is contained in a trait, second ancestors: ast::Fn -> ast::AssocItemList -> ast::Trait
if let Some(_) = function.syntax().ancestors().nth(2).and_then(ast::Trait::cast) {
    return None
}

We basically check the ancestor nodes of the function, take the second ancestor which is the Trait it is contained in (if it is a trait function), and if that is indeed the case we are done here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Veykril Thank you for review! I see, it make sense.

I tried as you suggested but found ast::Trait only matches trait definitions.
(the ancestor of a method in a trait implementation is ast::Impl, which is indistinguishable from impl for struct.)

So I think we would need at least the following procedure, right?

if let Some(impl_) = ctx.find_node_at_offset::<ast::Impl>() {
  if let Some(_) = resolve_target_trait(&ctx.sema, &impl_) {
    return None;
  }
}

@koka831 koka831 Oct 29, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, we can do it with the following:

    if let Some(impl_) = function.syntax().ancestors().nth(2).and_then(ast::Impl::cast) {
        if let Some(_) = impl_.trait_() {
            return None;
        }
    }

fix: remove unused import
@jonas-schievink

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Nov 1, 2022

Copy link
Copy Markdown
Contributor

📌 Commit cf90e4f has been approved by jonas-schievink

It is now in the queue for this repository.

@bors

bors commented Nov 1, 2022

Copy link
Copy Markdown
Contributor

⌛ Testing commit cf90e4f with merge a8e97bc...

@bors

bors commented Nov 1, 2022

Copy link
Copy Markdown
Contributor

☀️ Test successful - checks-actions
Approved by: jonas-schievink
Pushing a8e97bc to master...

@bors bors merged commit a8e97bc into rust-lang:master Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Remove unnecessary async" assist should not appear in trait impls

4 participants