diff options
| author | Florian Gross <fgross@noexcept.net> | 2019-05-02 16:41:28 +0000 |
|---|---|---|
| committer | Florian Gross <fgross@noexcept.net> | 2019-05-02 16:41:28 +0000 |
| commit | e25a0e95104ba73f78c6b3da5c703af17630ce3e (patch) | |
| tree | 2263de8628faeff829347913281253dfa0c34495 /clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp | |
| parent | a558ee81052830b8a4f2c5491c418c18e2f0ba32 (diff) | |
| download | bcm5719-llvm-e25a0e95104ba73f78c6b3da5c703af17630ce3e.tar.gz bcm5719-llvm-e25a0e95104ba73f78c6b3da5c703af17630ce3e.zip | |
Fixed: Duck-typing in readability-redundant-smartptr-get didn't catch MSVC STL smart pointers.
Differential Revision: https://reviews.llvm.org/D61209
llvm-svn: 359801
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp index 73157c9e037..834c0cbadb0 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp @@ -30,6 +30,10 @@ internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) { .bind("redundant_get"); } +internal::Matcher<Decl> knownSmartptr() { + return recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr")); +} + void registerMatchersForGetArrowStart(MatchFinder *Finder, MatchFinder::MatchCallback *Callback) { const auto QuacksLikeASmartptr = recordDecl( @@ -39,21 +43,23 @@ void registerMatchersForGetArrowStart(MatchFinder *Finder, has(cxxMethodDecl(hasName("operator*"), returns(qualType(references( type().bind("op*Type"))))))); + // Make sure we are not missing the known standard types. + const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr); + // Catch 'ptr.get()->Foo()' - Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(), - hasObjectExpression(ignoringImpCasts( - callToGet(QuacksLikeASmartptr)))), - Callback); + Finder->addMatcher( + memberExpr(expr().bind("memberExpr"), isArrow(), + hasObjectExpression(ignoringImpCasts(callToGet(Smartptr)))), + Callback); // Catch '*ptr.get()' or '*ptr->get()' Finder->addMatcher( - unaryOperator(hasOperatorName("*"), - hasUnaryOperand(callToGet(QuacksLikeASmartptr))), + unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(Smartptr))), Callback); // Catch '!ptr.get()' - const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(recordDecl( - QuacksLikeASmartptr, has(cxxConversionDecl(returns(booleanType())))))); + const auto CallToGetAsBool = ignoringParenImpCasts(callToGet( + recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType())))))); Finder->addMatcher( unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)), Callback); @@ -71,10 +77,7 @@ void registerMatchersForGetEquals(MatchFinder *Finder, // This one is harder to do with duck typing. // The operator==/!= that we are looking for might be member or non-member, // might be on global namespace or found by ADL, might be a template, etc. - // For now, lets keep a list of known standard types. - - const auto IsAKnownSmartptr = - recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr")); + // For now, lets keep it to the known standard types. // Matches against nullptr. Finder->addMatcher( @@ -82,7 +85,7 @@ void registerMatchersForGetEquals(MatchFinder *Finder, hasEitherOperand(ignoringImpCasts( anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(), integerLiteral(equals(0))))), - hasEitherOperand(callToGet(IsAKnownSmartptr))), + hasEitherOperand(callToGet(knownSmartptr()))), Callback); // FIXME: Match and fix if (l.get() == r.get()). |

