E0036#
Warning name: block_label_shadowing
The label on a block or loop shadows a label that is already in scope.
Block and loop labels share the same namespace. When a label name is reused on
a nested block or loop, it shadows the outer label, which can make the target
of a labelled break or continue unclear.
Because break can target either a block or a loop while continue can target
a loop, shadowing can redirect control flow to a different construct than
intended. Use distinct, descriptive names for nested block and loop labels.
Erroneous example#
///|
fn has_positive(xs : Array[Int]) -> Bool {
found~: {
// Warning: The label name `found` shadows a label name that is already in scope.
found~: for x in xs {
if x > 0 {
break found~
}
}
false
}
}
Suggestion#
Use distinct, descriptive label names for nested blocks and loops so each control-flow target is unambiguous.
///|
fn has_positive(xs : Array[Int]) -> Bool {
found~: {
search~: for x in xs {
if x > 0 {
break found~ true
}
continue search~
}
false
}
}