Skip to content

Suggest alternative return type for Options and Results #61277

Open
@llogiq

Description

@llogiq

Given the following code:

fn main() {
    let _ = vec[1].iter().next()?;
    Some(())
}

Rust reports an error for using ? and suggests adding ; after the Some(()). A preferrable Option (pun intended) is to suggest changing the return type to Option<()> when the body's result type is Option<_> and there exists at least one ? in the body. Similarly, if we have Results, we might want to try to infer a useful return type, e.g.

fn main() {
    let _ = std::fs::File::open("foo.txt")?;
    Ok(())
}

cc @estebank (diagnostics)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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