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

Also suggest Some when an Option<T> is expected, but T is given #42764

Closed
oli-obk opened this issue Jun 20, 2017 · 2 comments
Closed

Also suggest Some when an Option<T> is expected, but T is given #42764

oli-obk opened this issue Jun 20, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jun 20, 2017

let i: Option<i32> = 42i32;

produces

error[E0308]: mismatched types
 --> <anon>:2:26
  |
2 |     let i: Option<i32> = 42i32;
  |                          ^^^^^ expected enum `std::option::Option`, found i32
  |
  = note: expected type `std::option::Option<i32>`
             found type `i32`
  = help: here are some functions which might fulfill your needs:
          - .checked_abs()
          - .checked_neg()

I think the first item in this list should be Some

@est31
Copy link
Member

est31 commented Jun 20, 2017

More generally, if the expected type is an enum, and the found type is T, then you should search the enum's variants and check them whether T is the sole argument of one of them. If yes, suggest the variant constructor.

@nikomatsakis
Copy link
Contributor

I agree we should list variants like that, but I also think the checked_neg functions suggestions are sort of silly; see #42929

zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 13, 2017
Most notably, this will suggest `Some(x)` when the expected type was
an Option<T> but we got an x: T.

Resolves rust-lang#42764.
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 15, 2017
We want the suggested replacement (which IDE tooling and such might offer to
automatically swap in) to, like, actually be correct: suggesting `MyVariant(x)`
when the actual fix is `MyEnum::MyVariant(x)` might be better than nothing, but
Rust is supposed to be the future of computing: we're better than better than
nothing.

As an exceptional case, we excise the prelude path, preferring to suggest
`Some` or `Ok` rather than `std::prelude::v1::Some` and
`std::prelude::v2::Ok`. (It's not worth the effort to future-proof against
hypothetical preludes v2, v3, &c.: we trust our successors to grep—excuse me,
ripgrep—for that.)

Also, don't make this preëmpt the existing probe-for-return-type suggestions,
despite their being looked unfavorably upon, at least in this situation
(rust-lang#42764 (comment)): Cody
Schafer pointed out that that's a separate issue
(rust-lang#43178 (comment)).

This is in the matter of rust-lang#42764.
bors added a commit that referenced this issue Jul 19, 2017
suggest one-argument enum variant to fix type mismatch when applicable

Following @est31's [suggestion](#42764 (comment)).

![some_suggestion](https://user-images.githubusercontent.com/1076988/28101064-ee83f51e-667a-11e7-9e4f-d8f9eb2fb6c3.png)

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

No branches or pull requests

4 participants