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

Do not suggest unwrap on Some(T) where T is expected #42746

Closed
oli-obk opened this issue Jun 19, 2017 · 3 comments
Closed

Do not suggest unwrap on Some(T) where T is expected #42746

oli-obk opened this issue Jun 19, 2017 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2017

Currently

let i: i32 = Some(42);

reports

2 |     let i: i32 = Some(42);
  |                  ^^^^^^^^ expected i32, found enum `std::option::Option`
  |
  = note: expected type `i32`
             found type `std::option::Option<{integer}>`
  = help: here are some functions which might fulfill your needs:
          - .unwrap()
          - .unwrap_or_default()

I think Option and Result are special enough to warrant customized error messages. In this case simply suggesting to remove the Some

@nikomatsakis
Copy link
Contributor

See #42929 -- I think that we should not be giving such broad suggestions based solely on types. It's too hard to tell the difference of what is appropriate or inappropriate.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@zackmdavis
Copy link
Member

It's worth stressing that while this makes sense for the Some literal in the given example, in more complicated situations, it's entirely plausible for .unwrap() to be the right suggestion when a variable of type Option<T> is given where T is expected. (Discussion on #42929 seemed to blur the distinction.)

@estebank
Copy link
Contributor

Whitelist implemented in #46461:

error[E0308]: mismatched types
 --> src/main.rs:2:14
  |
2 | let i: i32 = Some(42);
  |              ^^^^^^^^ expected i32, found enum `std::option::Option`
  |
  = note: expected type `i32`
             found type `std::option::Option<{integer}>`

error: aborting due to previous error

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.
Projects
None yet
Development

No branches or pull requests

6 participants