[clang-tools-extra] [clang-tidy] Fix redundant-parentheses diagnosing synthetic parentheses (PR #228666)
Andre Sun via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 3 09:29:07 PDT 2026
https://github.com/and2049 updated https://github.com/llvm/llvm-project/pull/228666
>From d90c4699880f29568c86b7c84b80b105a6677791 Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Sat, 3 Oct 2026 02:13:12 -0400
Subject: [PATCH 1/3] [clang-tidy] Fix redundant-parentheses diagnosing
synthetic parentheses
Fixes #224705.
---
.../readability/RedundantParenthesesCheck.cpp | 2 ++
.../readability/redundant-parentheses-openmp.cpp | 8 ++++++++
.../checkers/readability/redundant-parentheses.cpp | 14 ++++++++++++++
3 files changed, 24 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-openmp.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index bc1d10586da8abb..75a11ffc02503b6 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -93,6 +93,8 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
+ if (PE->getLParen() == PE->getRParen())
+ return;
diag(PE->getBeginLoc(), "redundant parentheses around expression")
<< createSpacedRemoval(PE->getLParen(), *Result.SourceManager,
getLangOpts())
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-openmp.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-openmp.cpp
new file mode 100644
index 000000000000000..98e9c36fb0a8634
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-openmp.cpp
@@ -0,0 +1,8 @@
+// RUN: %check_clang_tidy %s readability-redundant-parentheses %t -- -- -fopenmp=libomp
+
+void linearClause(int *a, int n) {
+ int i = 0;
+#pragma omp simd linear(i)
+ for (int k = 0; k < n; ++k)
+ a[k] = i;
+}
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 c6275be9f249880..12286cd513f8533 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
@@ -74,6 +74,20 @@ void ignoreStdMaxMin() {
(std::min)(1,2);
}
+int global = 0;
+template <int &R> int &referenceTemplateArgument() {
+ return R;
+}
+int &useReferenceTemplateArgument() {
+ return referenceTemplateArgument<global>();
+}
+
+extern "C" int printf(const char *, ...);
+struct Dumped { int x; };
+void dumpStruct(Dumped *d) {
+ __builtin_dump_struct(d, printf);
+}
+
struct Foo
{
bool x;
>From 02341fa5bb4f9d4738cfaa6f946e975c8d17a8eb Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Sat, 3 Oct 2026 11:50:31 -0400
Subject: [PATCH 2/3] Document fix in release notes
---
clang-tools-extra/docs/ReleaseNotes.md | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 41aa783cabd2487..ec39e65827132d7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -319,8 +319,14 @@ infrastructure are described first, followed by tool-specific sections.
`atomic_compare_exchange_strong()`.
- 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.
+ <clang-tidy/checks/readability/redundant-parentheses>` check:
+
+ - Fixed a false positive on the required parentheses of `typeof` and
+ `typeof_unqual` operands.
+
+ - Fixed false positives and incorrect fixes caused by synthetic parentheses
+ in reference non-type template parameter uses, `__builtin_dump_struct` calls,
+ and OpenMP `linear` clauses.
- Fixed {doc}`readability-simplify-boolean-expr
<clang-tidy/checks/readability/simplify-boolean-expr>` producing invalid
>From 71063bdb1bbc3e918fdabc3d3e53e20c61c7725a Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Sat, 3 Oct 2026 12:25:39 -0400
Subject: [PATCH 3/3] Use AST matcher
---
.../clang-tidy/readability/RedundantParenthesesCheck.cpp | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 75a11ffc02503b6..09dd53e83b54776 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -34,6 +34,10 @@ AST_MATCHER(ParenExpr, isInMacro) {
E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
}
+AST_MATCHER(ParenExpr, isWritten) {
+ return Node.getLParen() != Node.getRParen();
+}
+
AST_MATCHER(TypeLoc, isTypeOfExprTypeLoc) {
return !Node.getUnqualifiedLoc().getAs<TypeOfExprTypeLoc>().isNull();
}
@@ -82,6 +86,7 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
callExpr(unless(cxxOperatorCallExpr(
unless(hasAnyOperatorName("()", "[]"))))),
arraySubscriptExpr())),
+ isWritten(),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()),
@@ -93,8 +98,6 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
- if (PE->getLParen() == PE->getRParen())
- return;
diag(PE->getBeginLoc(), "redundant parentheses around expression")
<< createSpacedRemoval(PE->getLParen(), *Result.SourceManager,
getLangOpts())
More information about the cfe-commits
mailing list