Skip to content

Commit 7abece4

Browse files
committed
C++: Add a 'good' example for cpp/unsigned-difference-expression-compared-zero.
1 parent 4de43e1 commit 7abece4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
unsigned limit = get_limit();
22
unsigned total = 0;
3-
while (limit - total > 0) { // wrong: if `total` is greater than `limit` this will underflow and continue executing the loop.
3+
4+
while (limit - total > 0) { // BAD: if `total` is greater than `limit` this will underflow and continue executing the loop.
45
total += get_data();
5-
}
6+
}
7+
8+
while (total < limit) { // GOOD: never underflows.
9+
total += get_data();
10+
}

0 commit comments

Comments
 (0)