diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index c74ef8f2713..2a072795a6c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -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, diff --git a/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.fixed b/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.fixed new file mode 100644 index 00000000000..15d4e393874 --- /dev/null +++ b/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.fixed @@ -0,0 +1,20 @@ +// run-rustfix +#![allow(unused, dead_code)] + +#[derive(Clone, Copy)] +struct Stuff { + count: i32, +} +struct Error; + +fn demo() -> Result { + 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() {} diff --git a/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.rs b/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.rs new file mode 100644 index 00000000000..8e811caa3bd --- /dev/null +++ b/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.rs @@ -0,0 +1,20 @@ +// run-rustfix +#![allow(unused, dead_code)] + +#[derive(Clone, Copy)] +struct Stuff { + count: i32, +} +struct Error; + +fn demo() -> Result { + 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() {} diff --git a/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.stderr b/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.stderr new file mode 100644 index 00000000000..0bcbd6f27c4 --- /dev/null +++ b/tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.stderr @@ -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`.