[clang-tidy][NFC] change patterns 'anyOf(..., anything())' to 'optionally(...)' (#143558)

Writing `optionally()` instead of `anyOf(..., anything())` lowers code
size and gives the author's intention better.
This commit is contained in:
Baranov Victor
2025-06-14 10:55:42 +03:00
committed by GitHub
parent 2e7fbb94bc
commit f46c44dbc0
5 changed files with 28 additions and 35 deletions

View File

@@ -702,17 +702,16 @@ void NotNullTerminatedResultCheck::registerMatchers(MatchFinder *Finder) {
return hasArgument(
CC.LengthPos,
allOf(
anyOf(
ignoringImpCasts(integerLiteral().bind(WrongLengthExprName)),
allOf(unless(hasDefinition(SizeOfCharExpr)),
allOf(CC.WithIncrease
? ignoringImpCasts(hasDefinition(HasIncOp))
: ignoringImpCasts(allOf(
unless(hasDefinition(HasIncOp)),
anyOf(hasDefinition(binaryOperator().bind(
UnknownLengthName)),
hasDefinition(anything())))),
AnyOfWrongLengthInit))),
anyOf(ignoringImpCasts(integerLiteral().bind(WrongLengthExprName)),
allOf(unless(hasDefinition(SizeOfCharExpr)),
allOf(CC.WithIncrease
? ignoringImpCasts(hasDefinition(HasIncOp))
: ignoringImpCasts(
allOf(unless(hasDefinition(HasIncOp)),
hasDefinition(optionally(
binaryOperator().bind(
UnknownLengthName))))),
AnyOfWrongLengthInit))),
expr().bind(LengthExprName)));
};

View File

@@ -24,14 +24,12 @@ void ExceptionBaseclassCheck::registerMatchers(MatchFinder *Finder) {
isSameOrDerivedFrom(hasName("::std::exception")))))))))),
// This condition is always true, but will bind to the
// template value if the thrown type is templated.
anyOf(has(expr(
hasType(substTemplateTypeParmType().bind("templ_type")))),
anything()),
optionally(has(
expr(hasType(substTemplateTypeParmType().bind("templ_type"))))),
// Bind to the declaration of the type of the value that
// is thrown. 'anything()' is necessary to always succeed
// in the 'eachOf' because builtin types are not
// 'namedDecl'.
eachOf(has(expr(hasType(namedDecl().bind("decl")))), anything()))
// is thrown. 'optionally' is necessary because builtin types
// are not 'namedDecl'.
optionally(has(expr(hasType(namedDecl().bind("decl"))))))
.bind("bad_throw"),
this);
}

View File

@@ -38,8 +38,7 @@ void StaticAssertCheck::registerMatchers(MatchFinder *Finder) {
binaryOperator(
hasAnyOperatorName("&&", "=="),
hasEitherOperand(ignoringImpCasts(stringLiteral().bind("assertMSG"))),
anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)),
anything()))
optionally(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast))))
.bind("assertExprRoot"),
IsAlwaysFalse);
auto NonConstexprFunctionCall =
@@ -52,12 +51,10 @@ void StaticAssertCheck::registerMatchers(MatchFinder *Finder) {
auto NonConstexprCode =
expr(anyOf(NonConstexprFunctionCall, NonConstexprVariableReference));
auto AssertCondition =
expr(
anyOf(expr(ignoringParenCasts(anyOf(
AssertExprRoot, unaryOperator(hasUnaryOperand(
ignoringParenCasts(AssertExprRoot)))))),
anything()),
unless(NonConstexprCode), unless(hasDescendant(NonConstexprCode)))
expr(optionally(expr(ignoringParenCasts(anyOf(
AssertExprRoot, unaryOperator(hasUnaryOperand(
ignoringParenCasts(AssertExprRoot))))))),
unless(NonConstexprCode), unless(hasDescendant(NonConstexprCode)))
.bind("condition");
auto Condition =
anyOf(ignoringParenImpCasts(callExpr(

View File

@@ -26,13 +26,12 @@ void UseBoolLiteralsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
void UseBoolLiteralsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
traverse(
TK_AsIs,
implicitCastExpr(
has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
hasImplicitDestinationType(qualType(booleanType())),
unless(isInTemplateInstantiation()),
anyOf(hasParent(explicitCastExpr().bind("cast")), anything()))),
traverse(TK_AsIs,
implicitCastExpr(
has(ignoringParenImpCasts(integerLiteral().bind("literal"))),
hasImplicitDestinationType(qualType(booleanType())),
unless(isInTemplateInstantiation()),
optionally(hasParent(explicitCastExpr().bind("cast"))))),
this);
Finder->addMatcher(

View File

@@ -348,8 +348,8 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
implicitCastExpr().bind("implicitCastFromBool"),
unless(hasParent(BitfieldConstruct)),
// Check also for nested casts, for example: bool -> int -> float.
anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
anything()),
optionally(
hasParent(implicitCastExpr().bind("furtherImplicitCast"))),
unless(isInTemplateInstantiation()),
unless(IsInCompilerGeneratedFunction))),
this);