Skip to content

Commit

Permalink
fix the issue of suggest unwrap/expect for shorthand field
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 28, 2023
1 parent e06c94d commit 9386e14
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
".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,
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.fixed
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 tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.rs
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 tests/ui/mismatched_types/issue-118145-unwrap-for-shorthand.stderr
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`.

0 comments on commit 9386e14

Please sign in to comment.