Skip to content

Improve error message on Err early returns that forgot the return keyword #86094

Closed
@sdroege

Description

@sdroege

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d5eabab7099a911f6bb9e93e9b7a0886

struct MyError;

fn foo(x: bool) -> Result<(), MyError> {
    if x {
        Err(MyError);
    }
    
    Ok(())
}

The current output is:

error[E0282]: type annotations needed
 --> src/main.rs:5:9
  |
5 |         Err(MyError);
  |         ^^^ cannot infer type for type parameter `T` declared on the enum `Result`

Ideally the output should look like:

error[E0282]: type annotations needed
 --> src/main.rs:5:9
  |
5 |         Err(MyError);
  |         ^^^ cannot infer type for type parameter `T` declared on the enum `Result`
help: try using `return` here:
  |
5 |         return Err(MyError);
  |         ^^^

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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