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#118413 - chenyukang:yukang-fix-118145-unwra…
…p-for-shorthand, r=compiler-errors Fix the issue of suggesting unwrap/expect for shorthand field Fixes rust-lang#118145
- Loading branch information
Showing
8 changed files
with
341 additions
and
9 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
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`. |
77 changes: 77 additions & 0 deletions
77
tests/ui/mismatched_types/mismatch-sugg-for-shorthand-field.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,77 @@ | ||
// run-rustfix | ||
// edition:2021 | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
fn test1() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option: Option<&'a str>, | ||
} | ||
|
||
let option: Option<String> = Some(string.clone()); | ||
let s = Demo { option: option.as_deref() }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test2() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let s = Demo { option_ref: option_ref.map(|x| x.as_str()) }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test3() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let option_ref_ref = option_ref.as_ref(); | ||
|
||
let s = Demo { option_ref_ref: option_ref_ref.map(|x| x.as_str()) }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test4() { | ||
let a = 1; | ||
struct Demo { | ||
a: String, | ||
} | ||
let s = Demo { a: a.to_string() }; //~ ERROR mismatched types | ||
} | ||
|
||
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
fn test5() { | ||
let a = async { 42 }; | ||
struct Demo { | ||
a: BoxFuture<'static, i32>, | ||
} | ||
let s = Demo { a: Box::pin(a) }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test6() { | ||
struct A; | ||
struct B; | ||
|
||
impl From<B> for A { | ||
fn from(_: B) -> Self { | ||
A | ||
} | ||
} | ||
|
||
struct Demo { | ||
a: A, | ||
} | ||
let a = B; | ||
let s = Demo { a: a.into() }; //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
77 changes: 77 additions & 0 deletions
77
tests/ui/mismatched_types/mismatch-sugg-for-shorthand-field.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,77 @@ | ||
// run-rustfix | ||
// edition:2021 | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
fn test1() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option: Option<&'a str>, | ||
} | ||
|
||
let option: Option<String> = Some(string.clone()); | ||
let s = Demo { option }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test2() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let s = Demo { option_ref }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test3() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let option_ref_ref = option_ref.as_ref(); | ||
|
||
let s = Demo { option_ref_ref }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test4() { | ||
let a = 1; | ||
struct Demo { | ||
a: String, | ||
} | ||
let s = Demo { a }; //~ ERROR mismatched types | ||
} | ||
|
||
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
fn test5() { | ||
let a = async { 42 }; | ||
struct Demo { | ||
a: BoxFuture<'static, i32>, | ||
} | ||
let s = Demo { a }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test6() { | ||
struct A; | ||
struct B; | ||
|
||
impl From<B> for A { | ||
fn from(_: B) -> Self { | ||
A | ||
} | ||
} | ||
|
||
struct Demo { | ||
a: A, | ||
} | ||
let a = B; | ||
let s = Demo { a }; //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.