Skip to content

[clang-tidy] bugprone-unused-return-value config now supports regexes #82952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2024

Conversation

felix642
Copy link
Contributor

@felix642 felix642 commented Feb 26, 2024

The parameter CheckedFunctions now supports regexes

Fixes #63107

@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: Félix-Antoine Constantin (felix642)

Changes

The parameter CheckedReturnTypes now supports regexes

Fixes #63107


Full diff: https://github.com/llvm/llvm-project/pull/82952.diff

5 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp (+101-101)
  • (modified) clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h (+3-3)
  • (modified) clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp (+5-3)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp (+1-1)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 05012c7df6a975..b4bf85c912c3ca 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -33,98 +33,98 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
 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;"
-                                   "::std::unique_ptr::release;"
-                                   "::std::basic_string::empty;"
-                                   "::std::vector::empty;"
-                                   "::std::back_inserter;"
-                                   "::std::distance;"
-                                   "::std::find;"
-                                   "::std::find_if;"
-                                   "::std::inserter;"
-                                   "::std::lower_bound;"
-                                   "::std::make_pair;"
-                                   "::std::map::count;"
-                                   "::std::map::find;"
-                                   "::std::map::lower_bound;"
-                                   "::std::multimap::equal_range;"
-                                   "::std::multimap::upper_bound;"
-                                   "::std::set::count;"
-                                   "::std::set::find;"
-                                   "::std::setfill;"
-                                   "::std::setprecision;"
-                                   "::std::setw;"
-                                   "::std::upper_bound;"
-                                   "::std::vector::at;"
-                                   // C standard library
-                                   "::bsearch;"
-                                   "::ferror;"
-                                   "::feof;"
-                                   "::isalnum;"
-                                   "::isalpha;"
-                                   "::isblank;"
-                                   "::iscntrl;"
-                                   "::isdigit;"
-                                   "::isgraph;"
-                                   "::islower;"
-                                   "::isprint;"
-                                   "::ispunct;"
-                                   "::isspace;"
-                                   "::isupper;"
-                                   "::iswalnum;"
-                                   "::iswprint;"
-                                   "::iswspace;"
-                                   "::isxdigit;"
-                                   "::memchr;"
-                                   "::memcmp;"
-                                   "::strcmp;"
-                                   "::strcoll;"
-                                   "::strncmp;"
-                                   "::strpbrk;"
-                                   "::strrchr;"
-                                   "::strspn;"
-                                   "::strstr;"
-                                   "::wcscmp;"
-                                   // POSIX
-                                   "::access;"
-                                   "::bind;"
-                                   "::connect;"
-                                   "::difftime;"
-                                   "::dlsym;"
-                                   "::fnmatch;"
-                                   "::getaddrinfo;"
-                                   "::getopt;"
-                                   "::htonl;"
-                                   "::htons;"
-                                   "::iconv_open;"
-                                   "::inet_addr;"
-                                   "::isascii;"
-                                   "::isatty;"
-                                   "::mmap;"
-                                   "::newlocale;"
-                                   "::openat;"
-                                   "::pathconf;"
-                                   "::pthread_equal;"
-                                   "::pthread_getspecific;"
-                                   "::pthread_mutex_trylock;"
-                                   "::readdir;"
-                                   "::readlink;"
-                                   "::recvmsg;"
-                                   "::regexec;"
-                                   "::scandir;"
-                                   "::semget;"
-                                   "::setjmp;"
-                                   "::shm_open;"
-                                   "::shmget;"
-                                   "::sigismember;"
-                                   "::strcasecmp;"
-                                   "::strsignal;"
-                                   "::ttyname")),
+      CheckedFunctions(utils::options::parseStringList(
+          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;"
+                                          "::std::back_inserter;"
+                                          "::std::distance;"
+                                          "::std::find;"
+                                          "::std::find_if;"
+                                          "::std::inserter;"
+                                          "::std::lower_bound;"
+                                          "::std::make_pair;"
+                                          "::std::map::count;"
+                                          "::std::map::find;"
+                                          "::std::map::lower_bound;"
+                                          "::std::multimap::equal_range;"
+                                          "::std::multimap::upper_bound;"
+                                          "::std::set::count;"
+                                          "::std::set::find;"
+                                          "::std::setfill;"
+                                          "::std::setprecision;"
+                                          "::std::setw;"
+                                          "::std::upper_bound;"
+                                          "::std::vector::at;"
+                                          // C standard library
+                                          "::bsearch;"
+                                          "::ferror;"
+                                          "::feof;"
+                                          "::isalnum;"
+                                          "::isalpha;"
+                                          "::isblank;"
+                                          "::iscntrl;"
+                                          "::isdigit;"
+                                          "::isgraph;"
+                                          "::islower;"
+                                          "::isprint;"
+                                          "::ispunct;"
+                                          "::isspace;"
+                                          "::isupper;"
+                                          "::iswalnum;"
+                                          "::iswprint;"
+                                          "::iswspace;"
+                                          "::isxdigit;"
+                                          "::memchr;"
+                                          "::memcmp;"
+                                          "::strcmp;"
+                                          "::strcoll;"
+                                          "::strncmp;"
+                                          "::strpbrk;"
+                                          "::strrchr;"
+                                          "::strspn;"
+                                          "::strstr;"
+                                          "::wcscmp;"
+                                          // POSIX
+                                          "::access;"
+                                          "::bind;"
+                                          "::connect;"
+                                          "::difftime;"
+                                          "::dlsym;"
+                                          "::fnmatch;"
+                                          "::getaddrinfo;"
+                                          "::getopt;"
+                                          "::htonl;"
+                                          "::htons;"
+                                          "::iconv_open;"
+                                          "::inet_addr;"
+                                          "::isascii;"
+                                          "::isatty;"
+                                          "::mmap;"
+                                          "::newlocale;"
+                                          "::openat;"
+                                          "::pathconf;"
+                                          "::pthread_equal;"
+                                          "::pthread_getspecific;"
+                                          "::pthread_mutex_trylock;"
+                                          "::readdir;"
+                                          "::readlink;"
+                                          "::recvmsg;"
+                                          "::regexec;"
+                                          "::scandir;"
+                                          "::semget;"
+                                          "::setjmp;"
+                                          "::shm_open;"
+                                          "::shmget;"
+                                          "::sigismember;"
+                                          "::strcasecmp;"
+                                          "::strsignal;"
+                                          "::ttyname"))),
       CheckedReturnTypes(utils::options::parseStringList(
           Options.get("CheckedReturnTypes", "::std::error_code;"
                                             "::std::error_condition;"
@@ -133,36 +133,36 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
                                             "::boost::system::error_code"))),
       AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
 
-UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
-                                               ClangTidyContext *Context,
-                                               std::string CheckedFunctions)
+UnusedReturnValueCheck::UnusedReturnValueCheck(
+    llvm::StringRef Name, ClangTidyContext *Context,
+    std::vector<StringRef> CheckedFunctions)
     : UnusedReturnValueCheck(Name, Context, std::move(CheckedFunctions), {},
                              false) {}
 
 UnusedReturnValueCheck::UnusedReturnValueCheck(
     llvm::StringRef Name, ClangTidyContext *Context,
-    std::string CheckedFunctions, std::vector<StringRef> CheckedReturnTypes,
-    bool AllowCastToVoid)
+    std::vector<StringRef> CheckedFunctions,
+    std::vector<StringRef> CheckedReturnTypes, bool AllowCastToVoid)
     : ClangTidyCheck(Name, Context),
       CheckedFunctions(std::move(CheckedFunctions)),
       CheckedReturnTypes(std::move(CheckedReturnTypes)),
       AllowCastToVoid(AllowCastToVoid) {}
 
 void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
-  Options.store(Opts, "CheckedFunctions", CheckedFunctions);
+  Options.store(Opts, "CheckedFunctions",
+                utils::options::serializeStringList(CheckedFunctions));
   Options.store(Opts, "CheckedReturnTypes",
                 utils::options::serializeStringList(CheckedReturnTypes));
   Options.store(Opts, "AllowCastToVoid", AllowCastToVoid);
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
-  auto FunVec = utils::options::parseStringList(CheckedFunctions);
-
   auto MatchedDirectCallExpr =
       expr(callExpr(callee(functionDecl(
                         // Don't match void overloads of checked functions.
                         unless(returns(voidType())),
-                        anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
+                        anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
+                                  CheckedFunctions)),
                               returns(hasCanonicalType(hasDeclaration(
                                   namedDecl(matchers::matchesAnyListedName(
                                       CheckedReturnTypes)))))))))
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
index ab2cc691b894f7..d65a567e1c468a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
@@ -29,14 +29,14 @@ class UnusedReturnValueCheck : public ClangTidyCheck {
   }
 
 private:
-  std::string CheckedFunctions;
+  const std::vector<StringRef> CheckedFunctions;
   const std::vector<StringRef> CheckedReturnTypes;
 
 protected:
   UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context,
-                         std::string CheckedFunctions);
+                         std::vector<StringRef> CheckedFunctions);
   UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context,
-                         std::string CheckedFunctions,
+                         std::vector<StringRef> CheckedFunctions,
                          std::vector<StringRef> CheckedReturnTypes,
                          bool AllowCastToVoid);
   bool AllowCastToVoid;
diff --git a/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp b/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp
index 3410559d435f63..8020f8cd062510 100644
--- a/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp
@@ -13,9 +13,11 @@ namespace clang::tidy::hicpp {
 IgnoredRemoveResultCheck::IgnoredRemoveResultCheck(llvm::StringRef Name,
                                                    ClangTidyContext *Context)
     : UnusedReturnValueCheck(Name, Context,
-                             "::std::remove;"
-                             "::std::remove_if;"
-                             "::std::unique") {
+                             {
+                                 "::std::remove",
+                                 "::std::remove_if",
+                                 "::std::unique",
+                             }) {
   // The constructor for ClangTidyCheck needs to have been called
   // before we can access options via Options.get().
   AllowCastToVoid = Options.get("AllowCastToVoid", true);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2223e58947f600 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -134,6 +134,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/unused-local-non-trivial-variable>` check by
   ignoring local variable with ``[maybe_unused]`` attribute.
 
+ - Improved :doc:`bugprone-unused-return-value
+   <clang-tidy/checks/bugprone/unused-return-value>` check by updating the
+   parameter `CheckedFunctions` to support regex
+
 - Cleaned up :doc:`cppcoreguidelines-prefer-member-initializer
   <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>`
   by removing enforcement of rule `C.48
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp
index d3650b210ab02b..3035183573ccd7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
 // RUN: -config='{CheckOptions: \
 // RUN:  {bugprone-unused-return-value.CheckedFunctions: \
-// RUN:    "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::memFun;::ns::ClassTemplate::staticFun"}}' \
+// RUN:    "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::(mem|static)Fun"}}' \
 // RUN: --
 
 namespace std {

@llvmbot
Copy link
Member

llvmbot commented Feb 26, 2024

@llvm/pr-subscribers-clang-tidy

Author: Félix-Antoine Constantin (felix642)

Changes

The parameter CheckedReturnTypes now supports regexes

Fixes #63107


Full diff: https://github.com/llvm/llvm-project/pull/82952.diff

5 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp (+101-101)
  • (modified) clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h (+3-3)
  • (modified) clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp (+5-3)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp (+1-1)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 05012c7df6a975..b4bf85c912c3ca 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -33,98 +33,98 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
 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;"
-                                   "::std::unique_ptr::release;"
-                                   "::std::basic_string::empty;"
-                                   "::std::vector::empty;"
-                                   "::std::back_inserter;"
-                                   "::std::distance;"
-                                   "::std::find;"
-                                   "::std::find_if;"
-                                   "::std::inserter;"
-                                   "::std::lower_bound;"
-                                   "::std::make_pair;"
-                                   "::std::map::count;"
-                                   "::std::map::find;"
-                                   "::std::map::lower_bound;"
-                                   "::std::multimap::equal_range;"
-                                   "::std::multimap::upper_bound;"
-                                   "::std::set::count;"
-                                   "::std::set::find;"
-                                   "::std::setfill;"
-                                   "::std::setprecision;"
-                                   "::std::setw;"
-                                   "::std::upper_bound;"
-                                   "::std::vector::at;"
-                                   // C standard library
-                                   "::bsearch;"
-                                   "::ferror;"
-                                   "::feof;"
-                                   "::isalnum;"
-                                   "::isalpha;"
-                                   "::isblank;"
-                                   "::iscntrl;"
-                                   "::isdigit;"
-                                   "::isgraph;"
-                                   "::islower;"
-                                   "::isprint;"
-                                   "::ispunct;"
-                                   "::isspace;"
-                                   "::isupper;"
-                                   "::iswalnum;"
-                                   "::iswprint;"
-                                   "::iswspace;"
-                                   "::isxdigit;"
-                                   "::memchr;"
-                                   "::memcmp;"
-                                   "::strcmp;"
-                                   "::strcoll;"
-                                   "::strncmp;"
-                                   "::strpbrk;"
-                                   "::strrchr;"
-                                   "::strspn;"
-                                   "::strstr;"
-                                   "::wcscmp;"
-                                   // POSIX
-                                   "::access;"
-                                   "::bind;"
-                                   "::connect;"
-                                   "::difftime;"
-                                   "::dlsym;"
-                                   "::fnmatch;"
-                                   "::getaddrinfo;"
-                                   "::getopt;"
-                                   "::htonl;"
-                                   "::htons;"
-                                   "::iconv_open;"
-                                   "::inet_addr;"
-                                   "::isascii;"
-                                   "::isatty;"
-                                   "::mmap;"
-                                   "::newlocale;"
-                                   "::openat;"
-                                   "::pathconf;"
-                                   "::pthread_equal;"
-                                   "::pthread_getspecific;"
-                                   "::pthread_mutex_trylock;"
-                                   "::readdir;"
-                                   "::readlink;"
-                                   "::recvmsg;"
-                                   "::regexec;"
-                                   "::scandir;"
-                                   "::semget;"
-                                   "::setjmp;"
-                                   "::shm_open;"
-                                   "::shmget;"
-                                   "::sigismember;"
-                                   "::strcasecmp;"
-                                   "::strsignal;"
-                                   "::ttyname")),
+      CheckedFunctions(utils::options::parseStringList(
+          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;"
+                                          "::std::back_inserter;"
+                                          "::std::distance;"
+                                          "::std::find;"
+                                          "::std::find_if;"
+                                          "::std::inserter;"
+                                          "::std::lower_bound;"
+                                          "::std::make_pair;"
+                                          "::std::map::count;"
+                                          "::std::map::find;"
+                                          "::std::map::lower_bound;"
+                                          "::std::multimap::equal_range;"
+                                          "::std::multimap::upper_bound;"
+                                          "::std::set::count;"
+                                          "::std::set::find;"
+                                          "::std::setfill;"
+                                          "::std::setprecision;"
+                                          "::std::setw;"
+                                          "::std::upper_bound;"
+                                          "::std::vector::at;"
+                                          // C standard library
+                                          "::bsearch;"
+                                          "::ferror;"
+                                          "::feof;"
+                                          "::isalnum;"
+                                          "::isalpha;"
+                                          "::isblank;"
+                                          "::iscntrl;"
+                                          "::isdigit;"
+                                          "::isgraph;"
+                                          "::islower;"
+                                          "::isprint;"
+                                          "::ispunct;"
+                                          "::isspace;"
+                                          "::isupper;"
+                                          "::iswalnum;"
+                                          "::iswprint;"
+                                          "::iswspace;"
+                                          "::isxdigit;"
+                                          "::memchr;"
+                                          "::memcmp;"
+                                          "::strcmp;"
+                                          "::strcoll;"
+                                          "::strncmp;"
+                                          "::strpbrk;"
+                                          "::strrchr;"
+                                          "::strspn;"
+                                          "::strstr;"
+                                          "::wcscmp;"
+                                          // POSIX
+                                          "::access;"
+                                          "::bind;"
+                                          "::connect;"
+                                          "::difftime;"
+                                          "::dlsym;"
+                                          "::fnmatch;"
+                                          "::getaddrinfo;"
+                                          "::getopt;"
+                                          "::htonl;"
+                                          "::htons;"
+                                          "::iconv_open;"
+                                          "::inet_addr;"
+                                          "::isascii;"
+                                          "::isatty;"
+                                          "::mmap;"
+                                          "::newlocale;"
+                                          "::openat;"
+                                          "::pathconf;"
+                                          "::pthread_equal;"
+                                          "::pthread_getspecific;"
+                                          "::pthread_mutex_trylock;"
+                                          "::readdir;"
+                                          "::readlink;"
+                                          "::recvmsg;"
+                                          "::regexec;"
+                                          "::scandir;"
+                                          "::semget;"
+                                          "::setjmp;"
+                                          "::shm_open;"
+                                          "::shmget;"
+                                          "::sigismember;"
+                                          "::strcasecmp;"
+                                          "::strsignal;"
+                                          "::ttyname"))),
       CheckedReturnTypes(utils::options::parseStringList(
           Options.get("CheckedReturnTypes", "::std::error_code;"
                                             "::std::error_condition;"
@@ -133,36 +133,36 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
                                             "::boost::system::error_code"))),
       AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
 
-UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
-                                               ClangTidyContext *Context,
-                                               std::string CheckedFunctions)
+UnusedReturnValueCheck::UnusedReturnValueCheck(
+    llvm::StringRef Name, ClangTidyContext *Context,
+    std::vector<StringRef> CheckedFunctions)
     : UnusedReturnValueCheck(Name, Context, std::move(CheckedFunctions), {},
                              false) {}
 
 UnusedReturnValueCheck::UnusedReturnValueCheck(
     llvm::StringRef Name, ClangTidyContext *Context,
-    std::string CheckedFunctions, std::vector<StringRef> CheckedReturnTypes,
-    bool AllowCastToVoid)
+    std::vector<StringRef> CheckedFunctions,
+    std::vector<StringRef> CheckedReturnTypes, bool AllowCastToVoid)
     : ClangTidyCheck(Name, Context),
       CheckedFunctions(std::move(CheckedFunctions)),
       CheckedReturnTypes(std::move(CheckedReturnTypes)),
       AllowCastToVoid(AllowCastToVoid) {}
 
 void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
-  Options.store(Opts, "CheckedFunctions", CheckedFunctions);
+  Options.store(Opts, "CheckedFunctions",
+                utils::options::serializeStringList(CheckedFunctions));
   Options.store(Opts, "CheckedReturnTypes",
                 utils::options::serializeStringList(CheckedReturnTypes));
   Options.store(Opts, "AllowCastToVoid", AllowCastToVoid);
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
-  auto FunVec = utils::options::parseStringList(CheckedFunctions);
-
   auto MatchedDirectCallExpr =
       expr(callExpr(callee(functionDecl(
                         // Don't match void overloads of checked functions.
                         unless(returns(voidType())),
-                        anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
+                        anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
+                                  CheckedFunctions)),
                               returns(hasCanonicalType(hasDeclaration(
                                   namedDecl(matchers::matchesAnyListedName(
                                       CheckedReturnTypes)))))))))
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
index ab2cc691b894f7..d65a567e1c468a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.h
@@ -29,14 +29,14 @@ class UnusedReturnValueCheck : public ClangTidyCheck {
   }
 
 private:
-  std::string CheckedFunctions;
+  const std::vector<StringRef> CheckedFunctions;
   const std::vector<StringRef> CheckedReturnTypes;
 
 protected:
   UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context,
-                         std::string CheckedFunctions);
+                         std::vector<StringRef> CheckedFunctions);
   UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context,
-                         std::string CheckedFunctions,
+                         std::vector<StringRef> CheckedFunctions,
                          std::vector<StringRef> CheckedReturnTypes,
                          bool AllowCastToVoid);
   bool AllowCastToVoid;
diff --git a/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp b/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp
index 3410559d435f63..8020f8cd062510 100644
--- a/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp
@@ -13,9 +13,11 @@ namespace clang::tidy::hicpp {
 IgnoredRemoveResultCheck::IgnoredRemoveResultCheck(llvm::StringRef Name,
                                                    ClangTidyContext *Context)
     : UnusedReturnValueCheck(Name, Context,
-                             "::std::remove;"
-                             "::std::remove_if;"
-                             "::std::unique") {
+                             {
+                                 "::std::remove",
+                                 "::std::remove_if",
+                                 "::std::unique",
+                             }) {
   // The constructor for ClangTidyCheck needs to have been called
   // before we can access options via Options.get().
   AllowCastToVoid = Options.get("AllowCastToVoid", true);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2223e58947f600 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -134,6 +134,10 @@ Changes in existing checks
   <clang-tidy/checks/bugprone/unused-local-non-trivial-variable>` check by
   ignoring local variable with ``[maybe_unused]`` attribute.
 
+ - Improved :doc:`bugprone-unused-return-value
+   <clang-tidy/checks/bugprone/unused-return-value>` check by updating the
+   parameter `CheckedFunctions` to support regex
+
 - Cleaned up :doc:`cppcoreguidelines-prefer-member-initializer
   <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>`
   by removing enforcement of rule `C.48
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp
index d3650b210ab02b..3035183573ccd7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-custom.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
 // RUN: -config='{CheckOptions: \
 // RUN:  {bugprone-unused-return-value.CheckedFunctions: \
-// RUN:    "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::memFun;::ns::ClassTemplate::staticFun"}}' \
+// RUN:    "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::(mem|static)Fun"}}' \
 // RUN: --
 
 namespace std {

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but update also check documentation, simply put there default config so user could easily copy it change, and add new functions. And mention that this support regexp.

@PiotrZSL
Copy link
Member

PiotrZSL commented Mar 4, 2024

Rebase, and could be merged.

@felix642 felix642 force-pushed the bugprone-unused-return-regex-option branch from eed343a to 13540e4 Compare March 4, 2024 14:30
@felix642 felix642 force-pushed the bugprone-unused-return-regex-option branch from 13540e4 to 00885ea Compare March 4, 2024 14:32
@felix642
Copy link
Contributor Author

felix642 commented Mar 4, 2024

Rebase, and could be merged.

Done, thank you for the quick review.

@felix642
Copy link
Contributor Author

felix642 commented Mar 4, 2024

Nevermind I broke the documentation.. Hold on

@PiotrZSL PiotrZSL merged commit 0097fd2 into llvm:main Mar 4, 2024
HerrCai0907 added a commit to HerrCai0907/llvm-project that referenced this pull request Apr 4, 2024
…refixes as the target

Fixes: llvm#87359
llvm#82952 introduces regexes matching and causes false postives.
This patch fixes it by adding $
bjosv added a commit to Nordix/llvm-project that referenced this pull request Apr 10, 2025
…as target

PR llvm#82952 introduced regexes matching for CheckedFunctions used by this checker.
Fix false postives by adding end of string to target regexes.

Signed-off-by: Björn Svensson <[email protected]>
bjosv added a commit to Nordix/llvm-project that referenced this pull request Apr 10, 2025
…as target

PR llvm#82952 introduced regexes matching for CheckedFunctions used by this checker.
Fix false positives by adding end-of-string to target regexes.

Signed-off-by: Björn Svensson <[email protected]>
carlosgalvezp pushed a commit that referenced this pull request Apr 11, 2025
…s as target (#135160)

PR #82952 introduced regex matching for `CheckedFunctions` in
`UnusedReturnValueCheck` which is used by the checker `cert-err33-c`.

Add a testcase and fix false positives by adding end-of-string to target
regex's.

---------

Signed-off-by: Björn Svensson <[email protected]>
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…s as target (llvm#135160)

PR llvm#82952 introduced regex matching for `CheckedFunctions` in
`UnusedReturnValueCheck` which is used by the checker `cert-err33-c`.

Add a testcase and fix false positives by adding end-of-string to target
regex's.

---------

Signed-off-by: Björn Svensson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bugprone-unused-return-value: Allow * in CheckedFunctions and/or CheckedReturnTypes
4 participants