Skip to content

Commit 912ea6e

Browse files
authored
[clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument (#84333)
1 parent aa6e7a6 commit 912ea6e

File tree

4 files changed

+126
-109
lines changed

4 files changed

+126
-109
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/docs/clang-tidy/checks/bugprone/unused-return-value.rst

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ Options
1414
This parameter supports regexp. The function is checked if the name
1515
and scope matches, with any arguments.
1616
By default the following functions are checked:
17-
``std::async, std::launder, std::remove, std::remove_if, std::unique,
18-
std::unique_ptr::release, std::basic_string::empty, std::vector::empty,
19-
std::back_inserter, std::distance, std::find, std::find_if, std::inserter,
20-
std::lower_bound, std::make_pair, std::map::count, std::map::find,
21-
std::map::lower_bound, std::multimap::equal_range,
22-
std::multimap::upper_bound, std::set::count, std::set::find, std::setfill,
23-
std::setprecision, std::setw, std::upper_bound, std::vector::at,
24-
bsearch, ferror, feof, isalnum, isalpha, isblank, iscntrl, isdigit, isgraph,
25-
islower, isprint, ispunct, isspace, isupper, iswalnum, iswprint, iswspace,
26-
isxdigit, memchr, memcmp, strcmp, strcoll, strncmp, strpbrk, strrchr,
27-
strspn, strstr, wcscmp, access, bind, connect, difftime, dlsym, fnmatch,
28-
getaddrinfo, getopt, htonl, htons, iconv_open, inet_addr, isascii, isatty,
29-
mmap, newlocale, openat, pathconf, pthread_equal, pthread_getspecific,
30-
pthread_mutex_trylock, readdir, readlink, recvmsg, regexec, scandir,
31-
semget, setjmp, shm_open, shmget, sigismember, strcasecmp, strsignal,
32-
ttyname``
17+
``::std::async$, ::std::launder$, ::std::remove$, ::std::remove_if$, ::std::unique$,
18+
::std::unique_ptr::release$, ::std::basic_string::empty$, ::std::vector::empty$,
19+
::std::back_inserter$, ::std::distance$, ::std::find$, ::std::find_if$, ::std::inserter$,
20+
::std::lower_bound$, ::std::make_pair$, ::std::map::count$, ::std::map::find$,
21+
::std::map::lower_bound$, ::std::multimap::equal_range$, ::std::multimap::upper_bound$,
22+
::std::set::count$, ::std::set::find$, ::std::setfill$, ::std::setprecision$,
23+
::std::setw$, ::std::upper_bound$, ::std::vector::at$, ::bsearch$, ::ferror$,
24+
::feof$, ::isalnum$, ::isalpha$, ::isblank$, ::iscntrl$, ::isdigit$, ::isgraph$,
25+
::islower$, ::isprint$, ::ispunct$, ::isspace$, ::isupper$, ::iswalnum$, ::iswprint$,
26+
::iswspace$, ::isxdigit$, ::memchr$, ::memcmp$, ::strcmp$, ::strcoll$, ::strncmp$,
27+
::strpbrk$, ::strrchr$, ::strspn$, ::strstr$, ::wcscmp$, ::access$, ::bind$,
28+
::connect$, ::difftime$, ::dlsym$, ::fnmatch$, ::getaddrinfo$, ::getopt$,
29+
::htonl$, ::htons$, ::iconv_open$, ::inet_addr$, isascii$, isatty$, ::mmap$,
30+
::newlocale$, ::openat$, ::pathconf$, ::pthread_equal$, ::pthread_getspecific$,
31+
::pthread_mutex_trylock$, ::readdir$, ::readlink$, ::recvmsg$, ::regexec$, ::scandir$,
32+
::semget$, ::setjmp$, ::shm_open$, ::shmget$, ::sigismember$, ::strcasecmp$, ::strsignal$,
33+
::ttyname$``
3334

3435
- ``std::async()``. Not using the return value makes the call synchronous.
3536
- ``std::launder()``. Not using the return value usually means that the

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)