[clang-tidy][NFC] Remove ad-hoc unless(isExpansionInSystemHeader()) from matchers (#183020)

#151035 centralized the code to skip matching nodes in system headers,
so individual checks no longer need the
`unless(isExpansionInSystemHeader())` boilerplate to achieve that. You
can verify that the logic introduced in that PR is equivalent to
`unless(isExpansionInSystemHeader())`; they both essentially boil down
to
`!SourceManager.isInSystemHeader(Node.getBeginLoc())`:

7c5c58cdc6/clang/include/clang/ASTMatchers/ASTMatchers.h (L283-L291)

7c5c58cdc6/clang/lib/ASTMatchers/ASTMatchFinder.cpp (L1363-L1372)

I audited all the uses of `isExpansionInSystemHeader` in the codebase,
and the only one I found that wasn't redundant was one in
`TaggedUnionMemberCountCheck.cpp`.
This commit is contained in:
Victor Chernyakin 2026-02-24 14:24:19 -07:00 committed by GitHub
parent 75cbd584ee
commit efc98de8e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 30 additions and 56 deletions

View File

@ -29,10 +29,8 @@ void UncheckedStatusOrAccessCheck::registerMatchers(MatchFinder *Finder) {
auto HasStatusOrCallDescendant =
hasDescendant(callExpr(callee(cxxMethodDecl(ofClass(hasAnyName(
"absl::StatusOr", "absl::internal_statusor::OperatorBase"))))));
Finder->addMatcher(functionDecl(unless(isExpansionInSystemHeader()),
hasBody(HasStatusOrCallDescendant))
.bind(FuncID),
this);
Finder->addMatcher(
functionDecl(hasBody(HasStatusOrCallDescendant)).bind(FuncID), this);
Finder->addMatcher(
cxxConstructorDecl(hasAnyConstructorInitializer(
withInitializer(HasStatusOrCallDescendant)))

View File

@ -17,9 +17,7 @@ using namespace clang::ast_matchers;
namespace clang::tidy::altera {
void StructPackAlignCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(recordDecl(isStruct(), isDefinition(),
unless(isExpansionInSystemHeader()))
.bind("struct"),
Finder->addMatcher(recordDecl(isStruct(), isDefinition()).bind("struct"),
this);
}

View File

@ -88,7 +88,7 @@ void EmptyCatchCheck::registerMatchers(MatchFinder *Finder) {
hasCanonicalType(AllowedNamedExceptionTypes)));
Finder->addMatcher(
cxxCatchStmt(unless(isExpansionInSystemHeader()), unless(isInMacro()),
cxxCatchStmt(unless(isInMacro()),
unless(hasCaughtType(IgnoredExceptionType)),
hasHandler(compoundStmt(
statementCountIs(0),

View File

@ -41,8 +41,8 @@ void IncDecInConditionsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
expr(
OperatorMatcher, unless(isExpansionInSystemHeader()),
unless(hasAncestor(OperatorMatcher)), expr().bind("parent"),
OperatorMatcher, unless(hasAncestor(OperatorMatcher)),
expr().bind("parent"),
forEachDescendant(
expr(anyOf(unaryOperator(isUnaryPrePostOperator(),

View File

@ -27,7 +27,6 @@ void IncorrectEnableSharedFromThisCheck::registerMatchers(MatchFinder *Finder) {
.bind("base_rec")));
Finder->addMatcher(
cxxRecordDecl(
unless(isExpansionInSystemHeader()),
hasDirectBase(cxxBaseSpecifier(unless(isPublic()), hasType(QType))
.bind("base")))
.bind("derived"),

View File

@ -53,8 +53,7 @@ void NonZeroEnumToBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
"|", "&", "^", "<<", ">>", "~", "|=", "&=", "^=", "<<=", ">>="));
Finder->addMatcher(
castExpr(hasCastKind(CK_IntegralToBoolean),
unless(isExpansionInSystemHeader()), hasType(booleanType()),
castExpr(hasCastKind(CK_IntegralToBoolean), hasType(booleanType()),
hasSourceExpression(
expr(hasType(qualType(hasCanonicalType(hasDeclaration(
enumDecl(isCompleteAndHasNoZeroValue(),

View File

@ -91,8 +91,7 @@ void StdNamespaceModificationCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(decl(anyOf(BadNonTemplateSpecializationDecl,
BadClassTemplateSpec, BadInnerClassTemplateSpec,
BadFunctionTemplateSpec, BadMemberFunctionSpec),
unless(isExpansionInSystemHeader()))
BadFunctionTemplateSpec, BadMemberFunctionSpec))
.bind("decl"),
this);
}

View File

@ -30,11 +30,11 @@ void UncheckedOptionalAccessCheck::registerMatchers(MatchFinder *Finder) {
auto HasOptionalCallDescendant = hasDescendant(callExpr(callee(cxxMethodDecl(
ofClass(UncheckedOptionalAccessModel::optionalClassDecl())))));
Finder->addMatcher(
decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),
// FIXME: Remove the filter below when lambdas are
// well supported by the check.
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
hasBody(HasOptionalCallDescendant)),
decl(anyOf(functionDecl(
// FIXME: Remove the filter below when lambdas are
// well supported by the check.
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
hasBody(HasOptionalCallDescendant)),
cxxConstructorDecl(hasAnyConstructorInitializer(
withInitializer(HasOptionalCallDescendant)))))
.bind(FuncID),

View File

@ -82,16 +82,14 @@ static FixItHint generateFixItHint(const FunctionDecl *Decl) {
void FunctionNamingCheck::registerMatchers(MatchFinder *Finder) {
// Enforce Objective-C function naming conventions on all functions except:
// • Functions defined in system headers.
// • C++ member functions.
// • Namespaced functions.
// • Implicitly defined functions.
// • The main function.
Finder->addMatcher(
functionDecl(
unless(anyOf(isExpansionInSystemHeader(), cxxMethodDecl(),
hasAncestor(namespaceDecl()), isMain(), isImplicit(),
matchesName(validFunctionNameRegex(true)),
unless(anyOf(cxxMethodDecl(), hasAncestor(namespaceDecl()), isMain(),
isImplicit(), matchesName(validFunctionNameRegex(true)),
allOf(isStaticStorageClass(),
matchesName(validFunctionNameRegex(false))))))
.bind("function"),

View File

@ -52,8 +52,7 @@ void PreferStaticOverAnonymousNamespaceCheck::storeOptions(
void PreferStaticOverAnonymousNamespaceCheck::registerMatchers(
MatchFinder *Finder) {
const auto IsDefinitionInAnonymousNamespace = allOf(
unless(isExpansionInSystemHeader()), isLexicallyInAnonymousNamespace(),
unless(isInMacro()), isDefinition());
isLexicallyInAnonymousNamespace(), unless(isInMacro()), isDefinition());
if (AllowMemberFunctionsInClass) {
Finder->addMatcher(

View File

@ -79,8 +79,7 @@ void OverrideWithDifferentVisibilityCheck::registerMatchers(
Finder->addMatcher(
cxxMethodDecl(
isVirtual(), FilterDestructors, FilterOperators,
ofClass(
cxxRecordDecl(unless(isExpansionInSystemHeader())).bind("class")),
ofClass(cxxRecordDecl().bind("class")),
forEachOverridden(cxxMethodDecl(ofClass(cxxRecordDecl().bind("base")),
unless(IgnoredDecl))
.bind("base_func")))

View File

@ -42,8 +42,6 @@ AST_MATCHER(FunctionDecl, hasOtherDeclarations) {
void UseConstraintsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
functionTemplateDecl(
// Skip external libraries included as system headers
unless(isExpansionInSystemHeader()),
has(functionDecl(unless(hasOtherDeclarations()), isDefinition(),
hasReturnTypeLoc(typeLoc().bind("return")))
.bind("function")))

View File

@ -137,7 +137,6 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
if (WarnOnSingleLocks) {
Finder->addMatcher(
compoundStmt(
unless(isExpansionInSystemHeader()),
has(declStmt(has(LockVarDecl)).bind("lock-decl-single")),
unless(has(declStmt(unless(equalsBoundNode("lock-decl-single")),
has(LockVarDecl))))),
@ -145,8 +144,7 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
}
Finder->addMatcher(
compoundStmt(unless(isExpansionInSystemHeader()),
has(declStmt(has(LockVarDecl)).bind("lock-decl-multiple")),
compoundStmt(has(declStmt(has(LockVarDecl)).bind("lock-decl-multiple")),
has(declStmt(unless(equalsBoundNode("lock-decl-multiple")),
has(LockVarDecl))))
.bind("block-multiple"),
@ -154,22 +152,19 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
if (WarnOnUsingAndTypedef) {
// Match 'typedef std::lock_guard<std::mutex> Lock'
Finder->addMatcher(typedefDecl(unless(isExpansionInSystemHeader()),
hasType(hasUnderlyingType(LockGuardType)))
Finder->addMatcher(typedefDecl(hasType(hasUnderlyingType(LockGuardType)))
.bind("lock-guard-typedef"),
this);
// Match 'using Lock = std::lock_guard<std::mutex>'
Finder->addMatcher(typeAliasDecl(unless(isExpansionInSystemHeader()),
hasType(templateSpecializationType(
Finder->addMatcher(typeAliasDecl(hasType(templateSpecializationType(
hasDeclaration(LockGuardClassDecl))))
.bind("lock-guard-using-alias"),
this);
// Match 'using std::lock_guard'
Finder->addMatcher(
usingDecl(unless(isExpansionInSystemHeader()),
hasAnyUsingShadowDecl(hasTargetDecl(LockGuardClassDecl)))
usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(LockGuardClassDecl)))
.bind("lock-guard-using-decl"),
this);
}

View File

@ -22,7 +22,6 @@ namespace clang::tidy::performance {
void AvoidEndlCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
callExpr(
unless(isExpansionInSystemHeader()),
anyOf(cxxOperatorCallExpr(
hasOverloadedOperatorName("<<"),
hasRHS(declRefExpr(to(namedDecl(hasName("::std::endl"))))

View File

@ -100,8 +100,7 @@ bool EnumSizeCheck::isLanguageVersionSupported(
void EnumSizeCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
enumDecl(unless(isExpansionInSystemHeader()), isDefinition(),
hasEnumerators(), unless(isExternC()),
enumDecl(isDefinition(), hasEnumerators(), unless(isExternC()),
unless(anyOf(
matchers::matchesAnyListedRegexName(EnumIgnoreList),
hasTypedefNameForAnonDecl(

View File

@ -91,8 +91,7 @@ void SIMDIntrinsicsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(callExpr(callee(functionDecl(
matchesName("^::(_mm_|_mm256_|_mm512_|vec_)"),
isVectorFunction())),
unless(isExpansionInSystemHeader()))
isVectorFunction())))
.bind("call"),
this);
}

View File

@ -74,7 +74,7 @@ void ContainerDataPointerCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
unaryOperator(
unless(isExpansionInSystemHeader()), hasOperatorName("&"),
hasOperatorName("&"),
hasUnaryOperand(expr(
anyOf(cxxOperatorCallExpr(SubscriptOperator, argumentCountIs(2),
hasArgument(0, ContainerExpr),

View File

@ -84,9 +84,8 @@ void ConvertMemberFunctionsToStaticCheck::registerMatchers(
cxxMethodDecl(
isDefinition(), isUserProvided(),
unless(anyOf(
isExpansionInSystemHeader(), isVirtual(), isStatic(),
hasTrivialBody(), isOverloadedOperator(), cxxConstructorDecl(),
cxxDestructorDecl(), cxxConversionDecl(),
isVirtual(), isStatic(), hasTrivialBody(), isOverloadedOperator(),
cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl(),
isExplicitObjectMemberFunction(), isTemplate(),
isDependentContext(),
ofClass(anyOf(

View File

@ -224,9 +224,9 @@ void MakeMemberFunctionConstCheck::registerMatchers(MatchFinder *Finder) {
cxxMethodDecl(
isDefinition(), isUserProvided(),
unless(anyOf(
isExpansionInSystemHeader(), isVirtual(), isConst(),
isStatic(), hasTrivialBody(), cxxConstructorDecl(),
cxxDestructorDecl(), isTemplate(), isDependentContext(),
isVirtual(), isConst(), isStatic(), hasTrivialBody(),
cxxConstructorDecl(), cxxDestructorDecl(), isTemplate(),
isDependentContext(),
ofClass(anyOf(isLambda(),
hasAnyDependentBases()) // Method might become
// virtual depending on

View File

@ -177,7 +177,6 @@ void OperatorsRepresentationCheck::registerBinaryOperatorMatcher(
Finder->addMatcher(
binaryOperator(
unless(isExpansionInSystemHeader()),
anyOf(hasInvalidBinaryOperatorRepresentation(
BO_LAnd, getRepresentation(BinaryOperators, "&&", "and")),
hasInvalidBinaryOperatorRepresentation(
@ -210,7 +209,6 @@ void OperatorsRepresentationCheck::registerUnaryOperatorMatcher(
Finder->addMatcher(
unaryOperator(
unless(isExpansionInSystemHeader()),
anyOf(hasInvalidUnaryOperatorRepresentation(
UO_LNot, getRepresentation(BinaryOperators, "!", "not")),
hasInvalidUnaryOperatorRepresentation(
@ -227,7 +225,6 @@ void OperatorsRepresentationCheck::registerOverloadedOperatorMatcher(
Finder->addMatcher(
cxxOperatorCallExpr(
unless(isExpansionInSystemHeader()),
anyOf(
hasInvalidOverloadedOperatorRepresentation(
OO_AmpAmp,

View File

@ -57,8 +57,7 @@ ReferenceToConstructedTemporaryCheck::getCheckTraversalKind() const {
void ReferenceToConstructedTemporaryCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(
varDecl(unless(isExpansionInSystemHeader()),
hasType(qualType(references(qualType().bind("type")))),
varDecl(hasType(qualType(references(qualType().bind("type")))),
decl().bind("var"),
hasInitializer(expr(hasDescendant(
materializeTemporaryExpr(