Skip to content

Commit 265db5e

Browse files
committed
[clang-tidy]avoid bugprone-unused-return-value false positive for assignment operator overloading
Fixes: #84480 We assuem assignemnt at most of time, operator overloading means the value is assigned to the other variable, then clang-tidy should suppress warning even if this operator overloading match the regex.
1 parent 7415524 commit 265db5e

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../utils/OptionsUtils.h"
1212
#include "clang/AST/ASTContext.h"
1313
#include "clang/ASTMatchers/ASTMatchFinder.h"
14+
#include "clang/ASTMatchers/ASTMatchers.h"
1415

1516
using namespace clang::ast_matchers;
1617
using namespace clang::ast_matchers::internal;
@@ -157,16 +158,19 @@ void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
157158
}
158159

159160
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
160-
auto MatchedDirectCallExpr =
161-
expr(callExpr(callee(functionDecl(
162-
// Don't match void overloads of checked functions.
163-
unless(returns(voidType())),
164-
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
165-
CheckedFunctions)),
166-
returns(hasCanonicalType(hasDeclaration(
167-
namedDecl(matchers::matchesAnyListedName(
168-
CheckedReturnTypes)))))))))
169-
.bind("match"));
161+
auto MatchedDirectCallExpr = expr(
162+
callExpr(callee(functionDecl(
163+
// Don't match void overloads of checked functions.
164+
unless(returns(voidType())),
165+
// Don't match copy or move assignment operator.
166+
unless(cxxMethodDecl(anyOf(isCopyAssignmentOperator(),
167+
isMoveAssignmentOperator()))),
168+
anyOf(isInstantiatedFrom(
169+
matchers::matchesAnyListedName(CheckedFunctions)),
170+
returns(hasCanonicalType(hasDeclaration(
171+
namedDecl(matchers::matchesAnyListedName(
172+
CheckedReturnTypes)))))))))
173+
.bind("match"));
170174

171175
auto CheckCastToVoid =
172176
AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +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 and avoiding false postive for
155+
parameter `CheckedFunctions` to support regexp, avoiding false positive for
156156
function with the same prefix as the default argument, e.g. ``std::unique_ptr``
157-
and ``std::unique``.
157+
and ``std::unique``, avoiding false positive for assignment operator overloading.
158158

159159
- Improved :doc:`bugprone-use-after-move
160160
<clang-tidy/checks/bugprone/use-after-move>` check to also handle
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
2+
// RUN: -config='{CheckOptions: \
3+
// RUN: {bugprone-unused-return-value.CheckedFunctions: "::*"}}' \
4+
// RUN: --
5+
6+
struct S {
7+
S(){};
8+
S(S const &);
9+
S(S &&);
10+
S &operator=(S const &);
11+
S &operator=(S &&);
12+
};
13+
14+
S returnValue();
15+
S const &returnRef();
16+
17+
void bar() {
18+
returnValue();
19+
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
20+
21+
S a{};
22+
a = returnValue();
23+
// CHECK-NOT: [[@LINE-1]]:3: warning
24+
a.operator=(returnValue());
25+
// CHECK-NOT: [[@LINE-1]]:3: warning
26+
27+
a = returnRef();
28+
// CHECK-NOT: [[@LINE-1]]:3: warning
29+
a.operator=(returnRef());
30+
// CHECK-NOT: [[@LINE-1]]:3: warning
31+
}

0 commit comments

Comments
 (0)