forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#90677 - bobrippling:suggest-tuple-parens, r…
…=camelid Suggest tuple-parentheses for enum variants This follows on from rust-lang#86493 / rust-lang#86481, making the parentheses suggestion. To summarise, given the following code: ```rust fn f() -> Option<(i32, i8)> { Some(1, 2) } ``` The current output is: ``` error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied --> b.rs:2:5 | 2 | Some(1, 2) | ^^^^ - - supplied 2 arguments | | | expected 1 argument error: aborting due to previous error For more information about this error, try `rustc --explain E0061`. ``` With this change, `rustc` will now suggest parentheses when: - The callee is expecting a single tuple argument - The number of arguments passed matches the element count in the above tuple - The arguments' types match the tuple's fields ``` error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied --> b.rs:2:5 | 2 | Some(1, 2) | ^^^^ - - supplied 2 arguments | help: use parentheses to construct a tuple | 2 | Some((1, 2)) | + + ```
- Loading branch information
Showing
6 changed files
with
255 additions
and
6 deletions.
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,16 @@ | ||
// Ensure we don't suggest tuple-wrapping when we'd end up with a type error | ||
|
||
fn main() { | ||
// we shouldn't suggest to fix these - `2` isn't a `bool` | ||
|
||
let _: Option<(i32, bool)> = Some(1, 2); | ||
//~^ ERROR this enum variant takes 1 argument but 2 arguments were supplied | ||
int_bool(1, 2); | ||
//~^ ERROR this function takes 1 argument but 2 arguments were supplied | ||
|
||
let _: Option<(i8,)> = Some(); | ||
//~^ ERROR this enum variant takes 1 argument but 0 arguments were supplied | ||
} | ||
|
||
fn int_bool(_: (i32, bool)) { | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/ui/suggestions/args-instead-of-tuple-errors.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,33 @@ | ||
error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied | ||
--> $DIR/args-instead-of-tuple-errors.rs:6:34 | ||
| | ||
LL | let _: Option<(i32, bool)> = Some(1, 2); | ||
| ^^^^ - - supplied 2 arguments | ||
| | | ||
| expected 1 argument | ||
|
||
error[E0061]: this function takes 1 argument but 2 arguments were supplied | ||
--> $DIR/args-instead-of-tuple-errors.rs:8:5 | ||
| | ||
LL | int_bool(1, 2); | ||
| ^^^^^^^^ - - supplied 2 arguments | ||
| | | ||
| expected 1 argument | ||
| | ||
note: function defined here | ||
--> $DIR/args-instead-of-tuple-errors.rs:15:4 | ||
| | ||
LL | fn int_bool(_: (i32, bool)) { | ||
| ^^^^^^^^ -------------- | ||
|
||
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied | ||
--> $DIR/args-instead-of-tuple-errors.rs:11:28 | ||
| | ||
LL | let _: Option<(i8,)> = Some(); | ||
| ^^^^-- supplied 0 arguments | ||
| | | ||
| expected 1 argument | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0061`. |
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,27 @@ | ||
// Test suggesting tuples where bare arguments may have been passed | ||
// See issue #86481 for details. | ||
|
||
// run-rustfix | ||
|
||
fn main() { | ||
let _: Result<(i32, i8), ()> = Ok((1, 2)); | ||
//~^ ERROR this enum variant takes 1 argument but 2 arguments were supplied | ||
let _: Option<(i32, i8, &'static str)> = Some((1, 2, "hi")); | ||
//~^ ERROR this enum variant takes 1 argument but 3 arguments were supplied | ||
let _: Option<()> = Some(()); | ||
//~^ ERROR this enum variant takes 1 argument but 0 arguments were supplied | ||
|
||
two_ints((1, 2)); //~ ERROR this function takes 1 argument | ||
|
||
with_generic((3, 4)); //~ ERROR this function takes 1 argument | ||
} | ||
|
||
fn two_ints(_: (i32, i32)) { | ||
} | ||
|
||
fn with_generic<T: Copy + Send>((a, b): (i32, T)) { | ||
if false { | ||
// test generics/bound handling | ||
with_generic((a, b)); //~ ERROR this function takes 1 argument | ||
} | ||
} |
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,27 @@ | ||
// Test suggesting tuples where bare arguments may have been passed | ||
// See issue #86481 for details. | ||
|
||
// run-rustfix | ||
|
||
fn main() { | ||
let _: Result<(i32, i8), ()> = Ok(1, 2); | ||
//~^ ERROR this enum variant takes 1 argument but 2 arguments were supplied | ||
let _: Option<(i32, i8, &'static str)> = Some(1, 2, "hi"); | ||
//~^ ERROR this enum variant takes 1 argument but 3 arguments were supplied | ||
let _: Option<()> = Some(); | ||
//~^ ERROR this enum variant takes 1 argument but 0 arguments were supplied | ||
|
||
two_ints(1, 2); //~ ERROR this function takes 1 argument | ||
|
||
with_generic(3, 4); //~ ERROR this function takes 1 argument | ||
} | ||
|
||
fn two_ints(_: (i32, i32)) { | ||
} | ||
|
||
fn with_generic<T: Copy + Send>((a, b): (i32, T)) { | ||
if false { | ||
// test generics/bound handling | ||
with_generic(a, b); //~ ERROR this function takes 1 argument | ||
} | ||
} |
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,84 @@ | ||
error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied | ||
--> $DIR/args-instead-of-tuple.rs:7:36 | ||
| | ||
LL | let _: Result<(i32, i8), ()> = Ok(1, 2); | ||
| ^^ - - supplied 2 arguments | ||
| | ||
help: use parentheses to construct a tuple | ||
| | ||
LL | let _: Result<(i32, i8), ()> = Ok((1, 2)); | ||
| + + | ||
|
||
error[E0061]: this enum variant takes 1 argument but 3 arguments were supplied | ||
--> $DIR/args-instead-of-tuple.rs:9:46 | ||
| | ||
LL | let _: Option<(i32, i8, &'static str)> = Some(1, 2, "hi"); | ||
| ^^^^ - - ---- supplied 3 arguments | ||
| | ||
help: use parentheses to construct a tuple | ||
| | ||
LL | let _: Option<(i32, i8, &'static str)> = Some((1, 2, "hi")); | ||
| + + | ||
|
||
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied | ||
--> $DIR/args-instead-of-tuple.rs:11:25 | ||
| | ||
LL | let _: Option<()> = Some(); | ||
| ^^^^-- supplied 0 arguments | ||
| | ||
help: expected the unit value `()`; create it with empty parentheses | ||
| | ||
LL | let _: Option<()> = Some(()); | ||
| ++ | ||
|
||
error[E0061]: this function takes 1 argument but 2 arguments were supplied | ||
--> $DIR/args-instead-of-tuple.rs:14:5 | ||
| | ||
LL | two_ints(1, 2); | ||
| ^^^^^^^^ - - supplied 2 arguments | ||
| | ||
note: function defined here | ||
--> $DIR/args-instead-of-tuple.rs:19:4 | ||
| | ||
LL | fn two_ints(_: (i32, i32)) { | ||
| ^^^^^^^^ ------------- | ||
help: use parentheses to construct a tuple | ||
| | ||
LL | two_ints((1, 2)); | ||
| + + | ||
|
||
error[E0061]: this function takes 1 argument but 2 arguments were supplied | ||
--> $DIR/args-instead-of-tuple.rs:16:5 | ||
| | ||
LL | with_generic(3, 4); | ||
| ^^^^^^^^^^^^ - - supplied 2 arguments | ||
| | ||
note: function defined here | ||
--> $DIR/args-instead-of-tuple.rs:22:4 | ||
| | ||
LL | fn with_generic<T: Copy + Send>((a, b): (i32, T)) { | ||
| ^^^^^^^^^^^^ ---------------- | ||
help: use parentheses to construct a tuple | ||
| | ||
LL | with_generic((3, 4)); | ||
| + + | ||
|
||
error[E0061]: this function takes 1 argument but 2 arguments were supplied | ||
--> $DIR/args-instead-of-tuple.rs:25:9 | ||
| | ||
LL | with_generic(a, b); | ||
| ^^^^^^^^^^^^ - - supplied 2 arguments | ||
| | ||
note: function defined here | ||
--> $DIR/args-instead-of-tuple.rs:22:4 | ||
| | ||
LL | fn with_generic<T: Copy + Send>((a, b): (i32, T)) { | ||
| ^^^^^^^^^^^^ ---------------- | ||
help: use parentheses to construct a tuple | ||
| | ||
LL | with_generic((a, b)); | ||
| + + | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0061`. |