Skip to content

Special case suggestion for missing tail expression when dealing with Ok(()) and Some(()) #90553

Description

@estebank

The suggestion in the following code isn't great:

error[E0308]: mismatched types
  --> src/main.rs:72:5
   |
72 | /     while let Some(i) = x.try_next().await? {
73 | |         println!("{:?}", i);
74 | |     }
   | |_____^ expected enum `Option`, found `()`
   |
   = note:   expected enum `Option<()>`
           found unit type `()`
help: try using a variant of the expected enum
   |
72 ~     Some(while let Some(i) = x.try_next().await? {
73 +         println!("{:?}", i);
74 +     })
   |

It makes sense why it happens: the while let expression resolves to type (), it's the tail expression of an async fn that returns Option<()>, so wrapping the expression in Some(()) would make the type checker happy, but suggesting that seems almost always wrong? I think I would prefer it if we special cased this and didn't gave a suggestion for Result<(), _> or Option<()>.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Nov 4, 2021
  2. self-assigned this
    on Nov 4, 2021
  3. m-ou-se commented on Nov 4, 2021

    @m-ou-se
    Member

    How about something like this?

    image

  4. m-ou-se commented on Nov 4, 2021

    @m-ou-se
    Member

    (Just need to figure out a way to detect whether the expression was the final one in a block.)

  5. estebank commented on Nov 4, 2021

    @estebank
    ContributorAuthor

    That looks good!

  6. added 5 commits that reference this issue on Nov 18, 2021
    aa17eeb
    281b65a
    3ccfe8b
    0927113
    7354bb3
  7. m-ou-se commented on Jan 19, 2022

    @m-ou-se
    Member

    Re-opening this, because it doesn't work yet for for loops:

    help: try wrapping the expression in `Ok`
      |
    4 ~     Ok(for _ in x {
    5 |         ...
    6 ~     })
      |
    
  8. added a commit that references this issue on Feb 17, 2022
    00a85d2
  9. added a commit that references this issue on Mar 2, 2022
    27288b7
  10. added 2 commits that reference this issue on Mar 2, 2022
    5c0e848
    bdad4a7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions