Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mkadziolka opened this issue Jul 28, 2019 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mkadziolka
Copy link

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.

@jonas-schievink
Copy link
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

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 28, 2019
bors added a commit that referenced this issue Dec 11, 2019
Point at method call when type annotations are needed

- Point at method call instead of whole expression when type annotations are needed.
- Suggest use of turbofish on function and methods.

Fix #49391, fix #46333, fix #48089. CC #58517, #63502, #63082.

r? @nikomatsakis
bors added a commit that referenced this issue Dec 13, 2019
Point at method call when type annotations are needed

- Point at method call instead of whole expression when type annotations are needed.
- Suggest use of turbofish on function and methods.

Fix #49391, fix #46333, fix #48089. CC #58517, #63502, #63082.

Fixes #40015

r? @nikomatsakis
@estebank
Copy link
Contributor

estebank commented Sep 2, 2020

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
Copy link
Contributor

estebank commented Aug 3, 2023

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() {
  |                               ++++++++++++++++++++

@estebank estebank closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants