llvm-project
86026ee6 - [clang-tidy] Warn about misuse of sizeof operator in loops. (#143205)

Commit
167 days ago
[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>
Author
Parents
Loading