Skip to content

[clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument #84333

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

Conversation

HerrCai0907
Copy link
Contributor

String without $ end causes incorrectly match results, For example std::unique can match std::unique_ptr::unique_ptr.
It causes false negative.
Fixes: #84314.

@llvmbot
Copy link
Member

llvmbot commented Mar 7, 2024

@llvm/pr-subscribers-clang-tidy

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

Author: Congcong Cai (HerrCai0907)

Changes

String without $ end causes incorrectly match results, For example std::unique can match std::unique_ptr::unique_ptr.
It causes false negative.
Fixes: #84314.


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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp (+92-92)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value.cpp (+14)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index b4bf85c912c3ca..1252b2f23805a1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -34,102 +34,102 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
                                                ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       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;"
+          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;"
+                                          "::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;"
+                                          "::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;"
-                                            "::std::errc;"
-                                            "::std::expected;"
+          Options.get("CheckedReturnTypes", "::std::error_code$;"
+                                            "::std::error_condition$;"
+                                            "::std::errc$;"
+                                            "::std::expected$;"
                                             "::boost::system::error_code"))),
       AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d98c4ff9a75044..aecf7cac66097c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -152,7 +152,9 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-unused-return-value
   <clang-tidy/checks/bugprone/unused-return-value>` check by updating the
-  parameter `CheckedFunctions` to support regexp.
+  parameter `CheckedFunctions` to support regexp and avoiding false negative for
+  function with the same prefix as the default argument, e.g. ``std::unique_ptr``
+  and ``std::unique``.
 
 - Improved :doc:`bugprone-use-after-move
   <clang-tidy/checks/bugprone/use-after-move>` check to also handle
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value.cpp
index 5c6ce1e4bf1fd2..e784c9b85172cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value.cpp
@@ -30,6 +30,11 @@ struct default_delete;
 
 template <typename T, typename Deleter = std::default_delete<T>>
 struct unique_ptr {
+  unique_ptr();
+  unique_ptr(unique_ptr const&);
+  unique_ptr(unique_ptr &&);
+  unique_ptr& operator=(unique_ptr const&);
+  unique_ptr& operator=(unique_ptr &&);
   T *release() noexcept;
 };
 
@@ -254,3 +259,12 @@ void noWarning() {
   ({ std::async(increment, 42); });
   auto StmtExprRetval = ({ std::async(increment, 42); });
 }
+
+namespace gh84314 {
+
+extern std::unique_ptr<int> alloc();
+void f1(std::unique_ptr<int> &foo) {
+    foo = alloc();
+}
+
+} // namespace gh84314
\ No newline at end of file

@HerrCai0907 HerrCai0907 changed the title [clang-tidy]avoid bugprone-unused-return-value false negative for function with the same prefix as the default argument [clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument Mar 7, 2024
@HerrCai0907 HerrCai0907 requested review from PiotrZSL and 5chmidti March 7, 2024 15:32
…ction with the same prefix as the default argument

string without `$` end will cause incorrectly match results.
Fixes: llvm#84314.
@HerrCai0907 HerrCai0907 force-pushed the 84314-clang-tidy-bugprone-unused-return-value-false-positive-for-unique_ptr-assignment branch from f96e0c7 to aaa9ea1 Compare March 7, 2024 15:33
@firewave
Copy link

firewave commented Mar 7, 2024

I think this might also require documentation changes.

The documentation is also a bit misleading in terms of the defaults: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unused-return-value.html. I add issues detecting a custom function as it required the :: prefix.

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 I agree that documentation should be changed to put there configuration in format that it is in .cpp file.

Copy link
Contributor

@5chmidti 5chmidti left a comment

Choose a reason for hiding this comment

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

+1 on the docs

@HerrCai0907 HerrCai0907 merged commit 912ea6e into llvm:main Mar 8, 2024
@HerrCai0907 HerrCai0907 deleted the 84314-clang-tidy-bugprone-unused-return-value-false-positive-for-unique_ptr-assignment branch March 8, 2024 13:19
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.

[clang-tidy] bugprone-unused-return-value false positive for unique_ptr assignment
5 participants