modified_range_bound
Finds a variable bounding a for range assigned in the loop, which does not change it.
Category suspicious, warn by default.
#import "Basic";
count_nonzero :: (xs: [] int) -> int {
n := xs.count;
for 0..n - 1 {
if xs[it] == 0 n -= 1;
}
return n;
}
main :: () {
print("%\n", count_nonzero(.[1, 0, 2]));
}warning[modified_range_bound]: `n` bounds this loop's range, which was fixed before the first iteration: changing it here does not change the loop
--> example.jai:7:24
|
7 | if xs[it] == 0 n -= 1;
| ^
|
= help: use a `while` loop if the bound should follow the change, or a separate variable if it should not
= note: `modified_range_bound` is `warn` by default#import "Basic";
count_nonzero :: (xs: [] int) -> int {
n := xs.count;
for xs {
if it == 0 n -= 1;
}
return n;
}
main :: () {
print("%\n", count_nonzero(.[1, 0, 2]));
}Only locals named directly in a range end (n in 0..n-1); xs.count bounds are left alone.