Rollup merge of #102210 - notriddle:notriddle/did-you-mean, r=cjgillot
diagnostics: avoid syntactically invalid suggestion in if conditionals Fixes #101065
This commit is contained in:
commit
81eb35f18a
@ -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()),
|
||||
|
14
src/test/ui/suggestions/issue-101065.fixed
Normal file
14
src/test/ui/suggestions/issue-101065.fixed
Normal file
@ -0,0 +1,14 @@
|
||||
// check-fail
|
||||
// run-rustfix
|
||||
|
||||
enum FakeResult<T> {
|
||||
Ok(T)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _x = if true {
|
||||
FakeResult::Ok(FakeResult::Ok(()))
|
||||
} else {
|
||||
FakeResult::Ok(FakeResult::Ok(())) //~ERROR E0308
|
||||
};
|
||||
}
|
14
src/test/ui/suggestions/issue-101065.rs
Normal file
14
src/test/ui/suggestions/issue-101065.rs
Normal file
@ -0,0 +1,14 @@
|
||||
// check-fail
|
||||
// run-rustfix
|
||||
|
||||
enum FakeResult<T> {
|
||||
Ok(T)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _x = if true {
|
||||
FakeResult::Ok(FakeResult::Ok(()))
|
||||
} else {
|
||||
FakeResult::Ok(()) //~ERROR E0308
|
||||
};
|
||||
}
|
23
src/test/ui/suggestions/issue-101065.stderr
Normal file
23
src/test/ui/suggestions/issue-101065.stderr
Normal file
@ -0,0 +1,23 @@
|
||||
error[E0308]: `if` and `else` have incompatible types
|
||||
--> $DIR/issue-101065.rs:12:9
|
||||
|
|
||||
LL | let _x = if true {
|
||||
| ______________-
|
||||
LL | | FakeResult::Ok(FakeResult::Ok(()))
|
||||
| | ---------------------------------- expected because of this
|
||||
LL | | } else {
|
||||
LL | | FakeResult::Ok(())
|
||||
| | ^^^^^^^^^^^^^^^^^^ expected enum `FakeResult`, found `()`
|
||||
LL | | };
|
||||
| |_____- `if` and `else` have incompatible types
|
||||
|
|
||||
= note: expected enum `FakeResult<FakeResult<()>>`
|
||||
found enum `FakeResult<()>`
|
||||
help: try wrapping the expression in `FakeResult::Ok`
|
||||
|
|
||||
LL | FakeResult::Ok(FakeResult::Ok(()))
|
||||
| +++++++++++++++ +
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
Loading…
x
Reference in New Issue
Block a user