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.

Activity

jonas-schievink

jonas-schievink commented on Jul 28, 2019

@jonas-schievink
Contributor

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

Using from_str::<Result<_, ()>> compiles

added
A-diagnosticsArea: Messages for errors, warnings, and lints
C-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.
on Jul 28, 2019
added 3 commits that reference this issue on Dec 11, 2019

Auto merge of #65951 - estebank:type-inference-error, r=<try>

Auto merge of #65951 - estebank:type-inference-error, r=nikomatsakis

estebank

estebank commented on Sep 2, 2020

@estebank
Contributor

Current output:

error[E0282]: type annotations needed
 --> src/lib.rs:4:13
  |
2 |     match serde_json::from_str(input).unwrap() {
  |           ------------------------------------ this method call resolves to `T`
3 |         Ok(v) => Ok(v),
4 |         Err(why) => Err(format!("JSON decode failed: {:?}", why)),
  |             ^^^ cannot infer type
estebank

estebank commented on Aug 3, 2023

@estebank
Contributor

Current output:

error[[E0282]](https://doc.rust-lang.org/nightly/error_codes/E0282.html): type annotations needed
 --> src/lib.rs:2:11
  |
2 |     match serde_json::from_str(input).unwrap() {
  |           ^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `from_str`
  |
help: consider specifying the generic argument
  |
2 |     match serde_json::from_str::<Result<Value, E>>(input).unwrap() {
  |                               ++++++++++++++++++++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @estebank@jonas-schievink@mkadziolka

        Issue actions

          Confusing error message when matching on an unnecessarily-unwrapped value · Issue #63082 · rust-lang/rust