-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the issue of suggest unwrap/expect for shorthand field
- Loading branch information
1 parent
e06c94d
commit 9386e14
Showing
4 changed files
with
75 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.fixed
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,20 @@ | ||
// run-rustfix | ||
#![allow(unused, dead_code)] | ||
|
||
#[derive(Clone, Copy)] | ||
struct Stuff { | ||
count: i32, | ||
} | ||
struct Error; | ||
|
||
fn demo() -> Result<Stuff, Error> { | ||
let count = Ok(1); | ||
Ok(Stuff { count: count? }) //~ ERROR mismatched types | ||
} | ||
|
||
fn demo_unwrap() -> Stuff { | ||
let count = Some(1); | ||
Stuff { count: count.expect("REASON") } //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
20 changes: 20 additions & 0 deletions
20
tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.rs
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,20 @@ | ||
// run-rustfix | ||
#![allow(unused, dead_code)] | ||
|
||
#[derive(Clone, Copy)] | ||
struct Stuff { | ||
count: i32, | ||
} | ||
struct Error; | ||
|
||
fn demo() -> Result<Stuff, Error> { | ||
let count = Ok(1); | ||
Ok(Stuff { count }) //~ ERROR mismatched types | ||
} | ||
|
||
fn demo_unwrap() -> Stuff { | ||
let count = Some(1); | ||
Stuff { count } //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
29 changes: 29 additions & 0 deletions
29
tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.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,29 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-118145-unwrap-for-shorthand.rs:12:16 | ||
| | ||
LL | Ok(Stuff { count }) | ||
| ^^^^^ expected `i32`, found `Result<{integer}, _>` | ||
| | ||
= note: expected type `i32` | ||
found enum `Result<{integer}, _>` | ||
help: use the `?` operator to extract the `Result<{integer}, _>` value, propagating a `Result::Err` value to the caller | ||
| | ||
LL | Ok(Stuff { count: count? }) | ||
| ++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-118145-unwrap-for-shorthand.rs:17:13 | ||
| | ||
LL | Stuff { count } | ||
| ^^^^^ expected `i32`, found `Option<{integer}>` | ||
| | ||
= 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 | Stuff { count: count.expect("REASON") } | ||
| ++++++++++++++++++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |