Skip to content

Commit 318bef9

Browse files
authored
[clang-tidy][NFC]refactor matcher for bugprone-optional-value-conversion (#130415)
The old `constructFrom` has hidden requirement which TypeMatcher must be used before ArgumentMatcher because there are bind inside. Inlining this function to make it more intuitive.
1 parent 1fe702f commit 318bef9

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,11 @@ AST_MATCHER_P(QualType, hasCleanType, Matcher<QualType>, InnerMatcher) {
2727
Finder, Builder);
2828
}
2929

30-
constexpr std::array<StringRef, 2> NameList{
30+
constexpr std::array<StringRef, 2> MakeSmartPtrList{
3131
"::std::make_unique",
3232
"::std::make_shared",
3333
};
3434

35-
Matcher<Expr> constructFrom(Matcher<QualType> TypeMatcher,
36-
Matcher<Expr> ArgumentMatcher) {
37-
return expr(
38-
anyOf(
39-
// construct optional
40-
cxxConstructExpr(argumentCountIs(1U), hasType(TypeMatcher),
41-
hasArgument(0U, ArgumentMatcher)),
42-
// known template methods in std
43-
callExpr(argumentCountIs(1),
44-
callee(functionDecl(
45-
matchers::matchesAnyListedName(NameList),
46-
hasTemplateArgument(0, refersToType(TypeMatcher)))),
47-
hasArgument(0, ArgumentMatcher))),
48-
unless(anyOf(hasAncestor(typeLoc()),
49-
hasAncestor(expr(matchers::hasUnevaluatedContext())))));
50-
}
51-
5235
} // namespace
5336

5437
OptionalValueConversionCheck::OptionalValueConversionCheck(
@@ -74,7 +57,7 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
7457
auto EqualsBoundOptionalType =
7558
qualType(hasCleanType(equalsBoundNode("optional-type")));
7659

77-
auto OptionalDereferenceMatcher = callExpr(
60+
auto OptionalDerefMatcherImpl = callExpr(
7861
anyOf(
7962
cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
8063
hasUnaryOperand(hasType(EqualsBoundOptionalType)))
@@ -88,11 +71,24 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
8871

8972
auto StdMoveCallMatcher =
9073
callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))),
91-
hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
74+
hasArgument(0, ignoringImpCasts(OptionalDerefMatcherImpl)));
75+
auto OptionalDerefMatcher =
76+
ignoringImpCasts(anyOf(OptionalDerefMatcherImpl, StdMoveCallMatcher));
77+
9278
Finder->addMatcher(
93-
expr(constructFrom(BindOptionalType,
94-
ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
95-
StdMoveCallMatcher))))
79+
expr(anyOf(
80+
// construct optional
81+
cxxConstructExpr(argumentCountIs(1), hasType(BindOptionalType),
82+
hasArgument(0, OptionalDerefMatcher)),
83+
// known template methods in std
84+
callExpr(
85+
argumentCountIs(1),
86+
callee(functionDecl(
87+
matchers::matchesAnyListedName(MakeSmartPtrList),
88+
hasTemplateArgument(0, refersToType(BindOptionalType)))),
89+
hasArgument(0, OptionalDerefMatcher))),
90+
unless(anyOf(hasAncestor(typeLoc()),
91+
hasAncestor(expr(matchers::hasUnevaluatedContext())))))
9692
.bind("expr"),
9793
this);
9894
}

0 commit comments

Comments
 (0)