1
Fork 0

diagnostics: avoid syntactically invalid suggestion in if conditionals

Fixes #101065
This commit is contained in:
Michael Howell 2022-09-23 13:24:35 -07:00
parent 4a14677239
commit ac06d9cca3
4 changed files with 61 additions and 0 deletions

View file

@ -417,6 +417,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::def::CtorKind::Const => unreachable!(),
};
// Suggest constructor as deep into the block tree as possible.
// This fixes https://github.com/rust-lang/rust/issues/101065,
// and also just helps make the most minimal suggestions.
let mut expr = expr;
while let hir::ExprKind::Block(block, _) = &expr.kind
&& let Some(expr_) = &block.expr
{
expr = expr_
}
vec![
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
(expr.span.shrink_to_hi(), close.to_owned()),