[clang-tools-extra] 3ac17f2 - [clang-tidy] Fix readability-redundant-parentheses false positive on typeof (#223512)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 17 18:56:27 PDT 2026
Author: Andre Sun
Date: 2026-09-18T09:56:21+08:00
New Revision: 3ac17f2256564f86e6e28394853fb71b1fe41227
URL: https://github.com/llvm/llvm-project/commit/3ac17f2256564f86e6e28394853fb71b1fe41227
DIFF: https://github.com/llvm/llvm-project/commit/3ac17f2256564f86e6e28394853fb71b1fe41227.diff
LOG: [clang-tidy] Fix readability-redundant-parentheses false positive on typeof (#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.
Added:
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c
Modified:
clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
clang-tools-extra/docs/ReleaseNotes.md
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 8435c438360a5..bc1d10586da8a 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,10 @@ AST_MATCHER(ParenExpr, isInMacro) {
E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
}
+AST_MATCHER(TypeLoc, isTypeOfExprTypeLoc) {
+ return !Node.getUnqualifiedLoc().getAs<TypeOfExprTypeLoc>().isNull();
+}
+
} // namespace
static FixItHint createSpacedRemoval(SourceLocation Loc,
@@ -79,7 +84,9 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
arraySubscriptExpr())),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
- hasParent(unaryExprOrTypeTraitExpr()))))
+ hasParent(unaryExprOrTypeTraitExpr()),
+ // typeof(...) parentheses are required syntax.
+ hasParent(typeLoc(isTypeOfExprTypeLoc())))))
.bind("dup"),
this);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 765e144305a26..a702721a0d757 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -278,6 +278,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..024b1bd6ae96c
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c
@@ -0,0 +1,16 @@
+// 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;
+ const typeof(a) e = a;
+ volatile typeof_unqual(1) f;
+ typeof(a) const g = a;
+ const typeof((3)) h = 3;
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: const typeof(3) h = 3;
+}
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..c6275be9f2498 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,8 @@ void exceptions() {
alignof((3));
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant parentheses around expression [readability-redundant-parentheses]
// CHECK-FIXES: alignof(3);
+ __typeof__(1) t;
+ const __typeof__(1) ct = 1;
}
namespace std {
More information about the cfe-commits
mailing list