Skip to content

Confusing error message when matching on an unnecessarily-unwrapped value #63082

Closed
@mkadziolka

Description

@mkadziolka

Consider this code (playground):

pub fn test(input: &str) -> Result<serde_json::Value, String> {
    match serde_json::from_str(input).unwrap() {
        Ok(v) => Ok(v),
        Err(why) => Err(format!("JSON decode failed: {:?}", why)),
    }
}

This looks like perfectly reasonable code, but upon further inspection, you can see an .unwrap() as a leftover from refactoring. However, Rust chooses a quite confusing way of pointing this out:

error[E0282]: type annotations needed
 --> src/lib.rs:4:13
  |
4 |         Err(why) => Err(format!("JSON decode failed: {:?}", why)),
  |             ^^^ cannot infer type

As far as I am aware, there is no way to solve this problem with just a type annotation.

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