Skip to content

Commit ca5c425

Browse files
committed
Fix Ok(()) suggestion when desugaring is involved.
1 parent 9ad5d82 commit ca5c425

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

compiler/rustc_typeck/src/check/demand.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
276276
// we suggest adding a separate return expression instead.
277277
// (To avoid things like suggesting `Ok(while .. { .. })`.)
278278
if expr_ty.is_unit() {
279+
let mut id = expr.hir_id;
280+
let mut parent;
281+
282+
// Unroll desugaring, to make sure this works for `for` loops etc.
283+
loop {
284+
parent = self.tcx.hir().get_parent_node(id);
285+
if let Some(parent_span) = self.tcx.hir().opt_span(parent) {
286+
if parent_span.find_ancestor_inside(expr.span).is_some() {
287+
// The parent node is part of the same span, so is the result of the
288+
// same expansion/desugaring and not the 'real' parent node.
289+
id = parent;
290+
continue;
291+
}
292+
}
293+
break;
294+
}
295+
279296
if let Some(hir::Node::Block(&hir::Block {
280297
span: block_span, expr: Some(e), ..
281-
})) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.hir_id))
298+
})) = self.tcx.hir().find(parent)
282299
{
283-
if e.hir_id == expr.hir_id {
300+
if e.hir_id == id {
284301
if let Some(span) = expr.span.find_ancestor_inside(block_span) {
285302
let return_suggestions =
286303
if self.tcx.is_diagnostic_item(sym::Result, expected_adt.did) {

0 commit comments

Comments
 (0)