Skip to content

fix: async trait method for unnecessary_async #13508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/ide-assists/src/handlers/unnecessary_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub(crate) fn unnecessary_async(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
if function.body()?.syntax().descendants().find_map(ast::AwaitExpr::cast).is_some() {
return None;
}
// Do nothing if the method is a member of trait.
if let Some(impl_) = function.syntax().ancestors().nth(2).and_then(ast::Impl::cast) {
if let Some(_) = impl_.trait_() {
return None;
}
}

// Remove the `async` keyword plus whitespace after it, if any.
let async_range = {
Expand Down Expand Up @@ -254,4 +260,18 @@ pub async fn f(s: &S) { s.f2() }"#,
fn does_not_apply_when_not_on_prototype() {
check_assist_not_applicable(unnecessary_async, "pub async fn f() { $0f2() }")
}

#[test]
fn does_not_apply_on_async_trait_method() {
check_assist_not_applicable(
unnecessary_async,
r#"
trait Trait {
async fn foo();
}
impl Trait for () {
$0async fn foo() {}
}"#,
);
}
}