[clang-tools-extra] [clang-tidy] False negatives readability-redundant-parantheses member of struct (PR #187054)
Mir Immad via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 18 04:24:13 PDT 2026
https://github.com/mirimmad updated https://github.com/llvm/llvm-project/pull/187054
>From 6c083f9ec004bc054829fd1cc8df9b9de4a408f4 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Tue, 17 Mar 2026 21:57:00 +0530
Subject: [PATCH 1/4] match against 'memberExpr'
---
.../clang-tidy/readability/RedundantParenthesesCheck.cpp | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index bb993a60c9d4e..38cca7790670a 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -54,7 +54,11 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
parenExpr(subExpr(anyOf(
parenExpr(), ConstantExpr,
declRefExpr(to(namedDecl(unless(
- matchers::matchesAnyListedRegexName(AllowedDecls))))))),
+ matchers::matchesAnyListedRegexName(AllowedDecls))))),
+ memberExpr()
+
+ )),
+
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()))))
>From 88eaf77cd3d52a4fb50a908e6351dd5043ab3620 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Wed, 18 Mar 2026 12:08:40 +0530
Subject: [PATCH 2/4] add tests
---
.../readability/RedundantParenthesesCheck.cpp | 5 +---
.../readability/redundant-parentheses.cpp | 24 +++++++++++++++++++
2 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 38cca7790670a..fa69c4b72eeb6 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -55,10 +55,7 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
parenExpr(), ConstantExpr,
declRefExpr(to(namedDecl(unless(
matchers::matchesAnyListedRegexName(AllowedDecls))))),
- memberExpr()
-
- )),
-
+ memberExpr())),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()))))
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 c77608c66469c..7e3d4b4d61f29 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
@@ -71,3 +71,27 @@ void ignoreStdMaxMin() {
(std::max)(1,2);
(std::min)(1,2);
}
+
+struct Foo
+{
+ bool x;
+ void foo()
+ {
+ if ((x)) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if (x) {
+ }
+ if((this->x)) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if(this->x) {
+ }
+ }
+};
+
+void memberExpr() {
+ Foo foo{};
+ if ((foo.x)) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if (foo.x) {
+ }
+}
\ No newline at end of file
>From 154bdc088992c033ab4a5e0326780091254a703d Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Wed, 18 Mar 2026 12:30:50 +0530
Subject: [PATCH 3/4] add a new line at EOF
---
.../clang-tidy/checkers/readability/redundant-parentheses.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 7e3d4b4d61f29..8f977d9ab7708 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
@@ -94,4 +94,4 @@ void memberExpr() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
// CHECK-FIXES: if (foo.x) {
}
-}
\ No newline at end of file
+}
>From a09c815efd4e2a8e35ed427ec81bb8a6f6a4f4f6 Mon Sep 17 00:00:00 2001
From: Immad Mir <mirimmad17 at gmail.com>
Date: Wed, 18 Mar 2026 16:53:46 +0530
Subject: [PATCH 4/4] match against callexpr and add more tests
---
.../readability/RedundantParenthesesCheck.cpp | 2 +-
.../readability/redundant-parentheses.cpp | 26 +++++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index fa69c4b72eeb6..9b3948a1c50c0 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -55,7 +55,7 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
parenExpr(), ConstantExpr,
declRefExpr(to(namedDecl(unless(
matchers::matchesAnyListedRegexName(AllowedDecls))))),
- memberExpr())),
+ memberExpr(), callExpr())),
unless(anyOf(isInMacro(),
// sizeof(...) is common used.
hasParent(unaryExprOrTypeTraitExpr()))))
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 8f977d9ab7708..ef4f7badd8b88 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
@@ -75,6 +75,9 @@ void ignoreStdMaxMin() {
struct Foo
{
bool x;
+ struct Y {
+ bool z;
+ } y;
void foo()
{
if ((x)) {
@@ -86,6 +89,14 @@ struct Foo
// CHECK-FIXES: if(this->x) {
}
}
+ bool bar() {
+ return true;
+ }
+
+ Y fooBar() {
+ Y y{};
+ return y;
+ }
};
void memberExpr() {
@@ -94,4 +105,19 @@ void memberExpr() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
// CHECK-FIXES: if (foo.x) {
}
+
+ if ((foo.y.z)) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if (foo.y.z) {
+ }
+
+ if ((foo.bar())) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if (foo.bar()) {
+ }
+
+ if((foo.fooBar().z)) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant parentheses around expression [readability-redundant-parentheses]
+ // CHECK-FIXES: if(foo.fooBar().z) {
+ }
}
More information about the cfe-commits
mailing list