[PATCH] D97144: [clang-tidy] Simplify shrink to fit check
Stephen Kelly via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 21 06:40:02 PST 2021
steveire created this revision.
steveire added reviewers: aaron.ballman, njames93.
Herald added a subscriber: xazax.hun.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D97144
Files:
clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.cpp
clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.h
Index: clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.h
+++ clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.h
@@ -30,6 +30,9 @@
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+ return TK_IgnoreUnlessSpelledInSource;
+ }
};
} // namespace modernize
Index: clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/ShrinkToFitCheck.cpp
@@ -21,31 +21,24 @@
void ShrinkToFitCheck::registerMatchers(MatchFinder *Finder) {
// Swap as a function need not to be considered, because rvalue can not
// be bound to a non-const reference.
- const auto ShrinkableAsMember =
- memberExpr(member(valueDecl().bind("ContainerDecl")));
- const auto ShrinkableAsDecl =
- declRefExpr(hasDeclaration(valueDecl().bind("ContainerDecl")));
- const auto CopyCtorCall = cxxConstructExpr(hasArgument(
- 0, anyOf(ShrinkableAsMember, ShrinkableAsDecl,
- unaryOperator(has(ignoringParenImpCasts(ShrinkableAsMember))),
- unaryOperator(has(ignoringParenImpCasts(ShrinkableAsDecl))))));
- const auto SwapParam =
- expr(anyOf(memberExpr(member(equalsBoundNode("ContainerDecl"))),
- declRefExpr(hasDeclaration(equalsBoundNode("ContainerDecl"))),
- unaryOperator(has(ignoringParenImpCasts(
- memberExpr(member(equalsBoundNode("ContainerDecl")))))),
- unaryOperator(has(ignoringParenImpCasts(declRefExpr(
- hasDeclaration(equalsBoundNode("ContainerDecl"))))))));
+ const auto ShrinkableExpr = mapAnyOf(memberExpr, declRefExpr);
+ const auto Shrinkable =
+ ShrinkableExpr.with(hasDeclaration(valueDecl().bind("ContainerDecl")));
+ const auto BoundShrinkable = ShrinkableExpr.with(
+ hasDeclaration(valueDecl(equalsBoundNode("ContainerDecl"))));
Finder->addMatcher(
cxxMemberCallExpr(
- on(hasType(hasCanonicalType(hasDeclaration(namedDecl(
- hasAnyName("std::basic_string", "std::deque", "std::vector")))))),
callee(cxxMethodDecl(hasName("swap"))),
- has(ignoringParenImpCasts(
- memberExpr(traverse(TK_AsIs, hasDescendant(CopyCtorCall))))),
- hasArgument(0, SwapParam.bind("ContainerToShrink")),
- unless(isInTemplateInstantiation()))
+ hasArgument(
+ 0, anyOf(Shrinkable, unaryOperator(hasUnaryOperand(Shrinkable)))),
+ on(cxxConstructExpr(hasArgument(
+ 0,
+ expr(anyOf(BoundShrinkable,
+ unaryOperator(hasUnaryOperand(BoundShrinkable))),
+ hasType(hasCanonicalType(hasDeclaration(namedDecl(hasAnyName(
+ "std::basic_string", "std::deque", "std::vector"))))))
+ .bind("ContainerToShrink")))))
.bind("CopyAndSwapTrick"),
this);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97144.325291.patch
Type: text/x-patch
Size: 3308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210221/7511ee23/attachment.bin>
More information about the cfe-commits
mailing list