Skip to content

Commit 12b802a

Browse files
authored
[clang-tidy]bugprone-unused-return-value ignore ++ and -- operator overloading (llvm#84922)
Fixes: llvm#84705 Further fix for llvm#84489
1 parent 57914f6 commit 12b802a

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, Matcher<FunctionDecl>,
3131
Finder, Builder);
3232
}
3333

34-
AST_MATCHER_P(CXXMethodDecl, isOperatorOverloading,
35-
llvm::SmallVector<OverloadedOperatorKind>, Kinds) {
36-
return llvm::is_contained(Kinds, Node.getOverloadedOperator());
34+
constexpr std::initializer_list<OverloadedOperatorKind>
35+
AssignmentOverloadedOperatorKinds = {
36+
OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
37+
OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
38+
OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual, OO_PlusPlus,
39+
OO_MinusMinus};
40+
41+
AST_MATCHER(FunctionDecl, isAssignmentOverloadedOperator) {
42+
return llvm::is_contained(AssignmentOverloadedOperatorKinds,
43+
Node.getOverloadedOperator());
3744
}
3845
} // namespace
3946

@@ -164,22 +171,18 @@ void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
164171
}
165172

166173
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
167-
auto MatchedDirectCallExpr = expr(
168-
callExpr(
169-
callee(functionDecl(
170-
// Don't match void overloads of checked functions.
171-
unless(returns(voidType())),
172-
// Don't match copy or move assignment operator.
173-
unless(cxxMethodDecl(isOperatorOverloading(
174-
{OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
175-
OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
176-
OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
177-
anyOf(
178-
isInstantiatedFrom(
179-
matchers::matchesAnyListedName(CheckedFunctions)),
180-
returns(hasCanonicalType(hasDeclaration(namedDecl(
181-
matchers::matchesAnyListedName(CheckedReturnTypes)))))))))
182-
.bind("match"));
174+
auto MatchedDirectCallExpr =
175+
expr(callExpr(callee(functionDecl(
176+
// Don't match copy or move assignment operator.
177+
unless(isAssignmentOverloadedOperator()),
178+
// Don't match void overloads of checked functions.
179+
unless(returns(voidType())),
180+
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
181+
CheckedFunctions)),
182+
returns(hasCanonicalType(hasDeclaration(
183+
namedDecl(matchers::matchesAnyListedName(
184+
CheckedReturnTypes)))))))))
185+
.bind("match"));
183186

184187
auto CheckCastToVoid =
185188
AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,53 @@
33
// RUN: {bugprone-unused-return-value.CheckedFunctions: "::*"}}' \
44
// RUN: --
55

6-
struct S {
7-
S(){};
8-
S(S const &);
9-
S(S &&);
10-
S &operator=(S const &);
11-
S &operator=(S &&);
12-
S &operator+=(S);
6+
struct S1 {
7+
S1(){};
8+
S1(S1 const &);
9+
S1(S1 &&);
10+
S1 &operator=(S1 const &);
11+
S1 &operator=(S1 &&);
12+
S1 &operator+=(S1);
13+
S1 &operator++();
14+
S1 &operator++(int);
15+
S1 &operator--();
16+
S1 &operator--(int);
1317
};
1418

15-
S returnValue();
16-
S const &returnRef();
19+
struct S2 {
20+
S2(){};
21+
S2(S2 const &);
22+
S2(S2 &&);
23+
};
24+
25+
S2 &operator-=(S2&, int);
26+
S2 &operator++(S2 &);
27+
S2 &operator++(S2 &, int);
28+
29+
S1 returnValue();
30+
S1 const &returnRef();
1731

1832
void bar() {
1933
returnValue();
2034
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this function should not be disregarded; neglecting it may lead to errors
2135

22-
S a{};
36+
S1 a{};
2337
a = returnValue();
2438
a.operator=(returnValue());
2539

2640
a = returnRef();
2741
a.operator=(returnRef());
2842

2943
a += returnRef();
44+
45+
a++;
46+
++a;
47+
a--;
48+
--a;
49+
50+
S2 b{};
51+
52+
b -= 1;
53+
b++;
54+
++b;
3055
}

0 commit comments

Comments
 (0)