Skip to content

Commit e855fea

Browse files
[clang-tidy] fix bugprone-sizeof-expression when sizeof expression with template types (#115275)
Fixed: #115175. `dependent type` are not the same even pointers are the same. --------- Co-authored-by: whisperity <[email protected]>
1 parent 3b29a8a commit e855fea

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
400400
"suspicious usage of 'sizeof(array)/sizeof(...)';"
401401
" denominator differs from the size of array elements")
402402
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
403-
} else if (NumTy && DenomTy && NumTy == DenomTy) {
403+
} else if (NumTy && DenomTy && NumTy == DenomTy &&
404+
!NumTy->isDependentType()) {
405+
// Dependent type should not be compared.
404406
diag(E->getOperatorLoc(),
405407
"suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions "
406408
"have the same type")

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ Changes in existing checks
175175
- Improved :doc:`bugprone-sizeof-expression
176176
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
177177
usages of ``sizeof()``, ``alignof()``, and ``offsetof()`` when adding or
178-
subtracting from a pointer directly or when used to scale a numeric value.
178+
subtracting from a pointer directly or when used to scale a numeric value and
179+
fix false positive when sizeof expression with template types.
179180

180181
- Improved :doc:`bugprone-throw-keyword-missing
181182
<clang-tidy/checks/bugprone/throw-keyword-missing>` by fixing a false positive

clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,10 @@ int ValidExpressions() {
385385
sum += sizeof(PtrArray) / sizeof(A[0]);
386386
return sum;
387387
}
388+
389+
namespace gh115175 {
390+
template<class T>
391+
int ValidateTemplateTypeExpressions(T t) {
392+
return sizeof(t.val) / sizeof(t.val[0]);
393+
}
394+
} // namespace gh115175

0 commit comments

Comments
 (0)