diff options
author | Jonathan Coe <jbcoe@me.com> | 2018-04-24 21:25:16 +0000 |
---|---|---|
committer | Jonathan Coe <jbcoe@me.com> | 2018-04-24 21:25:16 +0000 |
commit | 84cdd83d7bba6b0e59db446521d26f139a81e16c (patch) | |
tree | a6e3a2344e3bd18b9a99f7560d2cc613d47d1f6f /clang-tidy/bugprone | |
parent | bc9a022026e6b42c416c6da9ff8a1ba9b8ec2458 (diff) |
[clang-tidy] Improve bugprone-unused-return-value check
Summary:
Add support for checking class template member functions.
Also add the following functions to be checked by default:
- std::unique_ptr::release
- std::basic_string::empty
- std::vector::empty
Reviewers: alexfh, hokein, aaron.ballman, ilya-biryukov
Reviewed By: aaron.ballman
Subscribers: jbcoe, xazax.hun, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D45891
Patch by khuttun (Kalle Huttunen)
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@330772 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/bugprone')
-rw-r--r-- | clang-tidy/bugprone/UnusedReturnValueCheck.cpp | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/clang-tidy/bugprone/UnusedReturnValueCheck.cpp b/clang-tidy/bugprone/UnusedReturnValueCheck.cpp index 198d0dbe..d0c58857 100644 --- a/clang-tidy/bugprone/UnusedReturnValueCheck.cpp +++ b/clang-tidy/bugprone/UnusedReturnValueCheck.cpp @@ -19,14 +19,32 @@ namespace clang { namespace tidy { namespace bugprone { +namespace { + +// Matches functions that are instantiated from a class template member function +// matching InnerMatcher. Functions not instantiated from a class template +// member function are matched directly with InnerMatcher. +AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>, + InnerMatcher) { + FunctionDecl *InstantiatedFrom = Node.getInstantiatedFromMemberFunction(); + return InnerMatcher.matches(InstantiatedFrom ? *InstantiatedFrom : Node, + Finder, Builder); +} + +} // namespace + UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), - CheckedFunctions(Options.get("CheckedFunctions", "::std::async;" - "::std::launder;" - "::std::remove;" - "::std::remove_if;" - "::std::unique")) {} + CheckedFunctions(Options.get("CheckedFunctions", + "::std::async;" + "::std::launder;" + "::std::remove;" + "::std::remove_if;" + "::std::unique;" + "::std::unique_ptr::release;" + "::std::basic_string::empty;" + "::std::vector::empty")) {} void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "CheckedFunctions", CheckedFunctions); @@ -35,11 +53,11 @@ void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) { auto FunVec = utils::options::parseStringList(CheckedFunctions); auto MatchedCallExpr = expr(ignoringImplicit(ignoringParenImpCasts( - callExpr( - callee(functionDecl( - // Don't match void overloads of checked functions. - unless(returns(voidType())), hasAnyName(std::vector<StringRef>( - FunVec.begin(), FunVec.end()))))) + callExpr(callee(functionDecl( + // Don't match void overloads of checked functions. + unless(returns(voidType())), + isInstantiatedFrom(hasAnyName( + std::vector<StringRef>(FunVec.begin(), FunVec.end())))))) .bind("match")))); auto UnusedInCompoundStmt = |