Skip to content

Wrong suggestion usize::from for LHS expression #81293

Closed
@tesuji

Description

@tesuji

I try this code: (Playground)

fn main() {
    let a: u16;
    let b: u16 = 42;
    let c: usize = 5;

    a = c + b * 5;
}

I expect to see this happen: No invalid suggestion for usize::from(a) = c + b*5.

Instead, I got this errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:6:13
  |
6 |     a = c + b * 5;
  |             ^^^^^ expected `usize`, found `u16`

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
6 |     a = c + b * 5;
  |         ^^^^^^^^^ expected `u16`, found `usize`
  |
help: you can convert `a` from `u16` to `usize`, matching the type of `c + b * 5`
  |
6 |     usize::from(a) = c + b * 5;
  |     ^^^^^^^^^^^^^^

error[E0277]: cannot add `u16` to `usize`
 --> src/main.rs:6:11
  |
6 |     a = c + b * 5;
  |           ^ no implementation for `usize + u16`
  |
  = help: the trait `Add<u16>` is not implemented for `usize`

@rustbot label D-invalid-suggestion T-compiler

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.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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