Skip to content

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

@estebank

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<()>.

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