While let suggestion will work for closure

This commit is contained in:
yukang 2023-07-11 17:14:20 +08:00
parent d8899c577b
commit 9aed9697cf
4 changed files with 63 additions and 40 deletions

View File

@ -464,52 +464,53 @@ 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 {
pub result: bool, pub result: bool,
pub found_if: bool, pub found_if: bool,
pub err_span: Span, pub err_span: Span,
} }
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 {
match ex.kind { return;
hir::ExprKind::If(cond, _, _) => {
self.found_if = true;
walk_expr(self, cond);
self.found_if = false;
} }
_ => walk_expr(self, ex), match ex.kind {
hir::ExprKind::If(cond, _, _) => {
self.found_if = true;
walk_expr(self, cond);
self.found_if = false;
}
_ => walk_expr(self, ex),
}
}
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) {
if let hir::StmtKind::Local(hir::Local {
span, pat: hir::Pat{..}, ty: None, init: Some(_), ..
}) = &ex.kind
&& self.found_if
&& span.eq(&self.err_span) {
self.result = true;
}
walk_stmt(self, ex);
}
fn visit_body(&mut self, body: &'v hir::Body<'v>) {
hir::intravisit::walk_body(self, body);
} }
} }
fn visit_stmt(&mut self, ex: &'v hir::Stmt<'v>) { let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
if let hir::StmtKind::Local(hir::Local { visitor.visit_body(&body);
span, pat: hir::Pat{..}, ty: None, init: Some(_), .. if visitor.result {
}) = &ex.kind return Some(TypeErrorAdditionalDiags::AddLetForLetChains {
&& self.found_if span: span.shrink_to_lo(),
&& span.eq(&self.err_span) { });
self.result = true;
}
walk_stmt(self, ex);
}
fn visit_body(&mut self, body: &'v hir::Body<'v>) {
hir::intravisit::walk_body(self, body);
}
}
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
visitor.visit_body(&body);
if visitor.result {
return Some(TypeErrorAdditionalDiags::AddLetForLetChains{span: span.shrink_to_lo()});
} }
} }
None None

View File

@ -0,0 +1,4 @@
//run-rustfix
fn main() {
let _ = || { while let Some(_) = Some(1) { } }; //~ ERROR mismatched types
}

View File

@ -0,0 +1,4 @@
//run-rustfix
fn main() {
let _ = || { while Some(_) = Some(1) { } }; //~ ERROR mismatched types
}

View 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`.