rust-lang / rust-lang/rust-clippy
[lint] Suggest more permissive async bounds
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warn on T: Future bounds, suggest T: IntoFuture instead.
Advantage
T: IntoFutureis a strictly more permissive bound thanT: Futurebecause Rust has a blanketimpl<T: Future> IntoFuture for T.- From a semver perspective it's not breaking to change
T: FuturetoT: IntoFuture. - This will make it substantially nicer to actually implement and use
T: IntoFuturein the ecosystem. - This sets Rust up to eventually start desugaring
async {}to returnIntoFuturerather thanFuture, allowing us to solve a number of auto-trait issues. - We're considering making
gen {}returnIntoIteratorrather thanIteratorfor this same reason as well.
Drawbacks
- Like with any new lint, this may result in new warnings and cause people to update their code. However that is the intended effect.
- Implementations will need to call
.into_future()internally to access the future. But this is balanced out by saving callers from having to call.into_future()themselves. IntoFutureis more characters thanFuture. Ideally we would have had the more general trait have a shorter name, but unfortunately that didn't work out that way. Though luckily both traits are part of the prelude since the 2024 edition, so it's not harder in terms of imports.
Example
When someone writes a T: Future bound:
fn foo<T: Future>(x: T) { .. }
Suggest they write T: IntoFuture instead:
fn foo<T: IntoFuture>(x: T) { .. }
Comparison with existing lints
I did not find any relevant existing lints; this lint appears to be new.
Additional Context
A similar lint could probably be implemented for T: Iterator and T: IntoIterator as well. With the eye on gen {} potentially returning IntoIterator that might be worthwhile implementing as well.
If we do ever decide that we should change the desugaring of async {} from Future to IntoFuture, we'd probably want to have this lint on-by-default in the compiler. I'm filing this issue on clippy first so we can iterate on the lint before suggesting we add it to the compiler.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by searching rust-clippy for existing trait-bound lints and their UI tests, then trace how a lint is registered and how suggestions are emitted. Implement the warning for T: Future and its suggested T: IntoFuture replacement, and add coverage for the issue's example while checking that unrelated bounds are not diagnosed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100