mirror of https://github.com/rust-lang/rust.git
Add support for `Result<&T, _>'
This commit is contained in:
parent
9c1a9e03d5
commit
9c3c278b54
|
@ -1451,9 +1451,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let ty::Adt(callee_adt, _) = callee_ty.peel_refs().kind() else {
|
let ty::Adt(callee_adt, _) = callee_ty.peel_refs().kind() else {
|
||||||
return;
|
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;
|
return;
|
||||||
}
|
};
|
||||||
|
|
||||||
if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) {
|
if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) {
|
||||||
return;
|
return;
|
||||||
|
@ -1484,14 +1488,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
Ok(snip) => (snip, Applicability::MachineApplicable),
|
Ok(snip) => (snip, Applicability::MachineApplicable),
|
||||||
Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect),
|
Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect),
|
||||||
};
|
};
|
||||||
let sugg = &format!("map_or({provided_snip}, |v| v)");
|
let sugg = format!("map_or({provided_snip}, |v| v)");
|
||||||
err.span_suggestion_verbose(
|
let msg = format!("use `{adt_name}::map_or` to deref inner value of `{adt_name}`");
|
||||||
error_span,
|
err.span_suggestion_verbose(error_span, msg, sugg, applicability);
|
||||||
"use `Option::map_or` to deref inner value of `Option`",
|
|
||||||
sugg,
|
|
||||||
applicability,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Suggest wrapping the block in square brackets instead of curly braces
|
/// Suggest wrapping the block in square brackets instead of curly braces
|
||||||
|
|
|
@ -80,6 +80,10 @@ LL | arg.unwrap_or(&[])
|
||||||
| this argument influences the return type of `unwrap_or`
|
| this argument influences the return type of `unwrap_or`
|
||||||
note: method defined here
|
note: method defined here
|
||||||
--> $SRC_DIR/core/src/result.rs:LL:COL
|
--> $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
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue