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: missing_must_use_on_future_types #14066

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rmehri01
Copy link

Adds a lint that checks if Future is implemented for a type (struct or enum) and will warn if there is no #[must_use] attribute since futures must be awaited to do work, similar to how async fn works.

Example output:

error: this struct implements `Future` but is missing a `#[must_use]` attribute
  --> tests/ui/missing_must_use_on_future_types.rs:11:1
   |
LL | struct BasicStruct;
   | ^^^^^^^^^^^^^^^^^^
   |
   = note: `-D clippy::missing-must-use-on-future-types` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(clippy::missing_must_use_on_future_types)]`
help: add the attribute
   |
LL + #[must_use]
LL | struct BasicStruct;
   |

Closes #13886

changelog: [missing_must_use_on_future_types]: new lint

@rustbot
Copy link
Collaborator

rustbot commented Jan 23, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 23, 2025
@rmehri01 rmehri01 force-pushed the missing-must-use-on-future-types branch from acf6e40 to dc0d159 Compare January 23, 2025 05:30
@rmehri01 rmehri01 changed the title Missing must use on future types new lint: missing_must_use_on_future_types Jan 23, 2025
/// fn foo() -> Foo { Foo }
///
/// fn main() {
/// foo();
Copy link
Member

Choose a reason for hiding this comment

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

I think this would be better as foo().await, or something else that shows what the correct code is. A more complex example would be nice, i.e., this example might not get the point across

Also, we can remove no_run if you add .await, can't we?

Copy link
Author

Choose a reason for hiding this comment

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

I was trying to show accidentally missing the .await after calling foo() but yeah I agree this might not be the best example


impl<'tcx> LateLintPass<'tcx> for MissingMustUseOnFutureTypes {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
if let ItemKind::Impl(Impl {
Copy link
Member

Choose a reason for hiding this comment

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

Would it not be better to start at the type instead and use implements_trait? It seems like it'd be a lot simpler...

Not sure about performance tho

Copy link
Member

Choose a reason for hiding this comment

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

We should also check if we're in an external macro here.

You can add //@aux-build:proc_macros.rs at the top of your test to test macros.

Copy link
Member

Choose a reason for hiding this comment

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

In the local macro case, we mostly should ensure that the span points to the right place. This could be problematic

Copy link
Author

Choose a reason for hiding this comment

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

Would it not be better to start at the type instead and use implements_trait? It seems like it'd be a lot simpler...

Not sure about performance tho

I think this wouldn't work for the SometimesFuture since it would always need to hold?

Copy link
Member

Choose a reason for hiding this comment

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

I'm honestly not sure! If not then yeah, this is fine.

I'd look and see anyways because I'm not sure how implements_trait/what it calls handles not passing any arguments, or whether there's a way to make it check for any impl at all (I believe this same code would be used for T: Clone for example). It uses Obligation + InferCtxt if curious (I am).

/// fn foo() -> Foo { Foo }
///
/// fn main() {
/// foo().await;
Copy link
Member

Choose a reason for hiding this comment

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

This isn't complete; I meant adding .await alongside #[must_use] in the "Use instead" example to convey the improvement this makes. Maybe also with a comment pointing this out


struct SometimesFuture<T>(PhantomData<T>); //~ ERROR: missing a `#[must_use]` attribute

impl<T: Copy> Future for SometimesFuture<T> {
Copy link
Member

Choose a reason for hiding this comment

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

I would like to make sure two Future impls doesn't lint twice.

One idea if they do: Store all the data needed to lint in the struct, then lint in check_crate_post using span_lint_hir_and_then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing must_use on type implementing Future
3 participants