[clang-tools-extra] [clang-tidy] Fix false positive in readability-redundant-parentheses … (PR #192827)
Yuta Nakamura via cfe-commits
cfe-commits at lists.llvm.org
Mon May 18 21:24:36 PDT 2026
https://github.com/nakasan617 updated https://github.com/llvm/llvm-project/pull/192827
>From d7260fe29de6a9fa2e1b01495a8e50feaa6d22f8 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sun, 19 Apr 2026 21:13:13 -0700
Subject: [PATCH 1/7] [clang-tidy] Fix false positive in
readability-redundant-parentheses for overloaded operators
Overloaded comparison operators (e.g. iterator !=, std::string >=) are
represented as CXXOperatorCallExpr, a subclass of CallExpr. The matcher
previously matched all CallExpr, causing spurious warnings for parenthesized
overloaded-operator expressions while silently accepting equivalent built-in
operator expressions (e.g. int == int).
This fix excludes CXXOperatorCallExpr from the CallExpr match, except for
operator() (functor calls), which are treated like regular function calls.
This makes the check consistent: overloaded operators in operator form are
treated the same as built-in binary operators.
Fixes #192463, #192438, #192435.
---
.../readability/RedundantParenthesesCheck.cpp | 4 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 8 +++++
.../readability/redundant-parentheses.cpp | 33 +++++++++++++++++++
3 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 9b3948a1c50c0..e52af57a49127 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -55,7 +55,9 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
parenExpr(), ConstantExpr,
declRefExpr(to(namedDecl(unless(
matchers::matchesAnyListedRegexName(AllowedDecls))))),
- memberExpr(), callExpr())),
+ memberExpr(),
+ callExpr(unless(cxxOperatorCallExpr(
+ unless(hasOverloadedOperatorName("()")))))),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()))))
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 95ed0061d654c..ec17ae71f9123 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -230,6 +230,14 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Improved :doc:`readability-redundant-parentheses
+ <clang-tidy/checks/readability/redundant-parentheses>` check to fix a false
+ positive where parentheses around overloaded comparison operators (e.g.
+ ``iterator !=``, ``std::string >=``) were incorrectly flagged while
+ equivalent built-in operator expressions were not. Overloaded operators
+ written in operator form are now treated consistently with built-in binary
+ operators. Functor calls via ``operator()`` continue to be flagged.
+
- Improved :doc:`bugprone-argument-comment
<clang-tidy/checks/bugprone/argument-comment>` to also check for C++11
inherited constructors.
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 9a8a0d4d73483..212b60d424d23 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
@@ -99,6 +99,39 @@ struct Foo
}
};
+struct Iterator {
+ bool operator!=(const Iterator &) const;
+ bool operator>=(const Iterator &) const;
+};
+
+struct Functor {
+ bool operator()() const;
+};
+
+void overloadedOperators() {
+ Iterator it, end;
+
+ // Single parens around overloaded operator: no warning (consistent with
+ // built-in binary operators, which are not in the matcher).
+ if ((it != end)) {
+ }
+ if ((it >= end)) {
+ }
+
+ // Double parens: outer paren wraps a ParenExpr, so outer paren still warns.
+ if (((it != end))) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if ((it != end)) {
+ }
+
+ // Functor call via operator(): still warns (treated like a regular call).
+ Functor f{};
+ if ((f())) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if (f()) {
+ }
+}
+
void memberExpr() {
Foo foo{};
if ((foo.x)) {
>From d8585e0f6f76d100d970bc70fa8a72cf22228910 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Mon, 20 Apr 2026 00:25:18 -0700
Subject: [PATCH 2/7] Fix paren mismatch: subExpr was receiving two arguments
instead of one
The callExpr(unless(cxxOperatorCallExpr(...))) chain closed anyOf one
level too early, making unless(...) a second argument to subExpr rather
than a second argument to parenExpr.
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../clang-tidy/readability/RedundantParenthesesCheck.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index e52af57a49127..3eb952c5b7c5d 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -57,7 +57,7 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
matchers::matchesAnyListedRegexName(AllowedDecls))))),
memberExpr(),
callExpr(unless(cxxOperatorCallExpr(
- unless(hasOverloadedOperatorName("()")))))),
+ unless(hasOverloadedOperatorName("()"))))))),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()))))
>From 96860869f77febdb1cedba7902940af6ed4334c5 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Mon, 20 Apr 2026 20:50:05 -0700
Subject: [PATCH 3/7] [clang-tidy] Fix false positives in
readability-redundant-parentheses for member-access parens
Parens used as the receiver of a member access (.foo or ->foo) are not
redundant: removing them would change semantics (e.g.
(stream << x).str() or (*it).value). Add hasParent(memberExpr()) to
the unless conditions to suppress warnings in these cases.
Fixes: #192435, #192438
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../readability/RedundantParenthesesCheck.cpp | 9 ++--
.../readability/redundant-parentheses.cpp | 41 +++++++------------
2 files changed, 19 insertions(+), 31 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 3eb952c5b7c5d..5c010d1ae4b20 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -55,12 +55,13 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
parenExpr(), ConstantExpr,
declRefExpr(to(namedDecl(unless(
matchers::matchesAnyListedRegexName(AllowedDecls))))),
- memberExpr(),
- callExpr(unless(cxxOperatorCallExpr(
- unless(hasOverloadedOperatorName("()"))))))),
+ memberExpr(), callExpr())),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
- hasParent(unaryExprOrTypeTraitExpr()))))
+ hasParent(unaryExprOrTypeTraitExpr()),
+ // Don't warn when parens are the object of a
+ // member access: (expr).foo or (expr)->foo.
+ hasParent(memberExpr()))))
.bind("dup"),
this);
}
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 212b60d424d23..ac052b5b1d582 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
@@ -99,37 +99,24 @@ struct Foo
}
};
-struct Iterator {
- bool operator!=(const Iterator &) const;
- bool operator>=(const Iterator &) const;
+// Parens used as the object of a member access are not redundant.
+struct Stream {
+ Stream &operator<<(const char *);
+ const char *str() const;
};
-
-struct Functor {
- bool operator()() const;
+struct Val {
+ int x;
+};
+struct Iter {
+ Val operator*() const;
};
-void overloadedOperators() {
- Iterator it, end;
-
- // Single parens around overloaded operator: no warning (consistent with
- // built-in binary operators, which are not in the matcher).
- if ((it != end)) {
- }
- if ((it >= end)) {
- }
-
- // Double parens: outer paren wraps a ParenExpr, so outer paren still warns.
- if (((it != end))) {
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
- // CHECK-FIXES: if ((it != end)) {
- }
+void memberAccessOnParen(Stream &s, Iter it) {
+ // (s << "x").str() -- parens required for method chaining; no warning.
+ (s << "x").str();
- // Functor call via operator(): still warns (treated like a regular call).
- Functor f{};
- if ((f())) {
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
- // CHECK-FIXES: if (f()) {
- }
+ // (*it).x -- parens required before member access; no warning.
+ auto v = (*it).x;
}
void memberExpr() {
>From 70b810d2e50d0b7411c019ed382e22aaf3931bad Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Mon, 20 Apr 2026 21:12:56 -0700
Subject: [PATCH 4/7] Use isBaseOfMemberAccess matcher to handle implicit AST
nodes
hasParent(memberExpr()) fails when Clang inserts intermediate nodes
between ParenExpr and MemberExpr: ImplicitCastExpr (qualification cast
for const methods) or MaterializeTemporaryExpr (prvalue base). Replace
it with a custom AST_MATCHER that walks up through those transparent
nodes until it finds a MemberExpr or a non-transparent parent.
Co-Authored-By: Claude Sonnet 4.6 <noreply at anthropic.com>
---
.../readability/RedundantParenthesesCheck.cpp | 22 ++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 5c010d1ae4b20..626385df2400a 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -32,6 +32,26 @@ AST_MATCHER(ParenExpr, isInMacro) {
E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
}
+// Returns true if this ParenExpr is the base object of a member access
+// (.field or ->field), possibly through implicit casts or temporary
+// materialization that Clang inserts (e.g. qualification casts for const
+// methods, MaterializeTemporaryExpr for prvalue bases).
+AST_MATCHER(ParenExpr, isBaseOfMemberAccess) {
+ ASTContext &Ctx = Finder->getASTContext();
+ DynTypedNodeList Parents = Ctx.getParents(Node);
+ while (!Parents.empty()) {
+ const auto *E = Parents[0].get<Expr>();
+ if (!E)
+ break;
+ if (isa<MemberExpr>(E))
+ return true;
+ if (!isa<ImplicitCastExpr>(E) && !isa<MaterializeTemporaryExpr>(E))
+ break;
+ Parents = Ctx.getParents(*E);
+ }
+ return false;
+}
+
} // namespace
RedundantParenthesesCheck::RedundantParenthesesCheck(StringRef Name,
@@ -61,7 +81,7 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
hasParent(unaryExprOrTypeTraitExpr()),
// Don't warn when parens are the object of a
// member access: (expr).foo or (expr)->foo.
- hasParent(memberExpr()))))
+ isBaseOfMemberAccess())))
.bind("dup"),
this);
}
>From 804b42c33e8b5c04a6214575a43e3f85fe9934d7 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <yutanak6 at gmail.com>
Date: Sat, 25 Apr 2026 23:47:45 -0700
Subject: [PATCH 5/7] Fix alphabetical ordering of
readability-redundant-parentheses in ReleaseNotes
Move entry from top of 'Changes in existing checks' (before bugprone-*)
to its correct alphabetical position between readability-redundant-member-init
and readability-redundant-preprocessor.
---
clang-tools-extra/docs/ReleaseNotes.rst | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 210ebf223cc7d..68af962611cf6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -230,14 +230,6 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^
-- Improved :doc:`readability-redundant-parentheses
- <clang-tidy/checks/readability/redundant-parentheses>` check to fix a false
- positive where parentheses around overloaded comparison operators (e.g.
- ``iterator !=``, ``std::string >=``) were incorrectly flagged while
- equivalent built-in operator expressions were not. Overloaded operators
- written in operator form are now treated consistently with built-in binary
- operators. Functor calls via ``operator()`` continue to be flagged.
-
- Improved :doc:`bugprone-argument-comment
<clang-tidy/checks/bugprone/argument-comment>` to also check for C++11
inherited constructors.
@@ -553,6 +545,14 @@ Changes in existing checks
`IgnoreMacros` option to suppress warnings when the initializer involves
macros that may expand differently in other configurations.
+- Improved :doc:`readability-redundant-parentheses
+ <clang-tidy/checks/readability/redundant-parentheses>` check to fix a false
+ positive where parentheses around overloaded comparison operators (e.g.
+ ``iterator !=``, ``std::string >=``) were incorrectly flagged while
+ equivalent built-in operator expressions were not. Overloaded operators
+ written in operator form are now treated consistently with built-in binary
+ operators. Functor calls via ``operator()`` continue to be flagged.
+
- Improved :doc:`readability-redundant-preprocessor
<clang-tidy/checks/readability/redundant-preprocessor>` check by fixing a
false positive for nested ``#if`` directives using different builtin
>From 36ba85138a097b8c313f6bd3a840ab5979a95822 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <44285097+nakasan617 at users.noreply.github.com>
Date: Mon, 18 May 2026 21:24:07 -0700
Subject: [PATCH 6/7] Update
clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
Co-authored-by: Daniil Dudkin <unterumarmung at yandex.ru>
---
.../clang-tidy/readability/RedundantParenthesesCheck.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 626385df2400a..6ec3768c7d2ae 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -32,7 +32,7 @@ AST_MATCHER(ParenExpr, isInMacro) {
E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
}
-// Returns true if this ParenExpr is the base object of a member access
+// Matches if this ParenExpr is the base object of a member access
// (.field or ->field), possibly through implicit casts or temporary
// materialization that Clang inserts (e.g. qualification casts for const
// methods, MaterializeTemporaryExpr for prvalue bases).
>From cabbf58410bff05d51e3cf3979d5baf34e96b3e2 Mon Sep 17 00:00:00 2001
From: Yuta Nakamura <44285097+nakasan617 at users.noreply.github.com>
Date: Mon, 18 May 2026 21:24:25 -0700
Subject: [PATCH 7/7] Update
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
Co-authored-by: Daniil Dudkin <unterumarmung at yandex.ru>
---
.../clang-tidy/checkers/readability/redundant-parentheses.cpp | 2 --
1 file changed, 2 deletions(-)
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 ac052b5b1d582..635a4666362fc 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
@@ -112,10 +112,8 @@ struct Iter {
};
void memberAccessOnParen(Stream &s, Iter it) {
- // (s << "x").str() -- parens required for method chaining; no warning.
(s << "x").str();
- // (*it).x -- parens required before member access; no warning.
auto v = (*it).x;
}
More information about the cfe-commits
mailing list