Skip to content

Commit aaa9ea1

Browse files
committed
[clang-tidy]avoid bugprone-unused-return-value false negative for function with the same prefix as the default argument
string without `$` end will cause incorrectly match results. Fixes: #84314.
1 parent a11ab13 commit aaa9ea1

File tree

3 files changed

+109
-93
lines changed

3 files changed

+109
-93
lines changed

clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -34,102 +34,102 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
3434
ClangTidyContext *Context)
3535
: ClangTidyCheck(Name, Context),
3636
CheckedFunctions(utils::options::parseStringList(
37-
Options.get("CheckedFunctions", "::std::async;"
38-
"::std::launder;"
39-
"::std::remove;"
40-
"::std::remove_if;"
41-
"::std::unique;"
42-
"::std::unique_ptr::release;"
43-
"::std::basic_string::empty;"
44-
"::std::vector::empty;"
45-
"::std::back_inserter;"
46-
"::std::distance;"
47-
"::std::find;"
48-
"::std::find_if;"
49-
"::std::inserter;"
50-
"::std::lower_bound;"
51-
"::std::make_pair;"
52-
"::std::map::count;"
53-
"::std::map::find;"
54-
"::std::map::lower_bound;"
55-
"::std::multimap::equal_range;"
56-
"::std::multimap::upper_bound;"
57-
"::std::set::count;"
58-
"::std::set::find;"
59-
"::std::setfill;"
60-
"::std::setprecision;"
61-
"::std::setw;"
62-
"::std::upper_bound;"
63-
"::std::vector::at;"
37+
Options.get("CheckedFunctions", "::std::async$;"
38+
"::std::launder$;"
39+
"::std::remove$;"
40+
"::std::remove_if$;"
41+
"::std::unique$;"
42+
"::std::unique_ptr::release$;"
43+
"::std::basic_string::empty$;"
44+
"::std::vector::empty$;"
45+
"::std::back_inserter$;"
46+
"::std::distance$;"
47+
"::std::find$;"
48+
"::std::find_if$;"
49+
"::std::inserter$;"
50+
"::std::lower_bound$;"
51+
"::std::make_pair$;"
52+
"::std::map::count$;"
53+
"::std::map::find$;"
54+
"::std::map::lower_bound$;"
55+
"::std::multimap::equal_range$;"
56+
"::std::multimap::upper_bound$;"
57+
"::std::set::count$;"
58+
"::std::set::find$;"
59+
"::std::setfill$;"
60+
"::std::setprecision$;"
61+
"::std::setw$;"
62+
"::std::upper_bound$;"
63+
"::std::vector::at$;"
6464
// C standard library
65-
"::bsearch;"
66-
"::ferror;"
67-
"::feof;"
68-
"::isalnum;"
69-
"::isalpha;"
70-
"::isblank;"
71-
"::iscntrl;"
72-
"::isdigit;"
73-
"::isgraph;"
74-
"::islower;"
75-
"::isprint;"
76-
"::ispunct;"
77-
"::isspace;"
78-
"::isupper;"
79-
"::iswalnum;"
80-
"::iswprint;"
81-
"::iswspace;"
82-
"::isxdigit;"
83-
"::memchr;"
84-
"::memcmp;"
85-
"::strcmp;"
86-
"::strcoll;"
87-
"::strncmp;"
88-
"::strpbrk;"
89-
"::strrchr;"
90-
"::strspn;"
91-
"::strstr;"
92-
"::wcscmp;"
65+
"::bsearch$;"
66+
"::ferror$;"
67+
"::feof$;"
68+
"::isalnum$;"
69+
"::isalpha$;"
70+
"::isblank$;"
71+
"::iscntrl$;"
72+
"::isdigit$;"
73+
"::isgraph$;"
74+
"::islower$;"
75+
"::isprint$;"
76+
"::ispunct$;"
77+
"::isspace$;"
78+
"::isupper$;"
79+
"::iswalnum$;"
80+
"::iswprint$;"
81+
"::iswspace$;"
82+
"::isxdigit$;"
83+
"::memchr$;"
84+
"::memcmp$;"
85+
"::strcmp$;"
86+
"::strcoll$;"
87+
"::strncmp$;"
88+
"::strpbrk$;"
89+
"::strrchr$;"
90+
"::strspn$;"
91+
"::strstr$;"
92+
"::wcscmp$;"
9393
// POSIX
94-
"::access;"
95-
"::bind;"
96-
"::connect;"
97-
"::difftime;"
98-
"::dlsym;"
99-
"::fnmatch;"
100-
"::getaddrinfo;"
101-
"::getopt;"
102-
"::htonl;"
103-
"::htons;"
104-
"::iconv_open;"
105-
"::inet_addr;"
106-
"::isascii;"
107-
"::isatty;"
108-
"::mmap;"
109-
"::newlocale;"
110-
"::openat;"
111-
"::pathconf;"
112-
"::pthread_equal;"
113-
"::pthread_getspecific;"
114-
"::pthread_mutex_trylock;"
115-
"::readdir;"
116-
"::readlink;"
117-
"::recvmsg;"
118-
"::regexec;"
119-
"::scandir;"
120-
"::semget;"
121-
"::setjmp;"
122-
"::shm_open;"
123-
"::shmget;"
124-
"::sigismember;"
125-
"::strcasecmp;"
126-
"::strsignal;"
94+
"::access$;"
95+
"::bind$;"
96+
"::connect$;"
97+
"::difftime$;"
98+
"::dlsym$;"
99+
"::fnmatch$;"
100+
"::getaddrinfo$;"
101+
"::getopt$;"
102+
"::htonl$;"
103+
"::htons$;"
104+
"::iconv_open$;"
105+
"::inet_addr$;"
106+
"::isascii$;"
107+
"::isatty$;"
108+
"::mmap$;"
109+
"::newlocale$;"
110+
"::openat$;"
111+
"::pathconf$;"
112+
"::pthread_equal$;"
113+
"::pthread_getspecific$;"
114+
"::pthread_mutex_trylock$;"
115+
"::readdir$;"
116+
"::readlink$;"
117+
"::recvmsg$;"
118+
"::regexec$;"
119+
"::scandir$;"
120+
"::semget$;"
121+
"::setjmp$;"
122+
"::shm_open$;"
123+
"::shmget$;"
124+
"::sigismember$;"
125+
"::strcasecmp$;"
126+
"::strsignal$;"
127127
"::ttyname"))),
128128
CheckedReturnTypes(utils::options::parseStringList(
129-
Options.get("CheckedReturnTypes", "::std::error_code;"
130-
"::std::error_condition;"
131-
"::std::errc;"
132-
"::std::expected;"
129+
Options.get("CheckedReturnTypes", "::std::error_code$;"
130+
"::std::error_condition$;"
131+
"::std::errc$;"
132+
"::std::expected$;"
133133
"::boost::system::error_code"))),
134134
AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
135135

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ Changes in existing checks
152152

153153
- Improved :doc:`bugprone-unused-return-value
154154
<clang-tidy/checks/bugprone/unused-return-value>` check by updating the
155-
parameter `CheckedFunctions` to support regexp.
155+
parameter `CheckedFunctions` to support regexp and avoiding false postive for
156+
function with the same prefix as the default argument, e.g. ``std::unique_ptr``
157+
and ``std::unique``.
156158

157159
- Improved :doc:`bugprone-use-after-move
158160
<clang-tidy/checks/bugprone/use-after-move>` check to also handle

clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ struct default_delete;
3030

3131
template <typename T, typename Deleter = std::default_delete<T>>
3232
struct unique_ptr {
33+
unique_ptr();
34+
unique_ptr(unique_ptr const&);
35+
unique_ptr(unique_ptr &&);
36+
unique_ptr& operator=(unique_ptr const&);
37+
unique_ptr& operator=(unique_ptr &&);
3338
T *release() noexcept;
3439
};
3540

@@ -254,3 +259,12 @@ void noWarning() {
254259
({ std::async(increment, 42); });
255260
auto StmtExprRetval = ({ std::async(increment, 42); });
256261
}
262+
263+
namespace gh84314 {
264+
265+
extern std::unique_ptr<int> alloc();
266+
void f1(std::unique_ptr<int> &foo) {
267+
foo = alloc();
268+
}
269+
270+
} // namespace gh84314

0 commit comments

Comments
 (0)