Skip to content

Commit 0097fd2

Browse files
authored
[clang-tidy] bugprone-unused-return-value config now supports regexes (#82952)
The parameter `CheckedFunctions` now supports regexes Fixes #63107
1 parent ac783ad commit 0097fd2

File tree

6 files changed

+117
-110
lines changed

6 files changed

+117
-110
lines changed

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

Lines changed: 101 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -33,98 +33,98 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
3333
UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
3434
ClangTidyContext *Context)
3535
: ClangTidyCheck(Name, Context),
36-
CheckedFunctions(Options.get("CheckedFunctions",
37-
"::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;"
64-
// 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;"
93-
// 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;"
127-
"::ttyname")),
36+
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;"
64+
// 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;"
93+
// 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;"
127+
"::ttyname"))),
128128
CheckedReturnTypes(utils::options::parseStringList(
129129
Options.get("CheckedReturnTypes", "::std::error_code;"
130130
"::std::error_condition;"
@@ -133,36 +133,36 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
133133
"::boost::system::error_code"))),
134134
AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
135135

136-
UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
137-
ClangTidyContext *Context,
138-
std::string CheckedFunctions)
136+
UnusedReturnValueCheck::UnusedReturnValueCheck(
137+
llvm::StringRef Name, ClangTidyContext *Context,
138+
std::vector<StringRef> CheckedFunctions)
139139
: UnusedReturnValueCheck(Name, Context, std::move(CheckedFunctions), {},
140140
false) {}
141141

142142
UnusedReturnValueCheck::UnusedReturnValueCheck(
143143
llvm::StringRef Name, ClangTidyContext *Context,
144-
std::string CheckedFunctions, std::vector<StringRef> CheckedReturnTypes,
145-
bool AllowCastToVoid)
144+
std::vector<StringRef> CheckedFunctions,
145+
std::vector<StringRef> CheckedReturnTypes, bool AllowCastToVoid)
146146
: ClangTidyCheck(Name, Context),
147147
CheckedFunctions(std::move(CheckedFunctions)),
148148
CheckedReturnTypes(std::move(CheckedReturnTypes)),
149149
AllowCastToVoid(AllowCastToVoid) {}
150150

151151
void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
152-
Options.store(Opts, "CheckedFunctions", CheckedFunctions);
152+
Options.store(Opts, "CheckedFunctions",
153+
utils::options::serializeStringList(CheckedFunctions));
153154
Options.store(Opts, "CheckedReturnTypes",
154155
utils::options::serializeStringList(CheckedReturnTypes));
155156
Options.store(Opts, "AllowCastToVoid", AllowCastToVoid);
156157
}
157158

158159
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
159-
auto FunVec = utils::options::parseStringList(CheckedFunctions);
160-
161160
auto MatchedDirectCallExpr =
162161
expr(callExpr(callee(functionDecl(
163162
// Don't match void overloads of checked functions.
164163
unless(returns(voidType())),
165-
anyOf(isInstantiatedFrom(hasAnyName(FunVec)),
164+
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
165+
CheckedFunctions)),
166166
returns(hasCanonicalType(hasDeclaration(
167167
namedDecl(matchers::matchesAnyListedName(
168168
CheckedReturnTypes)))))))))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ class UnusedReturnValueCheck : public ClangTidyCheck {
2929
}
3030

3131
private:
32-
std::string CheckedFunctions;
32+
const std::vector<StringRef> CheckedFunctions;
3333
const std::vector<StringRef> CheckedReturnTypes;
3434

3535
protected:
3636
UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context,
37-
std::string CheckedFunctions);
37+
std::vector<StringRef> CheckedFunctions);
3838
UnusedReturnValueCheck(StringRef Name, ClangTidyContext *Context,
39-
std::string CheckedFunctions,
39+
std::vector<StringRef> CheckedFunctions,
4040
std::vector<StringRef> CheckedReturnTypes,
4141
bool AllowCastToVoid);
4242
bool AllowCastToVoid;

clang-tools-extra/clang-tidy/hicpp/IgnoredRemoveResultCheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ namespace clang::tidy::hicpp {
1313
IgnoredRemoveResultCheck::IgnoredRemoveResultCheck(llvm::StringRef Name,
1414
ClangTidyContext *Context)
1515
: UnusedReturnValueCheck(Name, Context,
16-
"::std::remove;"
17-
"::std::remove_if;"
18-
"::std::unique") {
16+
{
17+
"::std::remove",
18+
"::std::remove_if",
19+
"::std::unique",
20+
}) {
1921
// The constructor for ClangTidyCheck needs to have been called
2022
// before we can access options via Options.get().
2123
AllowCastToVoid = Options.get("AllowCastToVoid", true);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ Changes in existing checks
140140
<clang-tidy/checks/bugprone/unused-local-non-trivial-variable>` check by
141141
ignoring local variable with ``[maybe_unused]`` attribute.
142142

143+
- Improved :doc:`bugprone-unused-return-value
144+
<clang-tidy/checks/bugprone/unused-return-value>` check by updating the
145+
parameter `CheckedFunctions` to support regexp.
146+
143147
- Improved :doc:`cppcoreguidelines-missing-std-forward
144148
<clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by no longer
145149
giving false positives for deleted functions.

clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-return-value.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ Options
1010

1111
.. option:: CheckedFunctions
1212

13-
Semicolon-separated list of functions to check. The function is checked if
14-
the name and scope matches, with any arguments.
13+
Semicolon-separated list of functions to check.
14+
This parameter supports regexp. The function is checked if the name
15+
and scope matches, with any arguments.
1516
By default the following functions are checked:
1617
``std::async, std::launder, std::remove, std::remove_if, std::unique,
1718
std::unique_ptr::release, std::basic_string::empty, std::vector::empty,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
22
// RUN: -config='{CheckOptions: \
33
// RUN: {bugprone-unused-return-value.CheckedFunctions: \
4-
// RUN: "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::memFun;::ns::ClassTemplate::staticFun"}}' \
4+
// RUN: "::fun;::ns::Outer::Inner::memFun;::ns::Type::staticFun;::ns::ClassTemplate::(mem|static)Fun"}}' \
55
// RUN: --
66

77
namespace std {

0 commit comments

Comments
 (0)