1
Fork 0

Do not suggest using a break label when one is already present

This commit is contained in:
Esteban Küber 2021-01-21 18:13:05 -08:00
parent 7698807770
commit 9e82329c8f
2 changed files with 17 additions and 24 deletions

View file

@ -68,18 +68,18 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
hir::ExprKind::Block(ref b, Some(_label)) => { hir::ExprKind::Block(ref b, Some(_label)) => {
self.with_context(LabeledBlock, |v| v.visit_block(&b)); self.with_context(LabeledBlock, |v| v.visit_block(&b));
} }
hir::ExprKind::Break(label, ref opt_expr) => { hir::ExprKind::Break(break_label, ref opt_expr) => {
if let Some(e) = opt_expr { if let Some(e) = opt_expr {
self.visit_expr(e); self.visit_expr(e);
} }
if self.require_label_in_labeled_block(e.span, &label, "break") { if self.require_label_in_labeled_block(e.span, &break_label, "break") {
// If we emitted an error about an unlabeled break in a labeled // If we emitted an error about an unlabeled break in a labeled
// block, we don't need any further checking for this break any more // block, we don't need any further checking for this break any more
return; return;
} }
let loop_id = match label.target_id { let loop_id = match break_label.target_id {
Ok(loop_id) => Some(loop_id), Ok(loop_id) => Some(loop_id),
Err(hir::LoopIdError::OutsideLoopScope) => None, Err(hir::LoopIdError::OutsideLoopScope) => None,
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => { Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
@ -94,7 +94,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
} }
if let Some(break_expr) = opt_expr { if let Some(break_expr) = opt_expr {
let (head, label, loop_kind) = if let Some(loop_id) = loop_id { let (head, loop_label, loop_kind) = if let Some(loop_id) = loop_id {
match self.hir_map.expect_expr(loop_id).kind { match self.hir_map.expect_expr(loop_id).kind {
hir::ExprKind::Loop(_, label, source, sp) => { hir::ExprKind::Loop(_, label, source, sp) => {
(Some(sp), label, Some(source)) (Some(sp), label, Some(source))
@ -135,10 +135,15 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
"use `break` on its own without a value inside this `{}` loop", "use `break` on its own without a value inside this `{}` loop",
kind.name(), kind.name(),
), ),
"break".to_string(), format!(
"break{}",
break_label
.label
.map_or_else(String::new, |l| format!(" {}", l.ident))
),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
if let Some(label) = label { if let (Some(label), None) = (loop_label, break_label.label) {
match break_expr.kind { match break_expr.kind {
hir::ExprKind::Path(hir::QPath::Resolved( hir::ExprKind::Path(hir::QPath::Resolved(
None, None,

View file

@ -46,12 +46,8 @@ LL | break 'while_loop 123;
| |
help: use `break` on its own without a value inside this `while` loop help: use `break` on its own without a value inside this `while` loop
| |
LL | break; LL | break 'while_loop;
| ^^^^^ | ^^^^^^^^^^^^^^^^^
help: alternatively, you might have meant to use the available loop label
|
LL | break 'while_loop 'while_loop;
| ^^^^^^^^^^^
error[E0571]: `break` with value from a `while` loop error[E0571]: `break` with value from a `while` loop
--> $DIR/loop-break-value.rs:38:12 --> $DIR/loop-break-value.rs:38:12
@ -90,12 +86,8 @@ LL | break 'while_let_loop "nope";
| |
help: use `break` on its own without a value inside this `while` loop help: use `break` on its own without a value inside this `while` loop
| |
LL | break; LL | break 'while_let_loop;
| ^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
help: alternatively, you might have meant to use the available loop label
|
LL | break 'while_let_loop 'while_let_loop;
| ^^^^^^^^^^^^^^^
error[E0571]: `break` with value from a `for` loop error[E0571]: `break` with value from a `for` loop
--> $DIR/loop-break-value.rs:56:9 --> $DIR/loop-break-value.rs:56:9
@ -135,12 +127,8 @@ LL | break 'for_loop Some(17);
| |
help: use `break` on its own without a value inside this `for` loop help: use `break` on its own without a value inside this `for` loop
| |
LL | break; LL | break 'for_loop;
| ^^^^^ | ^^^^^^^^^^^^^^^
help: alternatively, you might have meant to use the available loop label
|
LL | break 'for_loop 'for_loop;
| ^^^^^^^^^
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:4:31 --> $DIR/loop-break-value.rs:4:31