Rust: Also suggest `Some` when an `Option<T>` is expected, but `T` is given

Created on 20 Jun 2017  路  2Comments  路  Source: rust-lang/rust

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

A-diagnostics

Most helpful comment

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.

All 2 comments

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.

I agree we should list variants like that, but I also think the checked_neg functions suggestions are sort of silly; see https://github.com/rust-lang/rust/issues/42929

Was this page helpful?
0 / 5 - 0 ratings