Skip to content

Suboptimal hint about converting numeric types: Suggests try_into().unwrap() when into() is possible. #71580

Closed
@steffahn

Description

@steffahn
fn main() {
    let a = 1u8;
    let _: i64 = a;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     let _: i64 = a;
  |            ---   ^ expected `i64`, found `u8`
  |            |
  |            expected due to this
  |
help: you can convert an `u8` to `i64` and panic if the converted value wouldn't fit
  |
3 |     let _: i64 = a.try_into().unwrap();
  |                  ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

I would’ve expected something like:

help: you can convert an `u8` to `i64`
  |
3 |     let _: i64 = a.into();
  |                  ^^^^^^^^

Edit: What actually should happen is the same as currently already with _: u64, i.e. it would look like

error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     let _: i64 = a;
  |            ---   ^
  |            |     |
  |            |     expected `i64`, found `u8`
  |            |     help: you can convert an `u8` to `i64`: `a.into()`
  |            expected due to this

@rustbot modify labels: A-diagnostics, T-compiler, D-papercut, C-enhancement, E-easy

This issue has been assigned to @samrat via this comment.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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

Issue actions