Skip to content

Commit d551880

Browse files
committed
let_chains: Inline visit_expr_with_let_maybe_allowed.
1 parent 92587e4 commit d551880

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

src/librustc_passes/ast_validation.rs

+25-37
Original file line numberDiff line numberDiff line change
@@ -335,40 +335,6 @@ impl<'a> AstValidator<'a> {
335335
}
336336
}
337337

338-
/// Visits the `expr` and adjusts whether `let $pat = $expr` is allowed in decendants.
339-
/// Returns whether we walked into `expr` or not.
340-
/// If we did, walking should not happen again.
341-
fn visit_expr_with_let_maybe_allowed(&mut self, expr: &'a Expr, let_allowed: bool) -> bool {
342-
match &expr.node {
343-
// Assuming the context permits, `($expr)` does not impose additional constraints.
344-
ExprKind::Paren(_) => {
345-
self.with_let_allowed(let_allowed, |this, _| visit::walk_expr(this, expr));
346-
}
347-
// Assuming the context permits,
348-
// l && r` allows decendants in `l` and `r` to be `let` expressions.
349-
ExprKind::Binary(op, ..) if op.node == BinOpKind::And => {
350-
self.with_let_allowed(let_allowed, |this, _| visit::walk_expr(this, expr));
351-
}
352-
// However, we do allow it in the condition of the `if` expression.
353-
// We do not allow `let` in `then` and `opt_else` directly.
354-
ExprKind::If(cond, then, opt_else) => {
355-
self.visit_block(then);
356-
walk_list!(self, visit_expr, opt_else);
357-
self.with_let_allowed(true, |this, _| this.visit_expr(cond));
358-
}
359-
// The same logic applies to `While`.
360-
ExprKind::While(cond, then, opt_label) => {
361-
walk_list!(self, visit_label, opt_label);
362-
self.visit_block(then);
363-
self.with_let_allowed(true, |this, _| this.visit_expr(cond));
364-
}
365-
// Don't walk into `expr` and defer further checks to the caller.
366-
_ => return false,
367-
}
368-
369-
true
370-
}
371-
372338
/// Emits an error banning the `let` expression provided.
373339
fn ban_let_expr(&self, expr: &'a Expr) {
374340
self.err_handler()
@@ -509,9 +475,31 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
509475
ExprKind::Let(_, _) if !let_allowed => {
510476
this.ban_let_expr(expr);
511477
}
512-
_ if this.visit_expr_with_let_maybe_allowed(&expr, let_allowed) => {
513-
// Prevent `walk_expr` to happen since we've already done that.
514-
return;
478+
// Assuming the context permits, `($expr)` does not impose additional constraints.
479+
ExprKind::Paren(_) => {
480+
this.with_let_allowed(let_allowed, |this, _| visit::walk_expr(this, expr));
481+
return; // We've already walked into `expr`.
482+
}
483+
// Assuming the context permits,
484+
// l && r` allows decendants in `l` and `r` to be `let` expressions.
485+
ExprKind::Binary(op, ..) if op.node == BinOpKind::And => {
486+
this.with_let_allowed(let_allowed, |this, _| visit::walk_expr(this, expr));
487+
return; // We've already walked into `expr`.
488+
}
489+
// However, we do allow it in the condition of the `if` expression.
490+
// We do not allow `let` in `then` and `opt_else` directly.
491+
ExprKind::If(cond, then, opt_else) => {
492+
this.visit_block(then);
493+
walk_list!(this, visit_expr, opt_else);
494+
this.with_let_allowed(true, |this, _| this.visit_expr(cond));
495+
return; // We've already walked into `expr`.
496+
}
497+
// The same logic applies to `While`.
498+
ExprKind::While(cond, then, opt_label) => {
499+
walk_list!(this, visit_label, opt_label);
500+
this.visit_block(then);
501+
this.with_let_allowed(true, |this, _| this.visit_expr(cond));
502+
return; // We've already walked into `expr`.
515503
}
516504
ExprKind::Closure(_, _, _, fn_decl, _, _) => {
517505
this.check_fn_decl(fn_decl);

src/libsyntax/parse/lexer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,7 @@ mod tests {
14911491
edition: Edition::from_session(),
14921492
ambiguous_block_expr_parse: Lock::new(FxHashMap::default()),
14931493
param_attr_spans: Lock::new(Vec::new()),
1494+
let_chains_spans: Lock::new(Vec::new()),
14941495
}
14951496
}
14961497

0 commit comments

Comments
 (0)