[clang-tools-extra] [clang-tidy] Fix false positive in misc-redundant-expression with type aliases (PR #198085)
Peiqi Li via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 02:52:46 PDT 2026
https://github.com/voyager-jhk updated https://github.com/llvm/llvm-project/pull/198085
>From df9bfd9397a416f92b7f97dd6c74ab4519e4edde Mon Sep 17 00:00:00 2001
From: voyager-jhk <voyager.lpq at gmail.com>
Date: Sat, 16 May 2026 20:50:59 +0800
Subject: [PATCH] [clang-tidy] Fix false positive in misc-redundant-expression
with type aliases
The `misc-redundant-expression` check previously flagged expressions as redundant
if their underlying `DeclRefExpr` pointed to the same declaration. This caused
false positives when comparing identical values accessed through distinct type
aliases (sugared types).
This patch uses `printPretty` to compare the source-level spellings of the
expressions, ensuring nested name specifiers and explicit template arguments
are correctly differentiated.
Fixes #145415
---
.../misc/RedundantExpressionCheck.cpp | 44 +++++++++++++++++--
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++
.../redundant-expression-type-aliases.cpp | 26 +++++++++++
3 files changed, 72 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-type-aliases.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
index 9db297990b274..eb70ade4d8e29 100644
--- a/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/RedundantExpressionCheck.cpp
@@ -19,6 +19,7 @@
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
@@ -97,9 +98,46 @@ static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
return false;
return cast<DependentScopeDeclRefExpr>(Left)->getQualifier() ==
cast<DependentScopeDeclRefExpr>(Right)->getQualifier();
- case Stmt::DeclRefExprClass:
- return cast<DeclRefExpr>(Left)->getDecl() ==
- cast<DeclRefExpr>(Right)->getDecl();
+ case Stmt::DeclRefExprClass: {
+ const auto *L = cast<DeclRefExpr>(Left);
+ const auto *R = cast<DeclRefExpr>(Right);
+
+ if (L->getDecl() != R->getDecl() || L->getFoundDecl() != R->getFoundDecl())
+ return false;
+
+ // Compare the printed representations of the qualifiers and template
+ // arguments. String comparison is required because non-dependent template
+ // specializations are not reliably uniqued in the AST.
+ const PrintingPolicy &Policy =
+ L->getDecl()->getASTContext().getPrintingPolicy();
+
+ if (L->hasQualifier() != R->hasQualifier())
+ return false;
+ if (L->hasQualifier()) {
+ std::string LQual, RQual;
+ llvm::raw_string_ostream LOS(LQual), ROS(RQual);
+ L->getQualifier().print(LOS, Policy);
+ R->getQualifier().print(ROS, Policy);
+ if (LQual != RQual)
+ return false;
+ }
+
+ if (L->hasExplicitTemplateArgs() != R->hasExplicitTemplateArgs())
+ return false;
+ if (L->hasExplicitTemplateArgs()) {
+ if (L->getNumTemplateArgs() != R->getNumTemplateArgs())
+ return false;
+ for (unsigned I = 0, E = L->getNumTemplateArgs(); I != E; ++I) {
+ std::string LArg, RArg;
+ llvm::raw_string_ostream LOS(LArg), ROS(RArg);
+ L->getTemplateArgs()[I].getArgument().print(Policy, LOS, true);
+ R->getTemplateArgs()[I].getArgument().print(Policy, ROS, true);
+ if (LArg != RArg)
+ return false;
+ }
+ }
+ return true;
+ }
case Stmt::MemberExprClass:
return cast<MemberExpr>(Left)->getMemberDecl() ==
cast<MemberExpr>(Right)->getMemberDecl();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 89fb1684bba7c..642a5621403c2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -500,6 +500,11 @@ Changes in existing checks
<clang-tidy/checks/misc/multiple-inheritance>` by avoiding false positives when
virtual inheritance causes concrete bases to be counted more than once.
+- Improved :doc:`misc-redundant-expression
+ <clang-tidy/checks/misc/redundant-expression>` check by avoiding false
+ positives when comparing expressions that are structurally identical but
+ use different type aliases.
+
- Improved :doc:`misc-throw-by-value-catch-by-reference
<clang-tidy/checks/misc/throw-by-value-catch-by-reference>` check:
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-type-aliases.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-type-aliases.cpp
new file mode 100644
index 0000000000000..dafd0e93efe8f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression-type-aliases.cpp
@@ -0,0 +1,26 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s misc-redundant-expression %t
+
+namespace std {
+template <class T, int N> struct array {};
+template <class T> struct tuple_size;
+template <class T, int N> struct tuple_size<array<T, N>> {
+ static constexpr int value = N;
+};
+template <class T> constexpr int tuple_size_v = tuple_size<T>::value;
+} // namespace std
+
+using MonthArray = std::array<int, 12>;
+using ZodiacArray = std::array<int, 12>;
+
+void test() {
+ bool b1 = std::tuple_size<MonthArray>::value == std::tuple_size<ZodiacArray>::value;
+ bool b2 = std::tuple_size_v<MonthArray> == std::tuple_size_v<ZodiacArray>;
+
+ bool b3 = std::tuple_size<MonthArray>::value == std::tuple_size<MonthArray>::value;
+ // CHECK-MESSAGES: :[[@LINE-1]]:48: warning: both sides of operator are equivalent [misc-redundant-expression]
+
+ bool b4 = std::tuple_size_v<
+ MonthArray> ==
+ std::tuple_size_v<MonthArray>;
+ // CHECK-MESSAGES: :[[@LINE-2]]:29: warning: both sides of operator are equivalent [misc-redundant-expression]
+}
More information about the cfe-commits
mailing list