Skip to content

Compiler suggests unnecessary try into #63697

Closed
@lopopolo

Description

@lopopolo

I updated a dependency and the type of a struct field changed from an i32 to a u16.

I am assigning a u16 value to this struct field. Before, I was calling i32::from to convert the u16 to an i32. With the upgrade this is unnecessary since the field and the value are both u16.

The rust compiler suggests this fix:

error[E0308]: mismatched types
  --> artichoke-frontend/src/parser.rs:90:37
   |
90 |             (*self.parser).lineno = i32::from((*self.context).lineno);
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u16, found i32
help: you can convert an `i32` to `u16` and panic if the converted value wouldn't fit
   |
90 |             (*self.parser).lineno = i32::from((*self.context).lineno).try_into().unwrap();
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: Could not compile `artichoke-frontend`.
warning: build failed, waiting for other jobs to finish...
error: build failed

But it should really suggest removing the i32::from and using the u16 directly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.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.P-mediumMedium priorityT-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