[clang-tools-extra] [clang-tidy] Fix readability-redundant-parentheses false positive on typeof (PR #223512)

Andre Sun via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 14 13:03:31 PDT 2026


https://github.com/and2049 created https://github.com/llvm/llvm-project/pull/223512

Preserve the required parentheses around the operand of `typeof`, `typeof_unqual`, and GNU `__typeof__` by skipping `ParenExpr` nodes whose immediate parent is a `TypeOfExprTypeLoc`. Redundant inner parentheses such as those in `typeof((x))` are still diagnosed.

Fixes #220899.

>From 3f51e48ad299eab6722e1cc20da4e278b8e97fd5 Mon Sep 17 00:00:00 2001
From: andre sun <andsun9999 at gmail.com>
Date: Mon, 14 Sep 2026 15:52:48 -0400
Subject: [PATCH] [clang-tidy] Fix readability-redundant-parentheses false
 positive on typeof

The required parentheses of typeof/typeof_unqual operands are no longer flagged as redundant. Fixes #220899.
---
 .../readability/RedundantParenthesesCheck.cpp        | 12 +++++++++++-
 clang-tools-extra/docs/ReleaseNotes.md               |  4 ++++
 .../checkers/readability/redundant-parentheses-c23.c | 10 ++++++++++
 .../checkers/readability/redundant-parentheses.cpp   |  1 +
 4 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c

diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 8435c438360a5..bbbc42a325da9 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -10,6 +10,7 @@
 #include "../utils/Matchers.h"
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/TypeLoc.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/ASTMatchers/ASTMatchersMacros.h"
@@ -33,6 +34,13 @@ AST_MATCHER(ParenExpr, isInMacro) {
          E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
 }
 
+AST_MATCHER(ParenExpr, isTypeOfArgument) {
+  return llvm::any_of(Finder->getASTContext().getParents(Node),
+                      [](const DynTypedNode &Parent) {
+                        return Parent.get<TypeOfExprTypeLoc>() != nullptr;
+                      });
+}
+
 } // namespace
 
 static FixItHint createSpacedRemoval(SourceLocation Loc,
@@ -79,7 +87,9 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
                     arraySubscriptExpr())),
                 unless(anyOf(isInMacro(),
                              // sizeof(...) is common used.
-                             hasParent(unaryExprOrTypeTraitExpr()))))
+                             hasParent(unaryExprOrTypeTraitExpr()),
+                             // typeof(...) parentheses are required syntax.
+                             isTypeOfArgument())))
           .bind("dup"),
       this);
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index e865792b05ff5..8c3472265495b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -256,6 +256,10 @@ infrastructure are described first, followed by tool-specific sections.
   exclusively for overload resolution. Added the {option}`IgnoredTypes`
   option to allow customizing the set of ignored types.
 
+- Improved {doc}`readability-redundant-parentheses
+  <clang-tidy/checks/readability/redundant-parentheses>` check by fixing a false
+  positive on the required parentheses of `typeof` and `typeof_unqual` operands.
+
 - Improved {doc}`readability-trailing-comma
   <clang-tidy/checks/readability/trailing-comma>` check:
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c
new file mode 100644
index 0000000000000..06be8140c2d32
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c
@@ -0,0 +1,10 @@
+// RUN: %check_clang_tidy -std=c23-or-later %s readability-redundant-parentheses %t
+
+void typeofOperand(void) {
+  typeof(1) a;
+  typeof_unqual(1) b;
+  typeof(a) c;
+  typeof((2)) d;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant parentheses around expression [readability-redundant-parentheses]
+  // CHECK-FIXES:    typeof(2) d;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
index bd6799e512087..aa65b897ae6da 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
@@ -61,6 +61,7 @@ void exceptions() {
   alignof((3));
   // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant parentheses around expression [readability-redundant-parentheses]
   // CHECK-FIXES:    alignof(3);
+  __typeof__(1) t;
 }
 
 namespace std {



More information about the cfe-commits mailing list