diff options
author | Malavika Samak <malavika.samak@gmail.com> | 2025-06-25 10:04:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-25 10:04:10 -0700 |
commit | 86026ee623cd9f02f4277a1f1ff0589b1b16fb30 (patch) | |
tree | 05592f7748cc22c9bd33611344870be89ef9c291 /llvm/unittests/ADT/ArrayRefTest.cpp | |
parent | 532c15a718d1a1d4492f213ff7c142cf93126ff6 (diff) | |
download | llvm-86026ee623cd9f02f4277a1f1ff0589b1b16fb30.zip llvm-86026ee623cd9f02f4277a1f1ff0589b1b16fb30.tar.gz llvm-86026ee623cd9f02f4277a1f1ff0589b1b16fb30.tar.bz2 |
[clang-tidy] Warn about misuse of sizeof operator in loops. (#143205)
The sizeof operator misuses in loop conditionals can be a source of
bugs. The common misuse is attempting to retrieve the number of elements
in the array by using the sizeof expression and forgetting to divide the
value by the sizeof the array elements. This results in an incorrect
computation of the array length and requires a warning from the sizeof
checker.
Example:
```
int array[20];
void test_for_loop() {
// Needs warning.
for(int i = 0; i < sizeof(array); i++) {
array[i] = i;
}
}
void test_while_loop() {
int count = 0;
// Needs warning.
while(count < sizeof(array)) {
array[count] = 0;
count = count + 2;
}
}
```
rdar://151403083
---------
Co-authored-by: MalavikaSamak <malavika2@apple.com>
Diffstat (limited to 'llvm/unittests/ADT/ArrayRefTest.cpp')
0 files changed, 0 insertions, 0 deletions