https://github.com/and2049 updated https://github.com/llvm/llvm-project/pull/223512
>From 3f51e48ad299eab6722e1cc20da4e278b8e97fd5 Mon Sep 17 00:00:00 2001 From: andre sun <[email protected]> Date: Mon, 14 Sep 2026 15:52:48 -0400 Subject: [PATCH 1/3] [clang-tidy] Fix readability-redundant-parentheses false positive on typeof The required parentheses of typeof/typeof_unqual operands are no longer flagged as redundant. Fixes #220899. --- .../readability/RedundantParenthesesCheck.cpp | 12 +++++++++++- clang-tools-extra/docs/ReleaseNotes.md | 4 ++++ .../checkers/readability/redundant-parentheses-c23.c | 10 ++++++++++ .../checkers/readability/redundant-parentheses.cpp | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp index 8435c438360a5..bbbc42a325da9 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp @@ -10,6 +10,7 @@ #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" @@ -33,6 +34,13 @@ AST_MATCHER(ParenExpr, isInMacro) { E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID(); } +AST_MATCHER(ParenExpr, isTypeOfArgument) { + return llvm::any_of(Finder->getASTContext().getParents(Node), + [](const DynTypedNode &Parent) { + return Parent.get<TypeOfExprTypeLoc>() != nullptr; + }); +} + } // namespace static FixItHint createSpacedRemoval(SourceLocation Loc, @@ -79,7 +87,9 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) { arraySubscriptExpr())), unless(anyOf(isInMacro(), // sizeof(...) is common used. - hasParent(unaryExprOrTypeTraitExpr())))) + hasParent(unaryExprOrTypeTraitExpr()), + // typeof(...) parentheses are required syntax. + isTypeOfArgument()))) .bind("dup"), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index e865792b05ff5..8c3472265495b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -256,6 +256,10 @@ infrastructure are described first, followed by tool-specific sections. exclusively for overload resolution. Added the {option}`IgnoredTypes` option to allow customizing the set of ignored types. +- 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. + - Improved {doc}`readability-trailing-comma <clang-tidy/checks/readability/trailing-comma>` check: diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c new file mode 100644 index 0000000000000..06be8140c2d32 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c @@ -0,0 +1,10 @@ +// RUN: %check_clang_tidy -std=c23-or-later %s readability-redundant-parentheses %t + +void typeofOperand(void) { + typeof(1) a; + typeof_unqual(1) b; + typeof(a) c; + typeof((2)) d; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant parentheses around expression [readability-redundant-parentheses] + // CHECK-FIXES: typeof(2) d; +} 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 bd6799e512087..aa65b897ae6da 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 @@ -61,6 +61,7 @@ void exceptions() { alignof((3)); // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant parentheses around expression [readability-redundant-parentheses] // CHECK-FIXES: alignof(3); + __typeof__(1) t; } namespace std { >From 0eef76635c4eadd6bb3aaafb865e3f125d603c3c Mon Sep 17 00:00:00 2001 From: andre sun <[email protected]> Date: Wed, 16 Sep 2026 11:15:32 -0400 Subject: [PATCH 2/3] [clang-tidy] Match typeof operand parent with a TypeLoc matcher Replace the explicit parent traversal with a TypeLoc matcher with hasParent. --- .../clang-tidy/readability/RedundantParenthesesCheck.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp index bbbc42a325da9..6ea0624e6b76f 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp @@ -34,11 +34,8 @@ AST_MATCHER(ParenExpr, isInMacro) { E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID(); } -AST_MATCHER(ParenExpr, isTypeOfArgument) { - return llvm::any_of(Finder->getASTContext().getParents(Node), - [](const DynTypedNode &Parent) { - return Parent.get<TypeOfExprTypeLoc>() != nullptr; - }); +AST_MATCHER(TypeLoc, isTypeOfExprTypeLoc) { + return !Node.getAs<TypeOfExprTypeLoc>().isNull(); } } // namespace @@ -89,7 +86,7 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) { // sizeof(...) is common used. hasParent(unaryExprOrTypeTraitExpr()), // typeof(...) parentheses are required syntax. - isTypeOfArgument()))) + hasParent(typeLoc(isTypeOfExprTypeLoc()))))) .bind("dup"), this); } >From 420dacbe116b450523abcc9202e5960e73a4c323 Mon Sep 17 00:00:00 2001 From: andre sun <[email protected]> Date: Wed, 16 Sep 2026 11:51:59 -0400 Subject: [PATCH 3/3] [clang-tidy] Handle qualified typeof operands Look through the QualifiedTypeLoc wrapper before the kind check. --- .../clang-tidy/readability/RedundantParenthesesCheck.cpp | 2 +- .../checkers/readability/redundant-parentheses-c23.c | 6 ++++++ .../checkers/readability/redundant-parentheses.cpp | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp index 6ea0624e6b76f..bc1d10586da8a 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp @@ -35,7 +35,7 @@ AST_MATCHER(ParenExpr, isInMacro) { } AST_MATCHER(TypeLoc, isTypeOfExprTypeLoc) { - return !Node.getAs<TypeOfExprTypeLoc>().isNull(); + return !Node.getUnqualifiedLoc().getAs<TypeOfExprTypeLoc>().isNull(); } } // namespace diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c index 06be8140c2d32..024b1bd6ae96c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses-c23.c @@ -7,4 +7,10 @@ void typeofOperand(void) { typeof((2)) d; // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant parentheses around expression [readability-redundant-parentheses] // CHECK-FIXES: typeof(2) d; + const typeof(a) e = a; + volatile typeof_unqual(1) f; + typeof(a) const g = a; + const typeof((3)) h = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: redundant parentheses around expression [readability-redundant-parentheses] + // CHECK-FIXES: const typeof(3) h = 3; } 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 aa65b897ae6da..c6275be9f2498 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 @@ -62,6 +62,7 @@ void exceptions() { // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant parentheses around expression [readability-redundant-parentheses] // CHECK-FIXES: alignof(3); __typeof__(1) t; + const __typeof__(1) ct = 1; } namespace std { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
