Skip to content

Integer mismatch incorrectly suggests T::from() as LHS of assignment #79756

Closed
@Reconcyl

Description

@Reconcyl

I tried the following code (playground link):

fn main() {
    let mut a: u8 = 0;
    let b: u32 = 0;
    a = b;
}

I expected the compiler to recommend using b.try_into().unwrap() to convert it into a u8. This seems to have been the case in version 1.37.0. Instead, it produces the following error message on stable, beta, and nightly:

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
4 |     a = b;
  |         ^ expected `u8`, found `u32`
  |
help: you can convert `a` from `u8` to `u32`, matching the type of `b`
  |
4 |     u32::from(a) = b;
  |     ^^^^^^^^^^^^

The suggestion is incorrect, as the return value of a function call is not assignable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.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