clang-tools 22.0.0git
RedundantParenthesesCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/Expr.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/ASTMatchers/ASTMatchersMacros.h"
14#include <cassert>
15
16using namespace clang::ast_matchers;
17
19
20namespace {
21
22AST_MATCHER_P(ParenExpr, subExpr, ast_matchers::internal::Matcher<Expr>,
23 InnerMatcher) {
24 return InnerMatcher.matches(*Node.getSubExpr(), Finder, Builder);
25}
26
27AST_MATCHER(ParenExpr, isInMacro) {
28 const Expr *E = Node.getSubExpr();
29 return Node.getLParen().isMacroID() || Node.getRParen().isMacroID() ||
30 E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID();
31}
32
33} // namespace
34
36 const auto ConstantExpr =
37 expr(anyOf(integerLiteral(), floatLiteral(), characterLiteral(),
38 cxxBoolLiteral(), stringLiteral(), cxxNullPtrLiteralExpr()));
39 Finder->addMatcher(
40 parenExpr(subExpr(anyOf(parenExpr(), ConstantExpr, declRefExpr())),
41 unless(anyOf(isInMacro(),
42 // sizeof(...) is common used.
43 hasParent(unaryExprOrTypeTraitExpr()))))
44 .bind("dup"),
45 this);
46}
47
48void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) {
49 const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup");
50 diag(PE->getBeginLoc(), "redundant parentheses around expression")
51 << FixItHint::CreateRemoval(PE->getLParen())
52 << FixItHint::CreateRemoval(PE->getRParen());
53}
54
55} // namespace clang::tidy::readability
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)