Rollup merge of #113567 - chenyukang:yukang-fix-113354-while-let, r=cjgillot
While let suggestion will work for closure body Fixes #113354
This commit is contained in:
commit
f7a34f9518
4 changed files with 63 additions and 40 deletions
|
@ -464,11 +464,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> Option<TypeErrorAdditionalDiags> {
|
) -> Option<TypeErrorAdditionalDiags> {
|
||||||
let hir = self.tcx.hir();
|
let hir = self.tcx.hir();
|
||||||
if let Some(node) = self.tcx.hir().find_by_def_id(cause.body_id) &&
|
if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(cause.body_id) {
|
||||||
let hir::Node::Item(hir::Item {
|
let body = hir.body(body_id);
|
||||||
kind: hir::ItemKind::Fn(_sig, _, body_id), ..
|
|
||||||
}) = node {
|
|
||||||
let body = hir.body(*body_id);
|
|
||||||
|
|
||||||
/// Find the if expression with given span
|
/// Find the if expression with given span
|
||||||
struct IfVisitor {
|
struct IfVisitor {
|
||||||
|
@ -479,7 +476,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
|
|
||||||
impl<'v> Visitor<'v> for IfVisitor {
|
impl<'v> Visitor<'v> for IfVisitor {
|
||||||
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
|
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
|
||||||
if self.result { return; }
|
if self.result {
|
||||||
|
return;
|
||||||
|
}
|
||||||
match ex.kind {
|
match ex.kind {
|
||||||
hir::ExprKind::If(cond, _, _) => {
|
hir::ExprKind::If(cond, _, _) => {
|
||||||
self.found_if = true;
|
self.found_if = true;
|
||||||
|
@ -509,7 +508,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
|
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
|
||||||
visitor.visit_body(&body);
|
visitor.visit_body(&body);
|
||||||
if visitor.result {
|
if visitor.result {
|
||||||
return Some(TypeErrorAdditionalDiags::AddLetForLetChains{span: span.shrink_to_lo()});
|
return Some(TypeErrorAdditionalDiags::AddLetForLetChains {
|
||||||
|
span: span.shrink_to_lo(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
|
4
tests/ui/inference/issue-113354.fixed
Normal file
4
tests/ui/inference/issue-113354.fixed
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
//run-rustfix
|
||||||
|
fn main() {
|
||||||
|
let _ = || { while let Some(_) = Some(1) { } }; //~ ERROR mismatched types
|
||||||
|
}
|
4
tests/ui/inference/issue-113354.rs
Normal file
4
tests/ui/inference/issue-113354.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
//run-rustfix
|
||||||
|
fn main() {
|
||||||
|
let _ = || { while Some(_) = Some(1) { } }; //~ ERROR mismatched types
|
||||||
|
}
|
14
tests/ui/inference/issue-113354.stderr
Normal file
14
tests/ui/inference/issue-113354.stderr
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/issue-113354.rs:3:24
|
||||||
|
|
|
||||||
|
LL | let _ = || { while Some(_) = Some(1) { } };
|
||||||
|
| ^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
||||||
|
|
|
||||||
|
help: consider adding `let`
|
||||||
|
|
|
||||||
|
LL | let _ = || { while let Some(_) = Some(1) { } };
|
||||||
|
| +++
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
Loading…
Add table
Add a link
Reference in a new issue