[clang-tools-extra] [clang-tidy] Fix readability-redundant-parentheses false positive on typeof (PR #223512)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 14 13:04:13 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Andre Sun (and2049)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/223512.diff
4 Files Affected:
- (modified) clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp (+11-1)
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+4)
- (added) clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c (+10)
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp (+1)
``````````diff
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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/223512
More information about the cfe-commits
mailing list