4

fix: async trait method for `unnecessary_async` by koka831 · Pull Request #13508...

 1 year ago
source link: https://github.com/rust-lang/rust-analyzer/pull/13508
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Contributor

koka831 commented Oct 28, 2022

Fix #13492

All reactions

// 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;

}

}

}

}

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.

All reactions

Contributor

Author

@koka831 koka831 Oct 29, 2022

@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;
  }
}
All reactions

Contributor

Author

@koka831 koka831 Oct 29, 2022

edited

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;
        }
    }
All reactions

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK