Skip to content

Commit c1dc914

Browse files
committed
[clang-tidy] Correct sizeof/alignas handling in misc-redundant-expression
Fixed issue with the comparison of UnaryExprOrTypeTraitExpr objects in which only the argument type was checked, without considering the kind of expression. This led to false positives when using sizeof(x) and alignof(x) expressions, as only the comparison x = x was performed without checking if sizeof was equal to alignof. Fixes: #63096 Reviewed By: Izaron Differential Revision: https://reviews.llvm.org/D152713
1 parent 81ad5a5 commit c1dc914

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
144144
const auto *RightUnaryExpr =
145145
cast<UnaryExprOrTypeTraitExpr>(Right);
146146
if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
147-
return LeftUnaryExpr->getArgumentType() ==
148-
RightUnaryExpr->getArgumentType();
147+
return LeftUnaryExpr->getKind() == RightUnaryExpr->getKind() &&
148+
LeftUnaryExpr->getArgumentType() ==
149+
RightUnaryExpr->getArgumentType();
149150
if (!LeftUnaryExpr->isArgumentType() && !RightUnaryExpr->isArgumentType())
150151
return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(),
151152
RightUnaryExpr->getArgumentExpr());

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ Changes in existing checks
360360
<clang-tidy/checks/misc/definitions-in-headers>` to avoid warning on
361361
declarations inside anonymous namespaces.
362362

363+
- Fixed false-positive in :doc:`misc-redundant-expression
364+
<clang-tidy/checks/misc/redundant-expression>` check where expressions like
365+
``alignof`` or ``sizeof`` were incorrectly flagged as identical.
366+
363367
- Improved :doc:`misc-unused-parameters
364368
<clang-tidy/checks/misc/unused-parameters>` check with new `IgnoreVirtual`
365369
option to optionally ignore virtual methods.

clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,15 @@ int TestAssignSideEffect(int i) {
843843

844844
return 2;
845845
}
846+
847+
namespace PR63096 {
848+
849+
struct alignas(sizeof(int)) X {
850+
int x;
851+
};
852+
853+
static_assert(alignof(X) == sizeof(X));
854+
static_assert(sizeof(X) == sizeof(X));
855+
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: both sides of operator are equivalent
856+
857+
}

0 commit comments

Comments
 (0)