Skip to content

Commit 06fb72b

Browse files
committed
[clang-tidy] fix bugprone-sizeof-expression when sizeof expression with template types
Fixed: #115175. `dependent type` are not the same even pointers are the same.
1 parent 895a8e6 commit 06fb72b

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-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-unchecked-optional-access
181182
<clang-tidy/checks/bugprone/unchecked-optional-access>` to support

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

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

0 commit comments

Comments
 (0)