Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

#[must_use] on async fns works on returned Future instead of the awaited value #78149

Closed

Description

I tried this code:

#[must_use]
async fn foo() -> i32 {
    std::future::pending().await
}

pub fn with_warnings() {
    foo();
}

pub async fn without_warning() {
    foo().await;
}

I expected the compiler to warn about the unused return value in without_warning(), but the compiler emitted two warnings in with_warnings():

warning: unused implementer of `Future` that must be used
 --> src/lib.rs:7:5
  |
7 |     foo();
  |     ^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: futures do nothing unless you `.await` or poll them

warning: unused return value of `foo` that must be used
 --> src/lib.rs:7:5
  |
7 |     foo();
  |     ^^^^^^

Playground (tried using nightly 2020-10-18 b1496c6)

As the real return values of async fns are actually impl Futures, you may consider this as the expected behavior (I don't agree, and impl Futures are already #[must_use]). If that is the case, there should be a way to make the compiler warn on unused .awaited values (when type of the .awaited value is not #[must_use]).

@rustbot modify labels: A-async-await A-lint -A-diagnostics

Activity

  1. ghost added
    C-bug
    on Oct 20, 2020
  2. added
    A-async-await
    A-diagnostics
    on Oct 20, 2020
  3. tmandry commented on Oct 20, 2020

    @tmandry
    Member

    I think this is indeed a bug, or at the very least unexpected behavior. It might be challenging to fix, since I don't know of a way to express the desired desugaring.

  4. added
    I-prioritize
    A-attributes
    AsyncAwait-Triaged
    on Oct 20, 2020
  5. nikomatsakis commented on Oct 20, 2020

    @nikomatsakis
    Contributor

    I agree that this will be challenging. The first problem as you said is that, in the desugaring, the #[must_use] is being applied to the function that returns the impl Future, and we don't have a clear place where it would be applied to mean "the value awaited by this future". So that's something we'd have to teach the lint somehow.

    Second problem then would be that the lint would have to have the idea of a "must use future" (let's call it), and it would have to track when that future is awaited and figure out that the resulting value is itself #[must_use].

    I'm pondering if there is a generalization that might make this easier and less special case, not clear.

  6. tmandry commented on Oct 20, 2020

    @tmandry
    Member

    In the short term we could make a lint to warn people who use the attribute on async fns that it won't do anything.

  7. camelid commented on Oct 20, 2020

    @camelid
    Member

    Assigning P-medium and removing I-prioritize as discussed in the prioritization working group.

  8. added
    P-medium
    and removed
    I-prioritize
    on Oct 20, 2020
  9. added
    T-compiler
    on Oct 21, 2020
  10. added
    A-lints
    and removed
    A-diagnostics
    on Oct 21, 2020
  11. 11 remaining items

  12. added 4 commits that reference this issue on Nov 16, 2021
  13. ijackson commented on Nov 25, 2021

    @ijackson
    Contributor

    A similar issue arises with fehler, withoutboats/fehler#45. In fact, it would be nice in general to be able to say, for a function which returns Result<Wombat,SomeError>, that the Wombat must be used. #[must_use] doesn't work there either, because it just demands use of the Result.

    Another way to look at this is that both Result and Future have syntactic sugar for unpacking and implementing control flow effects, which makes them both special in that their contained type is somehow the "underlying" "real" return type of the function.

  14. added a commit that references this issue on Nov 11, 2022
    243496e
  15. added 2 commits that reference this issue on Nov 11, 2022
    37c5efa
    f12f8e5
  16. added 2 commits that reference this issue on Nov 11, 2022
    ebddca4
    978e7ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-async-awaitA-attributesA-lintsAsyncAwait-PolishAsyncAwait-TriagedC-bugP-mediumT-compiler

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions