if
condition can be moved inside the for
loop as an enumerator guard SC-R1062Scala's for
loop has 2 elements — enumerator generator that defines the range for the loop and an enumerator guard, a condition that must be satisfied before the loop's body can be executed. If your for
loop has a single statement in its body and this statement is an if
condition without an else
clause, it is recommended that you move this explicit if
condition to the loop.
for (i <- lb to ub) {
if (i % 2 == 0) {
println(i)
}
}
for (i <- lb to ub if i % 2 == 0) {
println(i)
}