Skip to content
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

New lint: unused-async #7176

Closed
snowsignal opened this issue May 5, 2021 · 1 comment · Fixed by #7225
Closed

New lint: unused-async #7176

snowsignal opened this issue May 5, 2021 · 1 comment · Fixed by #7225
Assignees
Labels
A-lint Area: New lints

Comments

@snowsignal
Copy link

snowsignal commented May 5, 2021

What it does

Checks for functions that are declared async but have no .awaits inside of them.

Categories (optional)

  • Kind: pedantic

Reduces code smell and overhead by encouraging async functions to be refactored to synchronous functions.

Drawbacks

There are valid cases where a programmer may want to wrap synchronous code in an asynchronous future: for example, spawning a synchronous task on an asynchronous scheduler, or implementing an async function for a trait where the implementation is synchronous. These cases are the reason this lint is pedantic. However, there are a few additional steps I want to take with this lint to avoid causing hassle or confusion for programmers:

  • This lint should avoid trait functions, as there are valid reasons why an async trait function might have a non-async implementation.
  • This lint should recommend to the programmer the proper way to make an synchronous task into a future: using the async {} block.

Example

async fn get_random_number() -> i64 {
    4 // Chosen by fair dice roll. Guaranteed to be random.
}

Could be written as:

fn get_random_number() -> i64 {
    4 // Chosen by fair dice roll. Guaranteed to be random.
}

Something like this, however, should not be caught by clippy:

#[async_trait]
trait AsyncTrait {
    async fn foo();
}

struct Bar;

#[async_trait]
impl AsyncTrait for Bar {
    async fn foo() {
        println!("bar");
    }
}
@snowsignal snowsignal added the A-lint Area: New lints label May 5, 2021
@snowsignal
Copy link
Author

I'd like to implement this myself, if possible. Please let me know if this lint is a bad idea, or if you have any suggestions for improvement.

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant