fix the issue of suggest unwrap/expect for shorthand field
This commit is contained in:
parent
e06c94d6cb
commit
9386e14401
@ -1811,6 +1811,12 @@ pub(crate) fn suggest_missing_unwrap_expect(
|
||||
".expect(\"REASON\")",
|
||||
)
|
||||
};
|
||||
|
||||
let sugg = match self.tcx.hir().maybe_get_struct_pattern_shorthand_field(expr) {
|
||||
Some(ident) => format!(": {ident}{sugg}"),
|
||||
None => sugg.to_string(),
|
||||
};
|
||||
|
||||
err.span_suggestion_verbose(
|
||||
expr.span.shrink_to_hi(),
|
||||
msg,
|
||||
|
@ -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() {}
|
@ -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() {}
|
@ -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`.
|
Loading…
Reference in New Issue
Block a user