[NFC] use hasAnyOperatorName and hasAnyOverloadedOperatorName functions in clang-tidy matchers

This commit is contained in:
Nathan James 2020-03-10 00:42:21 +00:00
parent a7a3751775
commit 97572fa6e9
24 changed files with 79 additions and 129 deletions

View File

@ -50,7 +50,7 @@ void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
// Match [=!]= with a zero on one side and a string.find on the other.
binaryOperator(
anyOf(hasOperatorName("=="), hasOperatorName("!=")),
hasAnyOperatorName("==", "!="),
hasEitherOperand(ignoringParenImpCasts(ZeroLiteral)),
hasEitherOperand(ignoringParenImpCasts(StringFind.bind("findexpr"))))
.bind("expr"),

View File

@ -19,15 +19,11 @@ namespace bugprone {
void IntegerDivisionCheck::registerMatchers(MatchFinder *Finder) {
const auto IntType = hasType(isInteger());
const auto BinaryOperators = binaryOperator(anyOf(
hasOperatorName("%"), hasOperatorName("<<"), hasOperatorName(">>"),
hasOperatorName("<<"), hasOperatorName("^"), hasOperatorName("|"),
hasOperatorName("&"), hasOperatorName("||"), hasOperatorName("&&"),
hasOperatorName("<"), hasOperatorName(">"), hasOperatorName("<="),
hasOperatorName(">="), hasOperatorName("=="), hasOperatorName("!=")));
const auto BinaryOperators = binaryOperator(
hasAnyOperatorName("%", "<<", ">>", "<<", "^", "|", "&", "||", "&&", "<",
">", "<=", ">=", "==", "!="));
const auto UnaryOperators =
unaryOperator(anyOf(hasOperatorName("~"), hasOperatorName("!")));
const auto UnaryOperators = unaryOperator(hasAnyOperatorName("~", "!"));
const auto Exceptions =
anyOf(BinaryOperators, conditionalOperator(), binaryConditionalOperator(),

View File

@ -28,8 +28,7 @@ void MisplacedPointerArithmeticInAllocCheck::registerMatchers(
hasInitializer(ignoringParenImpCasts(
declRefExpr(hasDeclaration(AllocFunc)))));
const auto AdditiveOperator =
binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-")));
const auto AdditiveOperator = binaryOperator(hasAnyOperatorName("+", "-"));
const auto IntExpr = expr(hasType(isInteger()));

View File

@ -29,9 +29,7 @@ void MisplacedWideningCastCheck::storeOptions(
void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {
const auto Calc =
expr(anyOf(binaryOperator(
anyOf(hasOperatorName("+"), hasOperatorName("-"),
hasOperatorName("*"), hasOperatorName("<<"))),
expr(anyOf(binaryOperator(hasAnyOperatorName("+", "-", "*", "<<")),
unaryOperator(hasOperatorName("~"))),
hasType(isInteger()))
.bind("Calc");

View File

@ -48,8 +48,7 @@ void PosixReturnCheck::registerMatchers(MatchFinder *Finder) {
this);
Finder->addMatcher(
binaryOperator(
anyOf(hasOperatorName("=="), hasOperatorName("!="),
hasOperatorName("<="), hasOperatorName("<")),
hasAnyOperatorName("==", "!=", "<=", "<"),
hasLHS(callExpr(callee(functionDecl(
anyOf(matchesName("^::posix_"), matchesName("^::pthread_")),
unless(hasName("::posix_openpt")))))),

View File

@ -28,7 +28,7 @@ void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) {
.bind("sizeof"),
// Ignore ARRAYSIZE(<array of containers>) pattern.
unless(hasAncestor(binaryOperator(
anyOf(hasOperatorName("/"), hasOperatorName("%")),
hasAnyOperatorName("/", "%"),
hasLHS(ignoringParenCasts(sizeOfExpr(expr()))),
hasRHS(ignoringParenCasts(equalsBoundNode("sizeof"))))))),
this);

View File

@ -231,10 +231,7 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
binaryOperator(
anyOf(hasOperatorName("=="), hasOperatorName("!="),
hasOperatorName("<"), hasOperatorName("<="),
hasOperatorName(">"), hasOperatorName(">="),
hasOperatorName("+"), hasOperatorName("-")),
hasAnyOperatorName("==", "!=", "<", "<=", ">", ">=", "+", "-"),
hasEitherOperand(expr(anyOf(
ignoringParenImpCasts(SizeOfExpr),
ignoringParenImpCasts(binaryOperator(

View File

@ -20,8 +20,7 @@ namespace bugprone {
void StringIntegerAssignmentCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
cxxOperatorCallExpr(
anyOf(hasOverloadedOperatorName("="),
hasOverloadedOperatorName("+=")),
hasAnyOverloadedOperatorName("=", "+="),
callee(cxxMethodDecl(ofClass(classTemplateSpecializationDecl(
hasName("::std::basic_string"),
hasTemplateArgument(0, refersToType(hasCanonicalType(

View File

@ -131,7 +131,7 @@ void SuspiciousEnumUsageCheck::registerMatchers(MatchFinder *Finder) {
this);
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("|")),
binaryOperator(hasAnyOperatorName("+", "|"),
hasLHS(enumExpr("lhsExpr", "enumDecl")),
hasRHS(expr(enumExpr("rhsExpr", ""),
ignoringImpCasts(hasType(
@ -139,16 +139,15 @@ void SuspiciousEnumUsageCheck::registerMatchers(MatchFinder *Finder) {
this);
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("|")),
binaryOperator(hasAnyOperatorName("+", "|"),
hasEitherOperand(
expr(hasType(isInteger()), unless(enumExpr("", "")))),
hasEitherOperand(enumExpr("enumExpr", "enumDecl"))),
this);
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("|="), hasOperatorName("+=")),
hasRHS(enumExpr("enumExpr", "enumDecl"))),
this);
Finder->addMatcher(binaryOperator(hasAnyOperatorName("|=", "+="),
hasRHS(enumExpr("enumExpr", "enumDecl"))),
this);
}
void SuspiciousEnumUsageCheck::checkSuspiciousBitmaskUsage(

View File

@ -116,9 +116,8 @@ void SuspiciousStringCompareCheck::registerMatchers(MatchFinder *Finder) {
whileStmt(hasCondition(StringCompareCallExpr)),
doStmt(hasCondition(StringCompareCallExpr)),
forStmt(hasCondition(StringCompareCallExpr)),
binaryOperator(
anyOf(hasOperatorName("&&"), hasOperatorName("||")),
hasEitherOperand(StringCompareCallExpr))))
binaryOperator(hasAnyOperatorName("&&", "||"),
hasEitherOperand(StringCompareCallExpr))))
.bind("missing-comparison"),
this);
}

View File

@ -40,9 +40,9 @@ void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
// Self-check: Code compares something with 'this' pointer. We don't check
// whether it is actually the parameter what we compare.
const auto HasNoSelfCheck = cxxMethodDecl(unless(hasDescendant(
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
has(ignoringParenCasts(cxxThisExpr()))))));
const auto HasNoSelfCheck = cxxMethodDecl(unless(
hasDescendant(binaryOperator(hasAnyOperatorName("==", "!="),
has(ignoringParenCasts(cxxThisExpr()))))));
// Both copy-and-swap and copy-and-move method creates a copy first and
// assign it to 'this' with swap or move.

View File

@ -276,13 +276,11 @@ void UseAfterMoveFinder::getDeclRefs(
.bind("declref");
addDeclRefs(match(findAll(DeclRefMatcher), *S->getStmt(), *Context));
addDeclRefs(match(
findAll(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("*"),
hasOverloadedOperatorName("->"),
hasOverloadedOperatorName("[]")),
hasArgument(0, DeclRefMatcher))
.bind("operator")),
*S->getStmt(), *Context));
addDeclRefs(match(findAll(cxxOperatorCallExpr(
hasAnyOverloadedOperatorName("*", "->", "[]"),
hasArgument(0, DeclRefMatcher))
.bind("operator")),
*S->getStmt(), *Context));
}
}

View File

@ -18,8 +18,7 @@ namespace tidy {
namespace cert {
void PostfixOperatorCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(functionDecl(anyOf(hasOverloadedOperatorName("++"),
hasOverloadedOperatorName("--")),
Finder->addMatcher(functionDecl(hasAnyOverloadedOperatorName("++", "--"),
unless(isInstantiated()))
.bind("decl"),
this);

View File

@ -27,16 +27,13 @@ void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
// Flag all operators +, -, +=, -=, ++, -- that result in a pointer
Finder->addMatcher(
binaryOperator(
anyOf(hasOperatorName("+"), hasOperatorName("-"),
hasOperatorName("+="), hasOperatorName("-=")),
AllPointerTypes,
hasAnyOperatorName("+", "-", "+=", "-="), AllPointerTypes,
unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit()))))))
.bind("expr"),
this);
Finder->addMatcher(
unaryOperator(anyOf(hasOperatorName("++"), hasOperatorName("--")),
hasType(pointerType()))
unaryOperator(hasAnyOperatorName("++", "--"), hasType(pointerType()))
.bind("expr"),
this);

View File

@ -48,9 +48,7 @@ void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
// Match binary bitwise operations on signed integer arguments.
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("^"), hasOperatorName("|"),
hasOperatorName("&"), hasOperatorName("^="),
hasOperatorName("|="), hasOperatorName("&=")),
binaryOperator(hasAnyOperatorName("^", "|", "&", "^=", "|=", "&="),
unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
@ -62,8 +60,7 @@ void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
// Shifting and complement is not allowed for any signed integer type because
// the sign bit may corrupt the result.
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("<<"), hasOperatorName(">>"),
hasOperatorName("<<="), hasOperatorName(">>=")),
binaryOperator(hasAnyOperatorName("<<", ">>", "<<=", ">>="),
hasEitherOperand(SignedIntegerOperand),
hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
.bind("binary-sign-interference"),

View File

@ -532,14 +532,12 @@ static bool retrieveSymbolicExpr(const MatchFinder::MatchResult &Result,
static ast_matchers::internal::Matcher<Expr>
matchBinOpIntegerConstantExpr(StringRef Id) {
const auto BinOpCstExpr =
expr(
anyOf(binaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("|"),
hasOperatorName("&")),
hasEitherOperand(matchSymbolicExpr(Id)),
hasEitherOperand(matchIntegerConstantExpr(Id))),
binaryOperator(hasOperatorName("-"),
hasLHS(matchSymbolicExpr(Id)),
hasRHS(matchIntegerConstantExpr(Id)))))
expr(anyOf(binaryOperator(hasAnyOperatorName("+", "|", "&"),
hasEitherOperand(matchSymbolicExpr(Id)),
hasEitherOperand(matchIntegerConstantExpr(Id))),
binaryOperator(hasOperatorName("-"),
hasLHS(matchSymbolicExpr(Id)),
hasRHS(matchIntegerConstantExpr(Id)))))
.bind(Id);
return ignoringParenImpCasts(BinOpCstExpr);
}
@ -594,10 +592,7 @@ matchRelationalIntegerConstantExpr(StringRef Id) {
const auto OverloadedOperatorExpr =
cxxOperatorCallExpr(
anyOf(hasOverloadedOperatorName("=="),
hasOverloadedOperatorName("!="), hasOverloadedOperatorName("<"),
hasOverloadedOperatorName("<="), hasOverloadedOperatorName(">"),
hasOverloadedOperatorName(">=")),
hasAnyOverloadedOperatorName("==", "!=", "<", "<=", ">", ">="),
// Filter noisy false positives.
unless(isMacro()), unless(isInTemplateInstantiation()))
.bind(OverloadId);
@ -833,11 +828,9 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
// Binary with equivalent operands, like (X != 2 && X != 2).
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("-"), hasOperatorName("/"),
hasOperatorName("%"), hasOperatorName("|"),
hasOperatorName("&"), hasOperatorName("^"),
isComparisonOperator(), hasOperatorName("&&"),
hasOperatorName("||"), hasOperatorName("=")),
binaryOperator(anyOf(isComparisonOperator(),
hasAnyOperatorName("-", "/", "%", "|", "&", "^",
"&&", "||", "=")),
operandsAreEquivalent(),
// Filter noisy false positives.
unless(isInTemplateInstantiation()),
@ -852,9 +845,7 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
// Logical or bitwise operator with equivalent nested operands, like (X && Y
// && X) or (X && (Y && X))
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("|"), hasOperatorName("&"),
hasOperatorName("||"), hasOperatorName("&&"),
hasOperatorName("^")),
binaryOperator(hasAnyOperatorName("|", "&", "||", "&&", "^"),
nestedOperandsAreEquivalent(),
// Filter noisy false positives.
unless(isInTemplateInstantiation()),
@ -873,30 +864,20 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
this);
// Overloaded operators with equivalent operands.
Finder->addMatcher(
cxxOperatorCallExpr(
anyOf(
hasOverloadedOperatorName("-"), hasOverloadedOperatorName("/"),
hasOverloadedOperatorName("%"), hasOverloadedOperatorName("|"),
hasOverloadedOperatorName("&"), hasOverloadedOperatorName("^"),
hasOverloadedOperatorName("=="), hasOverloadedOperatorName("!="),
hasOverloadedOperatorName("<"), hasOverloadedOperatorName("<="),
hasOverloadedOperatorName(">"), hasOverloadedOperatorName(">="),
hasOverloadedOperatorName("&&"), hasOverloadedOperatorName("||"),
hasOverloadedOperatorName("=")),
parametersAreEquivalent(),
// Filter noisy false positives.
unless(isMacro()), unless(isInTemplateInstantiation()))
.bind("call"),
this);
Finder->addMatcher(cxxOperatorCallExpr(
hasAnyOverloadedOperatorName(
"-", "/", "%", "|", "&", "^", "==", "!=", "<",
"<=", ">", ">=", "&&", "||", "="),
parametersAreEquivalent(),
// Filter noisy false positives.
unless(isMacro()), unless(isInTemplateInstantiation()))
.bind("call"),
this);
// Overloaded operators with equivalent operands.
Finder->addMatcher(
cxxOperatorCallExpr(
anyOf(hasOverloadedOperatorName("|"), hasOverloadedOperatorName("&"),
hasOverloadedOperatorName("||"),
hasOverloadedOperatorName("&&"),
hasOverloadedOperatorName("^")),
hasAnyOverloadedOperatorName("|", "&", "||", "&&", "^"),
nestedParametersAreEquivalent(), argumentCountIs(2),
// Filter noisy false positives.
unless(isMacro()), unless(isInTemplateInstantiation()))
@ -910,9 +891,8 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
has(unaryOperator(
hasOperatorName("!"),
hasUnaryOperand(ignoringParenImpCasts(binaryOperator(
anyOf(hasOperatorName("|"), hasOperatorName("&")),
hasLHS(anyOf(binaryOperator(anyOf(hasOperatorName("|"),
hasOperatorName("&"))),
hasAnyOperatorName("|", "&"),
hasLHS(anyOf(binaryOperator(hasAnyOperatorName("|", "&")),
integerLiteral())),
hasRHS(integerLiteral())))))
.bind("logical-bitwise-confusion"))),
@ -971,13 +951,13 @@ void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) {
// Match expressions like: x < 2 && x > 2.
const auto ComparisonLeft = matchRelationalIntegerConstantExpr("lhs");
const auto ComparisonRight = matchRelationalIntegerConstantExpr("rhs");
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("||"), hasOperatorName("&&")),
hasLHS(ComparisonLeft), hasRHS(ComparisonRight),
// Already reported as redundant.
unless(operandsAreEquivalent()))
.bind("comparisons-of-symbol-and-const"),
this);
Finder->addMatcher(binaryOperator(hasAnyOperatorName("||", "&&"),
hasLHS(ComparisonLeft),
hasRHS(ComparisonRight),
// Already reported as redundant.
unless(operandsAreEquivalent()))
.bind("comparisons-of-symbol-and-const"),
this);
}
void RedundantExpressionCheck::checkArithmeticExpr(

View File

@ -38,7 +38,7 @@ void StaticAssertCheck::registerMatchers(MatchFinder *Finder) {
.bind("castExpr")));
auto AssertExprRoot = anyOf(
binaryOperator(
anyOf(hasOperatorName("&&"), hasOperatorName("==")),
hasAnyOperatorName("&&", "=="),
hasEitherOperand(ignoringImpCasts(stringLiteral().bind("assertMSG"))),
anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)),
anything()))

View File

@ -192,10 +192,10 @@ void UseDefaultMemberInitCheck::storeOptions(
void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
auto Init =
anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
unaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-")),
unaryOperator(hasAnyOperatorName("+", "-"),
hasUnaryOperand(integerLiteral())),
floatLiteral(),
unaryOperator(anyOf(hasOperatorName("+"), hasOperatorName("-")),
unaryOperator(hasAnyOperatorName("+", "-"),
hasUnaryOperand(floatLiteral())),
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
initListExpr(), declRefExpr(to(enumConstantDecl())));

View File

@ -85,8 +85,7 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) {
expr(hasType(ValidContainer)).bind("STLObject"));
Finder->addMatcher(
cxxOperatorCallExpr(
anyOf(hasOverloadedOperatorName("=="),
hasOverloadedOperatorName("!=")),
hasAnyOverloadedOperatorName("==", "!="),
anyOf(allOf(hasArgument(0, WrongComparend), hasArgument(1, STLArg)),
allOf(hasArgument(0, STLArg), hasArgument(1, WrongComparend))),
unless(hasAncestor(

View File

@ -293,12 +293,11 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
.bind("implicitCastToBool"),
this);
auto boolComparison = binaryOperator(
anyOf(hasOperatorName("=="), hasOperatorName("!=")),
hasLHS(implicitCastFromBool), hasRHS(implicitCastFromBool));
auto boolOpAssignment =
binaryOperator(anyOf(hasOperatorName("|="), hasOperatorName("&=")),
hasLHS(expr(hasType(booleanType()))));
auto boolComparison = binaryOperator(hasAnyOperatorName("==", "!="),
hasLHS(implicitCastFromBool),
hasRHS(implicitCastFromBool));
auto boolOpAssignment = binaryOperator(hasAnyOperatorName("|=", "&="),
hasLHS(expr(hasType(booleanType()))));
auto bitfieldAssignment = binaryOperator(
hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
auto bitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer(

View File

@ -28,8 +28,7 @@ void NonConstParameterCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(declRefExpr().bind("Ref"), this);
// Analyse parameter usage in function.
Finder->addMatcher(stmt(anyOf(unaryOperator(anyOf(hasOperatorName("++"),
hasOperatorName("--"))),
Finder->addMatcher(stmt(anyOf(unaryOperator(hasAnyOperatorName("++", "--")),
binaryOperator(), callExpr(), returnStmt(),
cxxConstructExpr()))
.bind("Mark"),

View File

@ -81,7 +81,7 @@ void registerMatchersForGetEquals(MatchFinder *Finder,
// Matches against nullptr.
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
binaryOperator(hasAnyOperatorName("==", "!="),
hasEitherOperand(ignoringImpCasts(
anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
integerLiteral(equals(0))))),

View File

@ -150,11 +150,7 @@ void RedundantStringCStrCheck::registerMatchers(
// Detect: 's == str.c_str()' -> 's == str'
Finder->addMatcher(
cxxOperatorCallExpr(
anyOf(
hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"),
hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="),
hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="),
hasOverloadedOperatorName("+")),
hasAnyOverloadedOperatorName("<", ">", ">=", "<=", "!=", "==", "+"),
anyOf(allOf(hasArgument(0, StringExpr),
hasArgument(1, StringCStrCallExpr)),
allOf(hasArgument(0, StringCStrCallExpr),
@ -163,11 +159,11 @@ void RedundantStringCStrCheck::registerMatchers(
// Detect: 'dst += str.c_str()' -> 'dst += str'
// Detect: 's = str.c_str()' -> 's = str'
Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="),
hasOverloadedOperatorName("+=")),
hasArgument(0, StringExpr),
hasArgument(1, StringCStrCallExpr)),
this);
Finder->addMatcher(
cxxOperatorCallExpr(hasAnyOverloadedOperatorName("=", "+="),
hasArgument(0, StringExpr),
hasArgument(1, StringCStrCallExpr)),
this);
// Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
Finder->addMatcher(

View File

@ -38,7 +38,7 @@ void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
// Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0.
Finder->addMatcher(
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
binaryOperator(hasAnyOperatorName("==", "!="),
hasEitherOperand(StrCompare.bind("compare")),
hasEitherOperand(integerLiteral(equals(0)).bind("zero")))
.bind("match2"),