]> git.lizzy.rs Git - rust.git/commit
Auto merge of #86116 - FabianWolff:issue-86100, r=varkor
authorbors <bors@rust-lang.org>
Fri, 11 Jun 2021 10:25:53 +0000 (10:25 +0000)
committerbors <bors@rust-lang.org>
Fri, 11 Jun 2021 10:25:53 +0000 (10:25 +0000)
commitdddebf94bccddaa7b8836380c1d90b34553d79d0
tree791e02b5df88211ddfaa1131936691bf4818c5db
parent66ba81059e15b3466c71fe5b5bf2418702dd1fd1
parent6206247335619ac83550fae2013ead4e66b454d1
Auto merge of #86116 - FabianWolff:issue-86100, r=varkor

Suggest a trailing comma if a 1-tuple is expected and a parenthesized expression is found

This pull request fixes #86100. The following code:
```rust
fn main() {
    let t: (i32,) = (1);
}
```
currently produces:
```
warning: unnecessary parentheses around assigned value
 --> test.rs:2:21
  |
2 |     let t: (i32,) = (1);
  |                     ^^^ help: remove these parentheses
  |
  = note: `#[warn(unused_parens)]` on by default

error[E0308]: mismatched types
 --> test.rs:2:21
  |
2 |     let t: (i32,) = (1);
  |            ------   ^^^ expected tuple, found integer
  |            |
  |            expected due to this
  |
  = note: expected tuple `(i32,)`
              found type `{integer}`

error: aborting due to previous error; 1 warning emitted
```
With my changes, I get the same warning and the following error:
```
error[E0308]: mismatched types
 --> test.rs:2:21
  |
2 |     let t: (i32,) = (1);
  |            ------   ^^^ expected tuple, found integer
  |            |
  |            expected due to this
  |
  = note: expected tuple `(i32,)`
              found type `{integer}`
help: use a trailing comma to create a tuple with one element
  |
2 |     let t: (i32,) = (1,);
  |                     ^^^^
```
i.e. I have added a suggestion to add a trailing comma to create a 1-tuple. This suggestion is only issued if a 1-tuple is expected and the expression (`(1)` in the example above) is surrounded by parentheses and does not already have a tuple type. In this situation, I'd say that it is pretty likely that the user meant to create a tuple.