[clang-tools-extra] c1dc914 - [clang-tidy] Correct sizeof/alignas handling in misc-redundant-expression

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 1 09:24:37 PDT 2023


Author: Piotr Zegar
Date: 2023-07-01T16:20:38Z
New Revision: c1dc914a8ca240650d7411d25c75ad7e2a5974e9

URL: https://github.com/llvm/llvm-project/commit/c1dc914a8ca240650d7411d25c75ad7e2a5974e9
DIFF: https://github.com/llvm/llvm-project/commit/c1dc914a8ca240650d7411d25c75ad7e2a5974e9.diff

LOG: [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: https://github.com/llvm/llvm-project/issues/63096

Reviewed By: Izaron

Differential Revision: https://reviews.llvm.org/D152713

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
index b5028074f017c4..4850440329bc15 100644
--- a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
@@ -144,8 +144,9 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
     const auto *RightUnaryExpr =
         cast<UnaryExprOrTypeTraitExpr>(Right);
     if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType())
-      return LeftUnaryExpr->getArgumentType() ==
-             RightUnaryExpr->getArgumentType();
+      return LeftUnaryExpr->getKind() == RightUnaryExpr->getKind() &&
+             LeftUnaryExpr->getArgumentType() ==
+                 RightUnaryExpr->getArgumentType();
     if (!LeftUnaryExpr->isArgumentType() && !RightUnaryExpr->isArgumentType())
       return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(),
                                RightUnaryExpr->getArgumentExpr());

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 726ec41f6a7369..84333f6cb347bf 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -360,6 +360,10 @@ Changes in existing checks
   <clang-tidy/checks/misc/definitions-in-headers>` to avoid warning on
   declarations inside anonymous namespaces.
 
+- Fixed false-positive in :doc:`misc-redundant-expression
+  <clang-tidy/checks/misc/redundant-expression>` check where expressions like
+  ``alignof`` or ``sizeof`` were incorrectly flagged as identical.
+
 - Improved :doc:`misc-unused-parameters
   <clang-tidy/checks/misc/unused-parameters>` check with new `IgnoreVirtual`
   option to optionally ignore virtual methods.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
index 6597d05e449664..895a1666757ff4 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp
@@ -843,3 +843,15 @@ int TestAssignSideEffect(int i) {
 
   return 2;
 }
+
+namespace PR63096 {
+
+struct alignas(sizeof(int)) X {
+  int x;
+};
+
+static_assert(alignof(X) == sizeof(X));
+static_assert(sizeof(X) == sizeof(X));
+// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: both sides of operator are equivalent
+
+}


        


More information about the cfe-commits mailing list