Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
oli-obk opened this issue Mar 30, 2017 · 0 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Mar 30, 2017

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 ==

@jdm jdm added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 1, 2017
bors added a commit that referenced this issue Jun 17, 2017
Report error for assignment in `if` condition

For code like `if x = 3 {}`, output:

```
error[E0308]: mismatched types
  --> $DIR/issue-17283.rs:25:8
   |
25 |     if x = x {
   |        ^^^^^
   |        |
   |        help: did you mean to compare equality? `x == x`
   |        expected bool, found ()
   |
   = note: expected type `bool`
              found type `()`
```

Fix #40926.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

2 participants