unsigned_range_end_minus_one
Finds for 0..n-1 with an unsigned n, which wraps around when n is 0.
Category suspicious, warn by default.
#import "Basic";
sum :: (data: *u8, n: u32) -> int {
total := 0;
for i: 0..n - 1 {
total += data[i];
}
return total;
}
main :: () {
bytes := u8.[1, 2, 3];
print("%\n", sum(bytes.data, 3));
}warning[unsigned_range_end_minus_one]: `n` is unsigned, so this range runs past the end when it is 0
--> example.jai:6:15
|
6 | for i: 0..n - 1 {
| ^^^^^
|
= help: `n` has type `u32` and `- 1` wraps around at 0: check for 0 first, or count with a signed integer
= note: `unsigned_range_end_minus_one` is `warn` by default#import "Basic";
sum :: (data: *u8, n: s64) -> int {
total := 0;
for i: 0..n - 1 {
total += data[i];
}
return total;
}
main :: () {
bytes := u8.[1, 2, 3];
print("%\n", sum(bytes.data, 3));
}Ranges include their end, so 0..n-1 visits the n indices below n, but with an unsigned n of 0 the end wraps around (or stops the program with overflow checks on) instead of giving an empty loop.