Skip to content

Commit 9e82329

Browse files
committed
Do not suggest using a break label when one is already present
1 parent 7698807 commit 9e82329

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

compiler/rustc_passes/src/loops.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
6868
hir::ExprKind::Block(ref b, Some(_label)) => {
6969
self.with_context(LabeledBlock, |v| v.visit_block(&b));
7070
}
71-
hir::ExprKind::Break(label, ref opt_expr) => {
71+
hir::ExprKind::Break(break_label, ref opt_expr) => {
7272
if let Some(e) = opt_expr {
7373
self.visit_expr(e);
7474
}
7575

76-
if self.require_label_in_labeled_block(e.span, &label, "break") {
76+
if self.require_label_in_labeled_block(e.span, &break_label, "break") {
7777
// If we emitted an error about an unlabeled break in a labeled
7878
// block, we don't need any further checking for this break any more
7979
return;
8080
}
8181

82-
let loop_id = match label.target_id {
82+
let loop_id = match break_label.target_id {
8383
Ok(loop_id) => Some(loop_id),
8484
Err(hir::LoopIdError::OutsideLoopScope) => None,
8585
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
@@ -94,7 +94,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
9494
}
9595

9696
if let Some(break_expr) = opt_expr {
97-
let (head, label, loop_kind) = if let Some(loop_id) = loop_id {
97+
let (head, loop_label, loop_kind) = if let Some(loop_id) = loop_id {
9898
match self.hir_map.expect_expr(loop_id).kind {
9999
hir::ExprKind::Loop(_, label, source, sp) => {
100100
(Some(sp), label, Some(source))
@@ -135,10 +135,15 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
135135
"use `break` on its own without a value inside this `{}` loop",
136136
kind.name(),
137137
),
138-
"break".to_string(),
138+
format!(
139+
"break{}",
140+
break_label
141+
.label
142+
.map_or_else(String::new, |l| format!(" {}", l.ident))
143+
),
139144
Applicability::MaybeIncorrect,
140145
);
141-
if let Some(label) = label {
146+
if let (Some(label), None) = (loop_label, break_label.label) {
142147
match break_expr.kind {
143148
hir::ExprKind::Path(hir::QPath::Resolved(
144149
None,

src/test/ui/loops/loop-break-value.stderr

+6-18
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ LL | break 'while_loop 123;
4646
|
4747
help: use `break` on its own without a value inside this `while` loop
4848
|
49-
LL | break;
50-
| ^^^^^
51-
help: alternatively, you might have meant to use the available loop label
52-
|
53-
LL | break 'while_loop 'while_loop;
54-
| ^^^^^^^^^^^
49+
LL | break 'while_loop;
50+
| ^^^^^^^^^^^^^^^^^
5551

5652
error[E0571]: `break` with value from a `while` loop
5753
--> $DIR/loop-break-value.rs:38:12
@@ -90,12 +86,8 @@ LL | break 'while_let_loop "nope";
9086
|
9187
help: use `break` on its own without a value inside this `while` loop
9288
|
93-
LL | break;
94-
| ^^^^^
95-
help: alternatively, you might have meant to use the available loop label
96-
|
97-
LL | break 'while_let_loop 'while_let_loop;
98-
| ^^^^^^^^^^^^^^^
89+
LL | break 'while_let_loop;
90+
| ^^^^^^^^^^^^^^^^^^^^^
9991

10092
error[E0571]: `break` with value from a `for` loop
10193
--> $DIR/loop-break-value.rs:56:9
@@ -135,12 +127,8 @@ LL | break 'for_loop Some(17);
135127
|
136128
help: use `break` on its own without a value inside this `for` loop
137129
|
138-
LL | break;
139-
| ^^^^^
140-
help: alternatively, you might have meant to use the available loop label
141-
|
142-
LL | break 'for_loop 'for_loop;
143-
| ^^^^^^^^^
130+
LL | break 'for_loop;
131+
| ^^^^^^^^^^^^^^^
144132

145133
error[E0308]: mismatched types
146134
--> $DIR/loop-break-value.rs:4:31

0 commit comments

Comments
 (0)