Skip to content

Commit

Permalink
Add support for `Result<&T, _>'
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jul 15, 2024
1 parent 9c1a9e0 commit 9c3c278
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 9 additions & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,9 +1451,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let ty::Adt(callee_adt, _) = callee_ty.peel_refs().kind() else {
return;
};
if !self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) {
let adt_name = if self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) {
"Option"
} else if self.tcx.is_diagnostic_item(sym::Result, callee_adt.did()) {
"Result"
} else {
return;
}
};

if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) {
return;
Expand Down Expand Up @@ -1484,14 +1488,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ok(snip) => (snip, Applicability::MachineApplicable),
Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect),
};
let sugg = &format!("map_or({provided_snip}, |v| v)");
err.span_suggestion_verbose(
error_span,
"use `Option::map_or` to deref inner value of `Option`",
sugg,
applicability,
);
return;
let sugg = format!("map_or({provided_snip}, |v| v)");
let msg = format!("use `{adt_name}::map_or` to deref inner value of `{adt_name}`");
err.span_suggestion_verbose(error_span, msg, sugg, applicability);
}

/// Suggest wrapping the block in square brackets instead of curly braces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ LL | arg.unwrap_or(&[])
| this argument influences the return type of `unwrap_or`
note: method defined here
--> $SRC_DIR/core/src/result.rs:LL:COL
help: use `Result::map_or` to deref inner value of `Result`
|
LL | arg.map_or(&[], |v| v)
| ~~~~~~~~~~~~~~~~~~

error: aborting due to 4 previous errors

Expand Down

0 comments on commit 9c3c278

Please sign in to comment.