summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
diff options
context:
space:
mode:
authorKirill Bobyrev <omtcyfz@gmail.com>2016-09-26 07:22:37 +0000
committerKirill Bobyrev <omtcyfz@gmail.com>2016-09-26 07:22:37 +0000
commit9559f04b9db004375b10f79e30d0e321c62ab222 (patch)
tree391a2101c2711cbe552b018304e09bd12d943e34 /clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
parent839d15a194a658dfe407e1681514231455138360 (diff)
downloadbcm5719-llvm-9559f04b9db004375b10f79e30d0e321c62ab222.tar.gz
bcm5719-llvm-9559f04b9db004375b10f79e30d0e321c62ab222.zip
[clang-tidy] make readability-redundant-smartptr-get report get() usage in conditions
This patch extends clang-tidy's readability-redundant-smartptr-get to produce warnings for previously unsupported cases: ``` std::unique_ptr<void> ptr; if (ptr.get()) if (ptr.get() == NULL) if (ptr.get() != NULL) ``` This is intended to fix https://llvm.org/bugs/show_bug.cgi?id=25804, a bug report opened by @Eugene.Zelenko. However, there still are cases not detected by the check. They can be found in `void Negative()` function defined in test/clang-tidy/readability-redundant-smartptr-get.cpp. Reviewers: alexfh Differential Revision: https://reviews.llvm.org/D24893 llvm-svn: 282386
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
index 3d1c863843f..5aad5b5213b 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp
@@ -60,21 +60,27 @@ void registerMatchersForGetEquals(MatchFinder *Finder,
// 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(
- anyOf(hasName("::std::unique_ptr"), hasName("::std::shared_ptr")));
+ const auto IsAKnownSmartptr =
+ recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
// Matches against nullptr.
Finder->addMatcher(
- binaryOperator(
- anyOf(hasOperatorName("=="), hasOperatorName("!=")),
- hasEitherOperand(ignoringImpCasts(cxxNullPtrLiteralExpr())),
- hasEitherOperand(callToGet(IsAKnownSmartptr))),
+ binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
+ hasEitherOperand(ignoringImpCasts(
+ anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
+ integerLiteral(equals(0))))),
+ hasEitherOperand(callToGet(IsAKnownSmartptr))),
+ Callback);
+
+ // Matches against if(ptr.get())
+ Finder->addMatcher(
+ ifStmt(hasCondition(ignoringImpCasts(callToGet(IsAKnownSmartptr)))),
Callback);
- // TODO: Catch ptr.get() == other_ptr.get()
-}
+ // FIXME: Match and fix if (l.get() == r.get()).
+}
-} // namespace
+} // namespace
void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
// Only register the matchers for C++; the functionality currently does not
@@ -102,10 +108,11 @@ bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType();
return OpArrowType == OpStarType && OpArrowType == GetType;
}
-} // namespace
+} // namespace
void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
- if (!allReturnTypesMatch(Result)) return;
+ if (!allReturnTypesMatch(Result))
+ return;
bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
OpenPOWER on IntegriCloud