Skip to content

assignment in condition reports type mismatch, should suggest comparison #40926

Closed
@oli-obk

Description

@oli-obk

cc #17283

if x = 3 { }

currently reports

error[E0308]: mismatched types
 --> <anon>:3:8
  |
3 |     if x = 3 { }
  |        ^^^^^ expected bool, found ()
  |
  = note: expected type `bool`
             found type `()`

Not only is the note and the label redundant, but imo there should be a note/suggestion asking the user whether they meant x == 3.

Similarly, if the arguments are swapped, one gets a

error[E0070]: invalid left-hand side expression
 --> <anon>:3:8
  |
3 |     if 3 = x { }
  |        ^^^^^ left-hand of expression not valid
error[E0308]: mismatched types
 --> <anon>:3:8
  |
3 |     if 3 = x { }
  |        ^^^^^ expected bool, found ()
  |
  = note: expected type `bool`
             found type `()`

The second error is bogus in this situation. The assignment is invalid, so the result type doesn't matter at all.

The first error should also be suggesting to use ==

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions