Skip to content

Commit c2ac1a6

Browse files
committed
[clang-tidy] Add AllowCastToVoid option to bugprone-unused-return-value
Add option that control casting to void behavior, change default issue suppression to disallow casting to void.
1 parent 028dbdb commit c2ac1a6

File tree

8 files changed

+29
-15
lines changed

8 files changed

+29
-15
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
130130
"::std::error_condition;"
131131
"::std::errc;"
132132
"::std::expected;"
133-
"::boost::system::error_code"))) {}
133+
"::boost::system::error_code"))),
134+
AllowCastToVoid(Options.get("AllowCastToVoid", false)) {}
134135

135136
void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
136137
Options.store(Opts, "CheckedFunctions", CheckedFunctions);
137138
Options.store(Opts, "CheckedReturnTypes",
138139
utils::options::serializeStringList(CheckedReturnTypes));
140+
Options.store(Opts, "AllowCastToVoid", AllowCastToVoid);
139141
}
140142

141143
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
@@ -151,11 +153,12 @@ void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
151153
CheckedReturnTypes)))))))))
152154
.bind("match"));
153155

154-
auto MatchedCallExpr =
155-
expr(anyOf(MatchedDirectCallExpr,
156-
explicitCastExpr(unless(cxxFunctionalCastExpr()),
157-
unless(hasCastKind(CK_ToVoid)),
158-
hasSourceExpression(MatchedDirectCallExpr))));
156+
auto CheckCastToVoid =
157+
AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
158+
auto MatchedCallExpr = expr(
159+
anyOf(MatchedDirectCallExpr,
160+
explicitCastExpr(unless(cxxFunctionalCastExpr()), CheckCastToVoid,
161+
hasSourceExpression(MatchedDirectCallExpr))));
159162

160163
auto UnusedInCompoundStmt =
161164
compoundStmt(forEach(MatchedCallExpr),
@@ -187,6 +190,10 @@ void UnusedReturnValueCheck::check(const MatchFinder::MatchResult &Result) {
187190
"the value returned by this function should not be disregarded; "
188191
"neglecting it may lead to errors")
189192
<< Matched->getSourceRange();
193+
194+
if (!AllowCastToVoid)
195+
return;
196+
190197
diag(Matched->getBeginLoc(),
191198
"cast the expression to void to silence this warning",
192199
DiagnosticIDs::Note);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class UnusedReturnValueCheck : public ClangTidyCheck {
3131
private:
3232
std::string CheckedFunctions;
3333
const std::vector<StringRef> CheckedReturnTypes;
34+
const bool AllowCastToVoid;
3435
};
3536

3637
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class CERTModule : public ClangTidyModule {
328328
ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
329329
Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
330330
Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
331+
Opts["cert-err33-c.AllowCastToVoid"] = "true";
331332
Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
332333
Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
333334
return Options;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ Changes in existing checks
235235
- Improved :doc:`bugprone-unused-return-value
236236
<clang-tidy/checks/bugprone/unused-return-value>` check diagnostic message,
237237
added support for detection of unused results when cast to non-``void`` type.
238+
Casting to ``void`` no longer suppresses issues by default, control this
239+
behavior with the new `AllowCastToVoid` option.
238240

239241
- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables
240242
<clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@ Options
5252
By default the following function return types are checked:
5353
`::std::error_code`, `::std::error_condition`, `::std::errc`, `::std::expected`, `::boost::system::error_code`
5454

55+
.. option:: AllowCastToVoid
56+
57+
Controls whether casting return values to ``void`` is permitted. Default: `false`.
58+
5559
:doc:`cert-err33-c <../cert/err33-c>` is an alias of this check that checks a
5660
fixed and large set of standard library functions.

clang-tools-extra/docs/clang-tidy/checks/cert/err33-c.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ functions are checked:
189189
This check is an alias of check :doc:`bugprone-unused-return-value <../bugprone/unused-return-value>`
190190
with a fixed set of functions.
191191

192+
Suppressing issues by casting to ``void`` is enabled by default and can be
193+
disabled by setting `AllowCastToVoid` option to ``false``.
194+
192195
The check corresponds to a part of CERT C Coding Standard rule `ERR33-C.
193196
Detect and handle standard library errors
194197
<https://wiki.sei.cmu.edu/confluence/display/c/ERR33-C.+Detect+and+handle+standard+library+errors>`_.

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,34 @@ void fun(int);
4848
void warning() {
4949
fun();
5050
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
51-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
5251

5352
(fun());
5453
// CHECK-MESSAGES: [[@LINE-1]]:4: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
55-
// CHECK-MESSAGES: [[@LINE-2]]:4: note: cast the expression to void to silence this warning
54+
55+
(void)fun();
56+
// CHECK-MESSAGES: [[@LINE-1]]:9: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
5657

5758
ns::Outer::Inner ObjA1;
5859
ObjA1.memFun();
5960
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
60-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
6161

6262
ns::AliasName::Inner ObjA2;
6363
ObjA2.memFun();
6464
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
65-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
6665

6766
ns::Derived ObjA3;
6867
ObjA3.memFun();
6968
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
70-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
7169

7270
ns::Type::staticFun();
7371
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
74-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
7572

7673
ns::ClassTemplate<int> ObjA4;
7774
ObjA4.memFun();
7875
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
79-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
8076

8177
ns::ClassTemplate<int>::staticFun();
8278
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
83-
// CHECK-MESSAGES: [[@LINE-2]]:3: note: cast the expression to void to silence this warning
8479
}
8580

8681
void noWarning() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// RUN: %check_clang_tidy %s bugprone-unused-return-value %t -- -- -fexceptions
1+
// RUN: %check_clang_tidy %s bugprone-unused-return-value %t -- \
2+
// RUN: --config="{CheckOptions: {bugprone-unused-return-value.AllowCastToVoid: true}}" -- -fexceptions
23

34
namespace std {
45

0 commit comments

Comments
 (0)