[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 12 07:34:47 PDT 2024
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/84922
Fixes: #84705
>From d760b280a79be973642a0549db0e5a8838c397fd Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 12 Mar 2024 22:33:18 +0800
Subject: [PATCH] [clang-tidy]bugprone-unused-return-value ignore `++` and `--`
operator overloading
Fixes: #84705
---
.../bugprone/UnusedReturnValueCheck.cpp | 28 +++++++++----------
.../unused-return-value-avoid-assignment.cpp | 8 ++++++
2 files changed, 22 insertions(+), 14 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 243fe47c2036b6..83b332fba1e2da 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -165,20 +165,20 @@ void UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
auto MatchedDirectCallExpr = expr(
- callExpr(
- callee(functionDecl(
- // Don't match void overloads of checked functions.
- unless(returns(voidType())),
- // Don't match copy or move assignment operator.
- unless(cxxMethodDecl(isOperatorOverloading(
- {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
- OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
- OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
- anyOf(
- isInstantiatedFrom(
- matchers::matchesAnyListedName(CheckedFunctions)),
- returns(hasCanonicalType(hasDeclaration(namedDecl(
- matchers::matchesAnyListedName(CheckedReturnTypes)))))))))
+ callExpr(callee(functionDecl(
+ // Don't match void overloads of checked functions.
+ unless(returns(voidType())),
+ // Don't match copy or move assignment operator.
+ unless(cxxMethodDecl(isOperatorOverloading(
+ {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
+ OO_SlashEqual, OO_PercentEqual, OO_CaretEqual,
+ OO_AmpEqual, OO_PipeEqual, OO_LessLessEqual,
+ OO_GreaterGreaterEqual, OO_PlusPlus, OO_MinusMinus}))),
+ anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)))))))))
.bind("match"));
auto CheckCastToVoid =
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
index b4a41004adf894..5809da9ec27b38 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -10,6 +10,10 @@ struct S {
S &operator=(S const &);
S &operator=(S &&);
S &operator+=(S);
+ S &operator++();
+ S &operator++(int);
+ S &operator--();
+ S &operator--(int);
};
S returnValue();
@@ -27,4 +31,8 @@ void bar() {
a.operator=(returnRef());
a += returnRef();
+ a++;
+ ++a;
+ a--;
+ --a;
}
More information about the cfe-commits
mailing list