forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest a trailing comma if a 1-tuple is expected
- Loading branch information
1 parent
e4a6032
commit 6206247
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Tests that a suggestion is issued for type mismatch errors when a | ||
// 1-tuple is expected and a parenthesized expression of non-tuple | ||
// type is supplied. | ||
|
||
fn foo<T>(_t: (T,)) {} | ||
struct S { _s: (String,) } | ||
|
||
fn main() { | ||
let _x: (i32,) = (5); | ||
//~^ ERROR: mismatched types [E0308] | ||
//~| HELP: use a trailing comma to create a tuple with one element | ||
|
||
foo((Some(3))); | ||
//~^ ERROR: mismatched types [E0308] | ||
//~| HELP: use a trailing comma to create a tuple with one element | ||
|
||
let _s = S { _s: ("abc".to_string()) }; | ||
//~^ ERROR: mismatched types [E0308] | ||
//~| HELP: use a trailing comma to create a tuple with one element | ||
|
||
// Do not issue the suggestion if the found type is already a tuple. | ||
let t = (1, 2); | ||
let _x: (i32,) = (t); | ||
//~^ ERROR: mismatched types [E0308] | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/ui/suggestions/issue-86100-tuple-paren-comma.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-86100-tuple-paren-comma.rs:9:22 | ||
| | ||
LL | let _x: (i32,) = (5); | ||
| ------ ^^^ 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 | ||
| | ||
LL | let _x: (i32,) = (5,); | ||
| ^^^^ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-86100-tuple-paren-comma.rs:13:9 | ||
| | ||
LL | foo((Some(3))); | ||
| ^^^^^^^^^ expected tuple, found enum `Option` | ||
| | ||
= note: expected tuple `(_,)` | ||
found enum `Option<{integer}>` | ||
help: use a trailing comma to create a tuple with one element | ||
| | ||
LL | foo((Some(3),)); | ||
| ^^^^^^^^^^ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-86100-tuple-paren-comma.rs:17:22 | ||
| | ||
LL | let _s = S { _s: ("abc".to_string()) }; | ||
| ^^^^^^^^^^^^^^^^^^^ expected tuple, found struct `String` | ||
| | ||
= note: expected tuple `(String,)` | ||
found struct `String` | ||
help: use a trailing comma to create a tuple with one element | ||
| | ||
LL | let _s = S { _s: ("abc".to_string(),) }; | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-86100-tuple-paren-comma.rs:23:22 | ||
| | ||
LL | let _x: (i32,) = (t); | ||
| ------ ^^^ expected a tuple with 1 element, found one with 2 elements | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected tuple `(i32,)` | ||
found tuple `({integer}, {integer})` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |