[clang-tools-extra] [clang-tidy] Extend readability-redundant-parentheses to type (PR #196739)
Omkar Rasal via cfe-commits
cfe-commits at lists.llvm.org
Fri May 22 10:55:37 PDT 2026
https://github.com/cs25mtech12008 updated https://github.com/llvm/llvm-project/pull/196739
>From 696defd512a25ef3ab24a4ad21d02c4220817a1a Mon Sep 17 00:00:00 2001
From: cs25mtech12008 <cs25mtech12008 at iith.ac.in>
Date: Sat, 9 May 2026 23:07:30 +0530
Subject: [PATCH 1/4] added check for the parentheses declaration
---
.../readability/RedundantParenthesesCheck.cpp | 27 ++++++++++++++++---
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 9b3948a1c50c0..6d473ccf1c8df 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "clang/AST/TypeLoc.h"
#include "RedundantParenthesesCheck.h"
#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
@@ -61,13 +62,31 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
hasParent(unaryExprOrTypeTraitExpr()))))
.bind("dup"),
this);
+
+ Finder->addMatcher(typeLoc(loc(parenType())).bind("parentheses-decl"), this);
}
void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
- const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
- diag(PE->getBeginLoc(), "redundant parentheses around expression")
- << FixItHint::CreateRemoval(PE->getLParen())
- << FixItHint::CreateRemoval(PE->getRParen());
+ if (const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup")) {
+
+ diag(PE->getBeginLoc(), "redundant parentheses around expression")
+ << FixItHint::CreateRemoval(PE->getLParen())
+ << FixItHint::CreateRemoval(PE->getRParen());
+ return ;
+ }
+
+ if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("parentheses-decl")) {
+ ParenTypeLoc ParenType = TL->getAs<ParenTypeLoc>();
+
+ if (ParenType.isNull())
+ return;
+
+ diag(ParenType.getLParenLoc(), "redundant parentheses in declaration")
+ << FixItHint::CreateRemoval(ParenType.getLParenLoc())
+ << FixItHint::CreateRemoval(ParenType.getRParenLoc());
+ return;
+ }
+
}
} // namespace clang::tidy::readability
>From b1eb370cc90287e2fa7984ec6c2264362774bd51 Mon Sep 17 00:00:00 2001
From: cs25mtech12008 <cs25mtech12008 at iith.ac.in>
Date: Sun, 10 May 2026 03:23:25 +0530
Subject: [PATCH 2/4] [clang-tidy] Addressed review comments for declaration
parentheses for readability
---
.../readability/RedundantParenthesesCheck.cpp | 15 +++++++++------
clang-tools-extra/docs/ReleaseNotes.rst | 3 +++
.../checks/readability/redundant-parentheses.rst | 6 ++++++
.../readability/redundant-parentheses.cpp | 12 ++++++++++++
4 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 6d473ccf1c8df..583f3d1941556 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -6,14 +6,15 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/AST/TypeLoc.h"
#include "RedundantParenthesesCheck.h"
#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"
+#include "clang/Lex/Lexer.h"
#include <cassert>
using namespace clang::ast_matchers;
@@ -68,25 +69,27 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup")) {
-
diag(PE->getBeginLoc(), "redundant parentheses around expression")
<< FixItHint::CreateRemoval(PE->getLParen())
<< FixItHint::CreateRemoval(PE->getRParen());
- return ;
+ return;
}
if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("parentheses-decl")) {
- ParenTypeLoc ParenType = TL->getAs<ParenTypeLoc>();
+ const auto ParenType = TL->getAs<ParenTypeLoc>();
if (ParenType.isNull())
return;
-
+ const QualType InnerLocType = ParenType.getInnerLoc().getType();
+ if (InnerLocType->isPointerType() || InnerLocType->isReferenceType() ||
+ InnerLocType->isMemberPointerType() || InnerLocType->isFunctionType() ||
+ InnerLocType->isArrayType())
+ return;
diag(ParenType.getLParenLoc(), "redundant parentheses in declaration")
<< FixItHint::CreateRemoval(ParenType.getLParenLoc())
<< FixItHint::CreateRemoval(ParenType.getRParenLoc());
return;
}
-
}
} // namespace clang::tidy::readability
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 51251eacbcd5e..45fee6f634327 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -641,6 +641,9 @@ Changes in existing checks
note to suggest materializing the temporary range when iterating over temporary
range expressions or initializer lists, as reusing them directly could be unsafe.
+- Improved :doc:`readability-redundant-parentheses
+ <clang-tidy/checks/readability/redundant-parentheses>` check to diagnose
+ redundant parentheses in declarations such as int (x) and int (f(int(arg)))
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
index b9c50c5b59889..90ffcab679b23 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
@@ -40,3 +40,9 @@ Options
Some STL library functions may have the same name as widely used function-like
macro. For example, ``std::max`` and ``max`` macro. A workaround to distinguish
them is adding parentheses around functions to prevent function-like macro.
+
+The check diagnoses redundant parentheses in declarations and keeps earlier changes as it is
+.. code-block:: c++
+
+ int (x); // becomes int x;
+ void f(int (arg)); // becomes void f(int arg);
\ No newline at end of file
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..11e46ae8a6a6f 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
@@ -121,3 +121,15 @@ void memberExpr() {
// CHECK-FIXES: if (foo.fooBar().z) {
}
}
+
+int (x);
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-FIXES: int x;
+
+void f(int (arg));
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant parentheses in declaration
+// CHECK-FIXES: void f(int arg);
+
+//Negative Test cases for redundant parentheses in declaration
+int (*functionPtr)(int);
+int (*array[2])(int);
>From 1ca6a811f1df2442101a568ac1af72eb37692d58 Mon Sep 17 00:00:00 2001
From: cs25mtech12008 <cs25mtech12008 at iith.ac.in>
Date: Mon, 11 May 2026 21:09:11 +0530
Subject: [PATCH 3/4] [clang-tidy] Address review feedback for redundant
parentheses
---
.../readability/RedundantParenthesesCheck.cpp | 18 +++----
clang-tools-extra/docs/ReleaseNotes.rst | 8 +--
.../readability/redundant-parentheses.rst | 7 +--
.../readability/redundant-parentheses.cpp | 49 ++++++++++++++++++-
4 files changed, 65 insertions(+), 17 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 583f3d1941556..ba08b105f2ed3 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -74,20 +74,20 @@ void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
<< FixItHint::CreateRemoval(PE->getRParen());
return;
}
-
if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("parentheses-decl")) {
const auto ParenType = TL->getAs<ParenTypeLoc>();
-
if (ParenType.isNull())
return;
- const QualType InnerLocType = ParenType.getInnerLoc().getType();
- if (InnerLocType->isPointerType() || InnerLocType->isReferenceType() ||
- InnerLocType->isMemberPointerType() || InnerLocType->isFunctionType() ||
- InnerLocType->isArrayType())
+ SourceLocation LParen = ParenType.getLParenLoc();
+ SourceLocation RParen = ParenType.getRParenLoc();
+ if (LParen.isMacroID() || RParen.isMacroID())
+ return;
+ const TypeLoc Inner = ParenType.getInnerLoc();
+ if (Inner.getType()->isFunctionType() ||
+ Inner.getType()->isFunctionPointerType())
return;
- diag(ParenType.getLParenLoc(), "redundant parentheses in declaration")
- << FixItHint::CreateRemoval(ParenType.getLParenLoc())
- << FixItHint::CreateRemoval(ParenType.getRParenLoc());
+ diag(LParen, "redundant parentheses in declaration")
+ << FixItHint::CreateRemoval(LParen) << FixItHint::CreateRemoval(RParen);
return;
}
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 45fee6f634327..e4805ee10ca99 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -617,6 +617,11 @@ 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 diagnose
+ redundant parentheses in declarations such as ``int (x)`` and
+ ``int f(int (arg))``.
+
- Improved :doc:`readability-redundant-preprocessor
<clang-tidy/checks/readability/redundant-preprocessor>` check by fixing a
false positive for nested ``#if`` directives using different builtin
@@ -641,9 +646,6 @@ Changes in existing checks
note to suggest materializing the temporary range when iterating over temporary
range expressions or initializer lists, as reusing them directly could be unsafe.
-- Improved :doc:`readability-redundant-parentheses
- <clang-tidy/checks/readability/redundant-parentheses>` check to diagnose
- redundant parentheses in declarations such as int (x) and int (f(int(arg)))
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
index 90ffcab679b23..54dcbd23bd2db 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
@@ -41,8 +41,9 @@ Options
macro. For example, ``std::max`` and ``max`` macro. A workaround to distinguish
them is adding parentheses around functions to prevent function-like macro.
-The check diagnoses redundant parentheses in declarations and keeps earlier changes as it is
+The check also diagnoses redundant parentheses in simple declarations:
+
.. code-block:: c++
- int (x); // becomes int x;
- void f(int (arg)); // becomes void f(int arg);
\ No newline at end of file
+ int (x); // becomes int x;
+ void f(int (arg)); // becomes void f(int arg);
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 11e46ae8a6a6f..847e59bf5aabe 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
@@ -130,6 +130,51 @@ void f(int (arg));
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant parentheses in declaration
// CHECK-FIXES: void f(int arg);
-//Negative Test cases for redundant parentheses in declaration
+int ((nestedX));
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: redundant parentheses in declaration
+// CHECK-FIXES: int nestedX;
+
+void nestedParam(int ((arg)));
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: redundant parentheses in declaration
+// CHECK-FIXES: void nestedParam(int arg);
+
+int (&referenceVar) = x;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-FIXES: int &referenceVar = x;
+
+struct S {};
+int (S::*memberPtr);
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-FIXES: int S::*memberPtr;
+
+int (arrayVar)[2];
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-FIXES: int arrayVar[2];
+
+template <class T>
+void templatedParam(T (arg));
+// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: redundant parentheses in declaration
+// CHECK-FIXES: void templatedParam(T arg);
+
+template <class T>
+struct TemplateStruct {
+ T (member);
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-FIXES: T member;
+};
+
+// Negative cases.
int (*functionPtr)(int);
-int (*array[2])(int);
+void (*callback)(int);
+int (*arrayPtr[2])(int);
+#define DECL_WITH_PARENS(name) int (name)
+DECL_WITH_PARENS(macroVar);
+#define PAREN_NAME(name) (name)
+int PAREN_NAME(macroName);
+using AliasName = int;
+void instantiateTemplates() {
+ templatedParam<int>(0);
+ TemplateStruct<int> s;
+}
\ No newline at end of file
>From 5127f57ee1c17b380469060e802370fb7340daf6 Mon Sep 17 00:00:00 2001
From: cs25mtech12008 <cs25mtech12008 at iith.ac.in>
Date: Thu, 21 May 2026 22:58:46 +0530
Subject: [PATCH 4/4] [clang-tidy] Add tests and change in message
---
.../readability/RedundantParenthesesCheck.cpp | 9 +++---
.../readability/redundant-parentheses.cpp | 28 +++++++++++--------
2 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index ba08b105f2ed3..428e2353100f5 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -76,17 +76,16 @@ void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
}
if (const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("parentheses-decl")) {
const auto ParenType = TL->getAs<ParenTypeLoc>();
- if (ParenType.isNull())
- return;
- SourceLocation LParen = ParenType.getLParenLoc();
- SourceLocation RParen = ParenType.getRParenLoc();
+ assert(!ParenType.isNull() && "Expected ParenTypeLoc");
+ const SourceLocation LParen = ParenType.getLParenLoc();
+ const SourceLocation RParen = ParenType.getRParenLoc();
if (LParen.isMacroID() || RParen.isMacroID())
return;
const TypeLoc Inner = ParenType.getInnerLoc();
if (Inner.getType()->isFunctionType() ||
Inner.getType()->isFunctionPointerType())
return;
- diag(LParen, "redundant parentheses in declaration")
+ diag(LParen, "redundant parentheses in type")
<< FixItHint::CreateRemoval(LParen) << FixItHint::CreateRemoval(RParen);
return;
}
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 847e59bf5aabe..856deae4cf052 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
@@ -123,45 +123,45 @@ void memberExpr() {
}
int (x);
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in type
// CHECK-FIXES: int x;
void f(int (arg));
-// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant parentheses in type
// CHECK-FIXES: void f(int arg);
int ((nestedX));
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
-// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in type
+// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: redundant parentheses in type
// CHECK-FIXES: int nestedX;
void nestedParam(int ((arg)));
-// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant parentheses in declaration
-// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant parentheses in type
+// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: redundant parentheses in type
// CHECK-FIXES: void nestedParam(int arg);
int (&referenceVar) = x;
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in type
// CHECK-FIXES: int &referenceVar = x;
struct S {};
int (S::*memberPtr);
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in type
// CHECK-FIXES: int S::*memberPtr;
int (arrayVar)[2];
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in type
// CHECK-FIXES: int arrayVar[2];
template <class T>
void templatedParam(T (arg));
-// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: redundant parentheses in type
// CHECK-FIXES: void templatedParam(T arg);
template <class T>
struct TemplateStruct {
T (member);
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in declaration
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant parentheses in type
// CHECK-FIXES: T member;
};
@@ -177,4 +177,8 @@ using AliasName = int;
void instantiateTemplates() {
templatedParam<int>(0);
TemplateStruct<int> s;
-}
\ No newline at end of file
+}
+using Fn = void(int);
+Fn (*funcPtr);
+void (func)(int);
+int (*ptr)(int);
\ No newline at end of file
More information about the cfe-commits
mailing list