-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 #116841 - chenyukang:yukang-suggest-unwrap-expect, r=…
…b-naber Suggest unwrap/expect for let binding type mismatch Found it when investigating #116738 I'm not sure whether it's a good style to suggest `unwrap`, seems it's may helpful for newcomers. #116738 needs another fix to improve it.
- Loading branch information
Showing
10 changed files
with
327 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
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,23 @@ | ||
#![allow(unused, dead_code)] | ||
|
||
fn test_unwrap() -> Option<i32> { | ||
let b: Result<i32, ()> = Ok(1); | ||
let v: i32 = b; // return type is not `Result`, we don't suggest ? here | ||
//~^ ERROR mismatched types | ||
Some(v) | ||
} | ||
|
||
fn test_unwrap_option() -> Result<i32, ()> { | ||
let b = Some(1); | ||
let v: i32 = b; // return type is not `Option`, we don't suggest ? here | ||
//~^ ERROR mismatched types | ||
Ok(v) | ||
} | ||
|
||
fn main() { | ||
let v: i32 = Some(0); //~ ERROR mismatched types | ||
|
||
let c = Ok(false); | ||
let v: i32 = c; //~ ERROR mismatched types | ||
|
||
} |
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/mismatch-ty-dont-suggest.rs:5:18 | ||
| | ||
LL | let v: i32 = b; // return type is not `Result`, we don't suggest ? here | ||
| --- ^ expected `i32`, found `Result<i32, ()>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Result<i32, ()>` | ||
help: consider using `Result::expect` to unwrap the `Result<i32, ()>` value, panicking if the value is a `Result::Err` | ||
| | ||
LL | let v: i32 = b.expect("REASON"); // return type is not `Result`, we don't suggest ? here | ||
| +++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-dont-suggest.rs:12:18 | ||
| | ||
LL | let v: i32 = b; // return type is not `Option`, we don't suggest ? here | ||
| --- ^ expected `i32`, found `Option<{integer}>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Option<{integer}>` | ||
help: consider using `Option::expect` to unwrap the `Option<{integer}>` value, panicking if the value is an `Option::None` | ||
| | ||
LL | let v: i32 = b.expect("REASON"); // return type is not `Option`, we don't suggest ? here | ||
| +++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-dont-suggest.rs:18:18 | ||
| | ||
LL | let v: i32 = Some(0); | ||
| --- ^^^^^^^ expected `i32`, found `Option<{integer}>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Option<{integer}>` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-dont-suggest.rs:21:18 | ||
| | ||
LL | let v: i32 = c; | ||
| --- ^ expected `i32`, found `Result<bool, _>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Result<bool, _>` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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,31 @@ | ||
// run-rustfix | ||
#![allow(unused, dead_code)] | ||
|
||
fn func() -> Option<i32> { | ||
Some(1) | ||
} | ||
|
||
fn test_unwrap() -> Result<i32, ()> { | ||
let b: Result<i32, ()> = Ok(1); | ||
let v: i32 = b?; //~ ERROR mismatched types | ||
Ok(v) | ||
} | ||
|
||
fn test_unwrap_option() -> Option<i32> { | ||
let b = Some(1); | ||
let v: i32 = b?; //~ ERROR mismatched types | ||
Some(v) | ||
} | ||
|
||
fn main() { | ||
let a = Some(1); | ||
let v: i32 = a.expect("REASON"); //~ ERROR mismatched types | ||
|
||
let b: Result<i32, ()> = Ok(1); | ||
let v: i32 = b.expect("REASON"); //~ ERROR mismatched types | ||
|
||
let v: i32 = func().expect("REASON"); //~ ERROR mismatched types | ||
|
||
let a = None; | ||
let v: i32 = a.expect("REASON"); //~ ERROR mismatched types | ||
} |
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,31 @@ | ||
// run-rustfix | ||
#![allow(unused, dead_code)] | ||
|
||
fn func() -> Option<i32> { | ||
Some(1) | ||
} | ||
|
||
fn test_unwrap() -> Result<i32, ()> { | ||
let b: Result<i32, ()> = Ok(1); | ||
let v: i32 = b; //~ ERROR mismatched types | ||
Ok(v) | ||
} | ||
|
||
fn test_unwrap_option() -> Option<i32> { | ||
let b = Some(1); | ||
let v: i32 = b; //~ ERROR mismatched types | ||
Some(v) | ||
} | ||
|
||
fn main() { | ||
let a = Some(1); | ||
let v: i32 = a; //~ ERROR mismatched types | ||
|
||
let b: Result<i32, ()> = Ok(1); | ||
let v: i32 = b; //~ ERROR mismatched types | ||
|
||
let v: i32 = func(); //~ ERROR mismatched types | ||
|
||
let a = None; | ||
let v: i32 = a; //~ ERROR mismatched types | ||
} |
93 changes: 93 additions & 0 deletions
93
tests/ui/mismatched_types/mismatch-ty-unwrap-expect.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,93 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-unwrap-expect.rs:10:18 | ||
| | ||
LL | let v: i32 = b; | ||
| --- ^ expected `i32`, found `Result<i32, ()>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Result<i32, ()>` | ||
help: use the `?` operator to extract the `Result<i32, ()>` value, propagating a `Result::Err` value to the caller | ||
| | ||
LL | let v: i32 = b?; | ||
| + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-unwrap-expect.rs:16:18 | ||
| | ||
LL | let v: i32 = b; | ||
| --- ^ expected `i32`, found `Option<{integer}>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Option<{integer}>` | ||
help: use the `?` operator to extract the `Option<{integer}>` value, propagating an `Option::None` value to the caller | ||
| | ||
LL | let v: i32 = b?; | ||
| + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-unwrap-expect.rs:22:18 | ||
| | ||
LL | let v: i32 = a; | ||
| --- ^ expected `i32`, found `Option<{integer}>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Option<{integer}>` | ||
help: consider using `Option::expect` to unwrap the `Option<{integer}>` value, panicking if the value is an `Option::None` | ||
| | ||
LL | let v: i32 = a.expect("REASON"); | ||
| +++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-unwrap-expect.rs:25:18 | ||
| | ||
LL | let v: i32 = b; | ||
| --- ^ expected `i32`, found `Result<i32, ()>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Result<i32, ()>` | ||
help: consider using `Result::expect` to unwrap the `Result<i32, ()>` value, panicking if the value is a `Result::Err` | ||
| | ||
LL | let v: i32 = b.expect("REASON"); | ||
| +++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-unwrap-expect.rs:27:18 | ||
| | ||
LL | let v: i32 = func(); | ||
| --- ^^^^^^ expected `i32`, found `Option<i32>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Option<i32>` | ||
help: consider using `Option::expect` to unwrap the `Option<i32>` value, panicking if the value is an `Option::None` | ||
| | ||
LL | let v: i32 = func().expect("REASON"); | ||
| +++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-ty-unwrap-expect.rs:30:18 | ||
| | ||
LL | let v: i32 = a; | ||
| --- ^ expected `i32`, found `Option<_>` | ||
| | | ||
| expected due to this | ||
| | ||
= note: expected type `i32` | ||
found enum `Option<_>` | ||
help: consider using `Option::expect` to unwrap the `Option<_>` value, panicking if the value is an `Option::None` | ||
| | ||
LL | let v: i32 = a.expect("REASON"); | ||
| +++++++++++++++++ | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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