[all-commits] [llvm/llvm-project] 86026e: [clang-tidy] Warn about misuse of sizeof operator ...
Malavika Samak via All-commits
all-commits at lists.llvm.org
Wed Jun 25 10:04:32 PDT 2025
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: 86026ee623cd9f02f4277a1f1ff0589b1b16fb30
https://github.com/llvm/llvm-project/commit/86026ee623cd9f02f4277a1f1ff0589b1b16fb30
Author: Malavika Samak <malavika.samak at gmail.com>
Date: 2025-06-25 (Wed, 25 Jun 2025)
Changed paths:
M clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
M clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.h
M clang-tools-extra/docs/ReleaseNotes.rst
M clang-tools-extra/docs/clang-tidy/checks/bugprone/sizeof-expression.rst
M clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
Log Message:
-----------
[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 at apple.com>
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list