Skip to content

Commit 50f063c

Browse files
committed
Extend error E0695 to unlabeled continue statements
1 parent 63ef53f commit 50f063c

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/librustc_passes/loops.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc::session::Session;
1313

1414
use rustc::hir::map::Map;
1515
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
16-
use rustc::hir;
16+
use rustc::hir::{self, Destination};
1717
use syntax::ast;
1818
use syntax_pos::Span;
1919

@@ -91,6 +91,8 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
9191
self.with_context(LabeledBlock, |v| v.visit_block(&b));
9292
}
9393
hir::ExprBreak(label, ref opt_expr) => {
94+
self.require_label_in_labeled_block(e.span, &label, "break");
95+
9496
let loop_id = match label.target_id.into() {
9597
Ok(loop_id) => loop_id,
9698
Err(hir::LoopIdError::OutsideLoopScope) => ast::DUMMY_NODE_ID,
@@ -109,14 +111,6 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
109111
}
110112

111113
if self.cx == LabeledBlock {
112-
if label.label.is_none() {
113-
struct_span_err!(self.sess, e.span, E0695,
114-
"unlabeled `break` inside of a labeled block")
115-
.span_label(e.span,
116-
"`break` statements that would diverge to or through \
117-
a labeled block need to bear a label")
118-
.emit();
119-
}
120114
return;
121115
}
122116

@@ -156,6 +150,8 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
156150
self.require_break_cx("break", e.span);
157151
}
158152
hir::ExprAgain(label) => {
153+
self.require_label_in_labeled_block(e.span, &label, "continue");
154+
159155
if let Err(hir::LoopIdError::UnlabeledCfInWhileCondition) = label.target_id {
160156
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
161157
}
@@ -193,6 +189,18 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
193189
}
194190
}
195191

192+
fn require_label_in_labeled_block(&mut self, span: Span, label: &Destination, cf_type: &str) {
193+
if self.cx == LabeledBlock {
194+
if label.label.is_none() {
195+
struct_span_err!(self.sess, span, E0695,
196+
"unlabeled `{}` inside of a labeled block", cf_type)
197+
.span_label(span,
198+
format!("`{}` statements that would diverge to or through \
199+
a labeled block need to bear a label", cf_type))
200+
.emit();
201+
}
202+
}
203+
}
196204
fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
197205
struct_span_err!(self.sess, span, E0590,
198206
"`break` or `continue` with no label in the condition of a `while` loop")

0 commit comments

Comments
 (0)