diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-06-05 23:12:51 +0400 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-06-05 23:12:51 +0400 |
| commit | c6e5bb32fbb3d0a85189e56086726c183f68ad0c (patch) | |
| tree | ea543b569b0b10c7348d15fe3a53696b65ef7ff3 /compiler/rustc_parse/src/parser/expr.rs | |
| parent | f21c0a274ebfb35612359e32bee965afb09640d6 (diff) | |
| download | rust-c6e5bb32fbb3d0a85189e56086726c183f68ad0c.tar.gz rust-c6e5bb32fbb3d0a85189e56086726c183f68ad0c.zip | |
Do not suggest adding labeled block if there are no labeled breaks
Diffstat (limited to 'compiler/rustc_parse/src/parser/expr.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 761d521a07e..0d252c3d465 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -13,6 +13,7 @@ use rustc_ast::tokenstream::Spacing; use rustc_ast::util::classify; use rustc_ast::util::literal::LitError; use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity}; +use rustc_ast::visit::Visitor; use rustc_ast::StmtKind; use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID}; use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind}; @@ -1556,6 +1557,28 @@ impl<'a> Parser<'a> { // Continue as an expression in an effort to recover on `'label: non_block_expr`. let expr = self.parse_expr().map(|expr| { + let found_labeled_breaks = { + struct FindLabeledBreaksVisitor(bool); + + impl<'ast> Visitor<'ast> for FindLabeledBreaksVisitor { + fn visit_expr_post(&mut self, ex: &'ast Expr) { + if let ExprKind::Break(Some(_label), _) = ex.kind { + self.0 = true; + } + } + } + + let mut vis = FindLabeledBreaksVisitor(false); + vis.visit_expr(&expr); + vis.0 + }; + + // Suggestion involves adding a (as of time of writing this, unstable) labeled block + // so if the label is not used, just return the unmodified expression + if !found_labeled_breaks { + return expr; + } + let span = expr.span; let sugg_msg = "consider enclosing expression in a block"; let suggestions = vec