[clang-tidy][NFC] Enable readability-any-all-of check (#167134)

Closes https://github.com/llvm/llvm-project/issues/156161.
Assisted-by: Claude Sonnet 4.5 via Claude Code
This commit is contained in:
Baranov Victor 2025-12-03 08:56:24 +03:00 committed by GitHub
parent 822fc44998
commit d05370e686
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 105 additions and 173 deletions

View File

@ -32,8 +32,7 @@ Checks: >
-readability-qualified-auto,
-readability-simplify-boolean-expr,
-readability-static-definition-in-anonymous-namespace,
-readability-suspicious-call-argument,
-readability-use-anyofallof
-readability-suspicious-call-argument
CheckOptions:
- key: performance-move-const-arg.CheckTriviallyCopyableMove

View File

@ -478,11 +478,10 @@ bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
if (FileName.ends_with(Filter.Name)) {
if (Filter.LineRanges.empty())
return true;
for (const FileFilter::LineRange &Range : Filter.LineRanges) {
if (Range.first <= LineNumber && LineNumber <= Range.second)
return true;
}
return false;
return llvm::any_of(
Filter.LineRanges, [&](const FileFilter::LineRange &Range) {
return Range.first <= LineNumber && LineNumber <= Range.second;
});
}
}
return false;

View File

@ -75,12 +75,9 @@ static bool isFallthroughSwitchBranch(const SwitchBranch &Branch) {
if (!S)
return true;
for (const Attr *A : S->getAttrs()) {
if (isa<FallThroughAttr>(A))
return false;
}
return true;
return llvm::all_of(S->getAttrs(), [](const Attr *A) {
return !isa<FallThroughAttr>(A);
});
}
} Visitor;

View File

@ -44,18 +44,17 @@ AST_MATCHER(CXXRecordDecl, correctHandleCaptureThisLambda) {
if (Node.hasSimpleMoveAssignment())
return false;
for (const CXXConstructorDecl *C : Node.ctors()) {
if (C->isCopyOrMoveConstructor() && C->isDefaulted() && !C->isDeleted())
return false;
}
for (const CXXMethodDecl *M : Node.methods()) {
if (M->isCopyAssignmentOperator())
llvm::errs() << M->isDeleted() << "\n";
if (M->isCopyAssignmentOperator() && M->isDefaulted() && !M->isDeleted())
return false;
if (M->isMoveAssignmentOperator() && M->isDefaulted() && !M->isDeleted())
return false;
}
if (llvm::any_of(Node.ctors(), [](const CXXConstructorDecl *C) {
return C->isCopyOrMoveConstructor() && C->isDefaulted() &&
!C->isDeleted();
}))
return false;
if (llvm::any_of(Node.methods(), [](const CXXMethodDecl *M) {
return (M->isCopyAssignmentOperator() ||
M->isMoveAssignmentOperator()) &&
M->isDefaulted() && !M->isDeleted();
}))
return false;
// FIXME: find ways to identifier correct handle capture this lambda
return true;
}

View File

@ -1589,11 +1589,9 @@ static bool lazyMapOfSetsIntersectionExists(const MapTy &Map, const ElemTy &E1,
if (E1Iterator == Map.end() || E2Iterator == Map.end())
return false;
for (const auto &E1SetElem : E1Iterator->second)
if (E2Iterator->second.contains(E1SetElem))
return true;
return false;
return llvm::any_of(E1Iterator->second, [&E2Iterator](const auto &E1SetElem) {
return E2Iterator->second.contains(E1SetElem);
});
}
/// Implements the heuristic that marks two parameters related if there is

View File

@ -119,14 +119,9 @@ static bool isAtLeastOneCondVarChanged(const Decl *Func, const Stmt *LoopStmt,
if (isVarThatIsPossiblyChanged(Func, LoopStmt, Cond, Context))
return true;
for (const Stmt *Child : Cond->children()) {
if (!Child)
continue;
if (isAtLeastOneCondVarChanged(Func, LoopStmt, Child, Context))
return true;
}
return false;
return llvm::any_of(Cond->children(), [&](const Stmt *Child) {
return Child && isAtLeastOneCondVarChanged(Func, LoopStmt, Child, Context);
});
}
/// Return the variable names in `Cond`.
@ -240,10 +235,9 @@ static bool hasStaticLocalVariable(const Stmt *Cond) {
return true;
}
for (const Stmt *Child : Cond->children())
if (Child && hasStaticLocalVariable(Child))
return true;
return false;
return llvm::any_of(Cond->children(), [](const Stmt *Child) {
return Child && hasStaticLocalVariable(Child);
});
}
/// Tests if the loop condition `Cond` involves static local variables and

View File

@ -92,10 +92,9 @@ public:
return false;
}
bool VisitStmt(const Stmt *S) {
for (const Stmt *Child : S->children())
if (Child && Visit(Child))
return true;
return false;
return llvm::any_of(S->children(), [this](const Stmt *Child) {
return Child && Visit(Child);
});
}
};

View File

@ -19,10 +19,11 @@ namespace clang::tidy::cppcoreguidelines {
namespace {
AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt,
ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
for (const DeclStmt *Stmt : {Node.getBeginStmt(), Node.getEndStmt()})
if (Stmt != nullptr && InnerMatcher.matches(*Stmt, Finder, Builder))
return true;
return false;
return llvm::any_of(llvm::ArrayRef{Node.getBeginStmt(), Node.getEndStmt()},
[&](const DeclStmt *Stmt) {
return Stmt &&
InnerMatcher.matches(*Stmt, Finder, Builder);
});
}
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt) {

View File

@ -20,10 +20,9 @@ AST_MATCHER(CXXRecordDecl, hasDirectVirtualBaseClass) {
return false;
if (!Node.getNumVBases())
return false;
for (const CXXBaseSpecifier &Base : Node.bases())
if (Base.isVirtual())
return true;
return false;
return llvm::any_of(Node.bases(), [](const CXXBaseSpecifier &Base) {
return Base.isVirtual();
});
}
} // namespace

View File

@ -114,17 +114,15 @@ hasCorrespondingOverloadInBaseClass(const CXXMethodDecl *MD,
RD = MD->getParent();
}
for (const auto &BS : RD->bases()) {
return llvm::any_of(RD->bases(), [&](const CXXBaseSpecifier &BS) {
// We can't say much about a dependent base class, but to avoid false
// positives assume it can have a corresponding overload.
if (BS.getType()->isDependentType())
return true;
if (const auto *BaseRD = BS.getType()->getAsCXXRecordDecl())
if (hasCorrespondingOverloadInBaseClass(MD, BaseRD))
return true;
}
return false;
if (const CXXRecordDecl *BaseRD = BS.getType()->getAsCXXRecordDecl())
return hasCorrespondingOverloadInBaseClass(MD, BaseRD);
return false;
});
}
void NewDeleteOverloadsCheck::registerMatchers(MatchFinder *Finder) {

View File

@ -30,13 +30,10 @@ static bool isOverrideMethod(const FunctionDecl *Function) {
static bool hasAttrAfterParam(const SourceManager *SourceManager,
const ParmVarDecl *Param) {
for (const auto *Attr : Param->attrs()) {
if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
Attr->getLocation())) {
return true;
}
}
return false;
return llvm::any_of(Param->attrs(), [&](const Attr *Attr) {
return SourceManager->isBeforeInTranslationUnit(Param->getLocation(),
Attr->getLocation());
});
}
void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {

View File

@ -510,27 +510,24 @@ static bool canBeModified(ASTContext *Context, const Expr *E) {
/// Returns true when it can be guaranteed that the elements of the
/// container are not being modified.
static bool usagesAreConst(ASTContext *Context, const UsageResult &Usages) {
for (const Usage &U : Usages) {
return llvm::none_of(Usages, [&Context](const Usage &U) {
// Lambda captures are just redeclarations (VarDecl) of the same variable,
// not expressions. If we want to know if a variable that is captured by
// reference can be modified in an usage inside the lambda's body, we need
// to find the expression corresponding to that particular usage, later in
// this loop.
if (U.Kind != Usage::UK_CaptureByCopy && U.Kind != Usage::UK_CaptureByRef &&
canBeModified(Context, U.Expression))
return false;
}
return true;
return U.Kind != Usage::UK_CaptureByCopy &&
U.Kind != Usage::UK_CaptureByRef &&
canBeModified(Context, U.Expression);
});
}
/// Returns true if the elements of the container are never accessed
/// by reference.
static bool usagesReturnRValues(const UsageResult &Usages) {
for (const auto &U : Usages) {
if (U.Expression && !U.Expression->isPRValue())
return false;
}
return true;
return llvm::all_of(Usages, [](const Usage &U) {
return !U.Expression || U.Expression->isPRValue();
});
}
/// Returns true if the container is const-qualified.

View File

@ -89,13 +89,11 @@ bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
// Next, check if the variable was removed from existence by an earlier
// iteration.
for (const auto &I : *ReplacedVars) {
if (I.second == V) {
DependsOnInsideVariable = true;
return false;
}
}
return true;
if (llvm::none_of(*ReplacedVars,
[&](const auto &I) { return I.second == V; }))
return true;
DependsOnInsideVariable = true;
return false;
}
/// If we already created a variable for TheLoop, check to make sure
@ -234,11 +232,8 @@ static bool containsExpr(ASTContext *Context, const ContainerT *Container,
const Expr *E) {
llvm::FoldingSetNodeID ID;
E->Profile(ID, *Context, true);
for (const auto &I : *Container) {
if (ID == I.second)
return true;
}
return false;
return llvm::any_of(*Container,
[&](const auto &I) { return ID == I.second; });
}
/// Returns true when the index expression is a declaration reference to

View File

@ -196,11 +196,7 @@ static bool hasRValueOverload(const CXXConstructorDecl *Ctor,
return true;
};
for (const auto *Candidate : Record->ctors()) {
if (IsRValueOverload(Candidate))
return true;
}
return false;
return llvm::any_of(Record->ctors(), IsRValueOverload);
}
/// Find all references to \p ParamDecl across all of the

View File

@ -44,17 +44,12 @@ AST_MATCHER_P(NamedDecl, hasAnyNameIgnoringTemplates, std::vector<StringRef>,
// clang/lib/ASTMatchers/ASTMatchersInternal.cpp and checks whether
// FullNameTrimmed matches any of the given Names.
const StringRef FullNameTrimmedRef = FullNameTrimmed;
for (const StringRef Pattern : Names) {
if (Pattern.starts_with("::")) {
if (FullNameTrimmed == Pattern)
return true;
} else if (FullNameTrimmedRef.ends_with(Pattern) &&
FullNameTrimmedRef.drop_back(Pattern.size()).ends_with("::")) {
return true;
}
}
return false;
return llvm::any_of(Names, [&](const StringRef Pattern) {
if (Pattern.starts_with("::"))
return FullNameTrimmed == Pattern;
return FullNameTrimmedRef.ends_with(Pattern) &&
FullNameTrimmedRef.drop_back(Pattern.size()).ends_with("::");
});
}
// Checks if the given matcher is the last argument of the given CallExpr.

View File

@ -55,13 +55,12 @@ public:
bool visitUnqualName(StringRef UnqualName) {
// Check for collisions with function arguments.
for (const ParmVarDecl *Param : F.parameters())
Collision = llvm::any_of(F.parameters(), [&](const ParmVarDecl *Param) {
if (const IdentifierInfo *Ident = Param->getIdentifier())
if (Ident->getName() == UnqualName) {
Collision = true;
return true;
}
return false;
return Ident->getName() == UnqualName;
return false;
});
return Collision;
}
bool TraverseTypeLoc(TypeLoc TL, bool TraverseQualifier = true) {

View File

@ -25,11 +25,9 @@ AST_MATCHER_P(ObjCImplementationDecl, hasInterface,
AST_MATCHER_P(ObjCContainerDecl, hasInstanceMethod,
ast_matchers::internal::Matcher<ObjCMethodDecl>, Base) {
// Check each instance method against the provided matcher.
for (const auto *I : Node.instance_methods()) {
if (Base.matches(*I, Finder, Builder))
return true;
}
return false;
return llvm::any_of(Node.instance_methods(), [&](const ObjCMethodDecl *I) {
return Base.matches(*I, Finder, Builder);
});
}
} // namespace

View File

@ -23,12 +23,9 @@ namespace {
AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
AST_MATCHER_P(CXXRecordDecl, hasBase, Matcher<QualType>, InnerMatcher) {
for (const CXXBaseSpecifier &BaseSpec : Node.bases()) {
const QualType BaseType = BaseSpec.getType();
if (InnerMatcher.matches(BaseType, Finder, Builder))
return true;
}
return false;
return llvm::any_of(Node.bases(), [&](const CXXBaseSpecifier &BaseSpec) {
return InnerMatcher.matches(BaseSpec.getType(), Finder, Builder);
});
}
} // namespace

View File

@ -20,12 +20,9 @@ namespace clang::tidy::readability {
namespace {
AST_MATCHER(CXXMethodDecl, hasOnlyDefaultParameters) {
for (const auto *Param : Node.parameters()) {
if (!Param->hasDefaultArg())
return false;
}
return true;
return llvm::all_of(Node.parameters(), [](const ParmVarDecl *Param) {
return Param->hasDefaultArg();
});
}
const auto DefaultSmartPointers = "::std::shared_ptr;::std::unique_ptr;"

View File

@ -136,11 +136,9 @@ getRepresentation(const std::vector<llvm::StringRef> &Config,
template <typename T>
static bool isAnyOperatorEnabled(const std::vector<llvm::StringRef> &Config,
const T &Operators) {
for (const auto &[traditional, alternative] : Operators) {
if (!getRepresentation(Config, traditional, alternative).empty())
return true;
}
return false;
return llvm::any_of(Operators, [&](const auto &Op) {
return !getRepresentation(Config, Op.first, Op.second).empty();
});
}
OperatorsRepresentationCheck::OperatorsRepresentationCheck(

View File

@ -758,7 +758,7 @@ bool SuspiciousCallArgumentCheck::areParamAndArgComparable(
bool SuspiciousCallArgumentCheck::areArgsSwapped(std::size_t Position1,
std::size_t Position2) const {
for (const Heuristic H : AppliedHeuristics) {
return llvm::any_of(AppliedHeuristics, [&](Heuristic H) {
const bool A1ToP2Similar = areNamesSimilar(
ArgNames[Position2], ParamNames[Position1], H, BoundKind::SimilarAbove);
const bool A2ToP1Similar = areNamesSimilar(
@ -771,11 +771,9 @@ bool SuspiciousCallArgumentCheck::areArgsSwapped(std::size_t Position1,
!areNamesSimilar(ArgNames[Position2], ParamNames[Position2], H,
BoundKind::DissimilarBelow);
if ((A1ToP2Similar || A2ToP1Similar) && A1ToP1Dissimilar &&
A2ToP2Dissimilar)
return true;
}
return false;
return (A1ToP2Similar || A2ToP1Similar) && A1ToP1Dissimilar &&
A2ToP2Dissimilar;
});
}
bool SuspiciousCallArgumentCheck::areNamesSimilar(StringRef Arg,

View File

@ -65,15 +65,9 @@ static bool hasPtrOrReferenceInStmt(const Stmt *S, const ValueDecl *Var) {
if (isPtrOrReferenceForVar(S, Var))
return true;
for (const Stmt *Child : S->children()) {
if (!Child)
continue;
if (hasPtrOrReferenceInStmt(Child, Var))
return true;
}
return false;
return llvm::any_of(S->children(), [&](const Stmt *Child) {
return Child && hasPtrOrReferenceInStmt(Child, Var);
});
}
static bool refersToEnclosingLambdaCaptureByRef(const Decl *Func,

View File

@ -21,10 +21,7 @@ using llvm::SmallPtrSet;
template <typename S>
static bool isSetDifferenceEmpty(const S &S1, const S &S2) {
for (auto E : S1)
if (S2.count(E) == 0)
return false;
return true;
return llvm::none_of(S1, [&S2](const auto &E) { return !S2.contains(E); });
}
// Extracts all Nodes keyed by ID from Matches and inserts them into Nodes.

View File

@ -148,12 +148,9 @@ bool ExprSequence::inSequence(const Stmt *Before, const Stmt *After) const {
// If 'After' is a parent of 'Before' or is sequenced after one of these
// parents, we know that it is sequenced after 'Before'.
for (const Stmt *Parent : BeforeParents) {
if (Parent == After || inSequence(Parent, After))
return true;
}
return false;
return llvm::any_of(BeforeParents, [&](const Stmt *Parent) {
return Parent == After || inSequence(Parent, After);
});
}
bool ExprSequence::potentiallyAfter(const Stmt *After,

View File

@ -24,11 +24,9 @@ static bool hasDeletedCopyConstructor(QualType Type) {
auto *Record = Type->getAsCXXRecordDecl();
if (!Record || !Record->hasDefinition())
return false;
for (const auto *Constructor : Record->ctors()) {
if (Constructor->isCopyConstructor() && Constructor->isDeleted())
return true;
}
return false;
return llvm::any_of(Record->ctors(), [](const auto *Constructor) {
return Constructor->isCopyConstructor() && Constructor->isDeleted();
});
}
std::optional<bool> isExpensiveToCopy(QualType Type,
@ -70,14 +68,10 @@ bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl,
return false;
}
// If all its direct bases are trivially constructible.
for (const CXXBaseSpecifier &Base : ClassDecl->bases()) {
if (!isTriviallyDefaultConstructible(Base.getType(), Context))
return false;
if (Base.isVirtual())
return false;
}
return true;
return llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &Base) {
return isTriviallyDefaultConstructible(Base.getType(), Context) &&
!Base.isVirtual();
});
}
// Based on QualType::isTrivial.