Skip to content

.try_into().unwrap() is suggested even when .into() would work #70851

Closed
@jonas-schievink

Description

@jonas-schievink
fn returns_usize() -> usize {
    let x = 0u8;
    x
}
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
1 | fn returns_usize() -> usize {
  |                       ----- expected `usize` because of return type
2 |     let x = 0u8;
3 |     x
  |     ^ expected `usize`, found `u8`
  |
help: you can convert an `u8` to `usize` and panic if the converted value wouldn't fit
  |
3 |     x.try_into().unwrap()
  |

It would be better to suggest x.into() instead, which is shorter, cannot fail, and doesn't require importing a trait.

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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