Skip to content

Commit 24cdb27

Browse files
committed
let_chainify suggest_coercing_result_via_try_operator
1 parent 6c97f13 commit 24cdb27

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+22-36
Original file line numberDiff line numberDiff line change
@@ -961,43 +961,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
961961
expected: Ty<'tcx>,
962962
found: Ty<'tcx>,
963963
) -> bool {
964-
let ty::Adt(e, args_e) = expected.kind() else {
965-
return false;
966-
};
967-
let ty::Adt(f, args_f) = found.kind() else {
968-
return false;
969-
};
970-
if e.did() != f.did() {
971-
return false;
972-
}
973-
if Some(e.did()) != self.tcx.get_diagnostic_item(sym::Result) {
974-
return false;
975-
}
976964
let map = self.tcx.hir();
977-
if let Some(hir::Node::Expr(expr)) = map.find_parent(expr.hir_id)
978-
&& let hir::ExprKind::Ret(_) = expr.kind
979-
{
980-
// `return foo;`
981-
} else if map.get_return_block(expr.hir_id).is_some() {
982-
// Function's tail expression.
983-
} else {
984-
return false;
985-
}
986-
let e_ok = args_e.type_at(0);
987-
let f_ok = args_f.type_at(0);
988-
if !self.infcx.can_eq(self.param_env, f_ok, e_ok) {
989-
return false;
990-
}
991-
let e = args_e.type_at(1);
992-
let f = args_f.type_at(1);
993-
if self
994-
.infcx
995-
.type_implements_trait(
996-
self.tcx.get_diagnostic_item(sym::Into).unwrap(),
997-
[f, e],
998-
self.param_env,
999-
)
1000-
.must_apply_modulo_regions()
965+
let returned = matches!(
966+
map.find_parent(expr.hir_id),
967+
Some(hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Ret(_), .. }))
968+
) || map.get_return_block(expr.hir_id).is_some();
969+
if returned
970+
&& let ty::Adt(e, args_e) = expected.kind()
971+
&& let ty::Adt(f, args_f) = found.kind()
972+
&& e.did() == f.did()
973+
&& Some(e.did()) == self.tcx.get_diagnostic_item(sym::Result)
974+
&& let e_ok = args_e.type_at(0)
975+
&& let f_ok = args_f.type_at(0)
976+
&& self.infcx.can_eq(self.param_env, f_ok, e_ok)
977+
&& let e_err = args_e.type_at(1)
978+
&& let f_err = args_f.type_at(1)
979+
&& self
980+
.infcx
981+
.type_implements_trait(
982+
self.tcx.get_diagnostic_item(sym::Into).unwrap(),
983+
[f_err, e_err],
984+
self.param_env,
985+
)
986+
.must_apply_modulo_regions()
1001987
{
1002988
err.multipart_suggestion(
1003989
"use `?` to coerce and return an appropriate `Err`, and wrap the resulting value \

0 commit comments

Comments
 (0)