C++: Add a 'good' example for cpp/unsigned-difference-expression-compared-zero.

This commit is contained in:
Geoffrey White
2024-07-04 16:33:38 +01:00
parent 4de43e1bfa
commit 7abece46c7

View File

@@ -1,5 +1,10 @@
unsigned limit = get_limit();
unsigned total = 0;
while (limit - total > 0) { // wrong: if `total` is greater than `limit` this will underflow and continue executing the loop.
while (limit - total > 0) { // BAD: if `total` is greater than `limit` this will underflow and continue executing the loop.
total += get_data();
}
}
while (total < limit) { // GOOD: never underflows.
total += get_data();
}