[clang-tools-extra] [clang-tidy] add readability-redundant-parentheses (PR #159911)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 21 13:33:31 PDT 2025
================
@@ -0,0 +1,54 @@
+
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RedundantParenthesesCheck.h"
+#include "clang/AST/Expr.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include <cassert>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER_P(ParenExpr, subExpr, ast_matchers::internal::Matcher<Expr>,
+ InnerMatcher) {
+ return InnerMatcher.matches(*Node.getSubExpr(), Finder, Builder);
+}
+
+} // namespace
+
+void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ parenExpr(subExpr(anyOf(parenExpr(), integerLiteral(), floatLiteral(),
+ characterLiteral(), cxxBoolLiteral(),
+ stringLiteral(), declRefExpr())),
+ unless(
+ // sizeof(...) is common used.
+ hasParent(unaryExprOrTypeTraitExpr())))
+ .bind("dup"),
+ this);
+}
+
+void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
+ assert(PE);
+ const Expr *E = PE->getSubExpr();
+ if (PE->getLParen().isMacroID() || PE->getRParen().isMacroID() ||
+ E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID())
+ return;
----------------
vbvictor wrote:
Could we move whole logic to matchers and check for macro there?
We really need to create a generic matcher for `isInMacro`.
https://github.com/llvm/llvm-project/pull/159911
More information about the cfe-commits
mailing list