https://github.com/cs25mtech12008 updated https://github.com/llvm/llvm-project/pull/196739
>From 696defd512a25ef3ab24a4ad21d02c4220817a1a Mon Sep 17 00:00:00 2001 From: cs25mtech12008 <[email protected]> Date: Sat, 9 May 2026 23:07:30 +0530 Subject: [PATCH 1/2] 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 <[email protected]> Date: Sun, 10 May 2026 03:23:25 +0530 Subject: [PATCH 2/2] [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); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
