From cc9e869960a6ababab334ac3aab8c1f9c768a070 Mon Sep 17 00:00:00 2001 From: xiaoye <50870160+xiaoyewww@users.noreply.github.com> Date: Tue, 29 Aug 2023 10:41:00 +0800 Subject: [PATCH] [clang-tidy] No. 53,54 enable cppcoreguidelines-c-copy-assignment-signature and bugprone-use-after-move (#56601) --- .clang-tidy | 4 ++-- paddle/fluid/distributed/rpc/rpc_agent.cc | 2 +- .../framework/new_executor/program_interpreter.cc | 2 +- .../stream_safe_custom_device_allocator.cc | 2 +- paddle/fluid/operators/math/beam_search.cc | 12 ++++++++---- paddle/phi/core/sparse_coo_tensor.cc | 14 +++++++++----- paddle/phi/core/sparse_coo_tensor.h | 2 +- .../cpu/weighted_sample_neighbors_kernel.cc | 14 ++++++++++---- .../cpp/fluid/reader/reader_blocking_queue_test.cc | 10 ++++++++-- test/cpp/phi/core/test_string_tensor.cc | 2 +- 10 files changed, 42 insertions(+), 22 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index fe4835d05cd..78f7ceaf0a0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -43,7 +43,7 @@ bugprone-misplaced-widening-cast, bugprone-unhandled-self-assignment, bugprone-unused-raii, -bugprone-unused-return-value, --bugprone-use-after-move, +bugprone-use-after-move, -bugprone-virtual-near-miss, -clang-analyzer-apiModeling.StdCLibraryFunctions, -clang-analyzer-apiModeling.TrustNonnull, @@ -153,7 +153,7 @@ clang-analyzer-unix.Vfork, -clang-analyzer-valist.ValistBase, cppcoreguidelines-avoid-c-arrays, -cppcoreguidelines-avoid-goto, --cppcoreguidelines-c-copy-assignment-signature, +cppcoreguidelines-c-copy-assignment-signature, -cppcoreguidelines-explicit-virtual-functions, -cppcoreguidelines-init-variables, -cppcoreguidelines-narrowing-conversions, diff --git a/paddle/fluid/distributed/rpc/rpc_agent.cc b/paddle/fluid/distributed/rpc/rpc_agent.cc index 18fa2aba841..428ed15fa9e 100644 --- a/paddle/fluid/distributed/rpc/rpc_agent.cc +++ b/paddle/fluid/distributed/rpc/rpc_agent.cc @@ -43,7 +43,7 @@ RpcAgent::RpcAgent(std::string name, std::vector infos) { PADDLE_ENFORCE_EQ( server_.AddService(rpc_service_.get(), brpc::SERVER_DOESNT_OWN_SERVICE), 0, - platform::errors::Fatal("Fail to add service: %s", name)); + platform::errors::Fatal("Fail to add service: %s", name_)); } int RpcAgent::StartWorker() { diff --git a/paddle/fluid/framework/new_executor/program_interpreter.cc b/paddle/fluid/framework/new_executor/program_interpreter.cc index c31e0355f8d..c94a326f698 100644 --- a/paddle/fluid/framework/new_executor/program_interpreter.cc +++ b/paddle/fluid/framework/new_executor/program_interpreter.cc @@ -614,7 +614,6 @@ void ProgramInterpreter::Convert( for (size_t op_idx = 0; op_idx < op_nums; ++op_idx) { auto& op_func_node = nodes[op_idx]; auto* dev_ctx_ = stream_analyzer_.ParseDeviceContext(op_func_node); - vec_instruction_.emplace_back(op_idx, std::move(op_func_node), *dev_ctx_); #ifdef PADDLE_WITH_CUDA if (FLAGS_new_executor_use_cuda_graph) { auto& op = op_func_node.operator_base_; @@ -637,6 +636,7 @@ void ProgramInterpreter::Convert( .RecordCapturingDeviceContext(dev_ctx_); } #endif + vec_instruction_.emplace_back(op_idx, std::move(op_func_node), *dev_ctx_); } BuildOperatorDependences(); diff --git a/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc b/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc index 9cf680e3078..b88f40f6a27 100644 --- a/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc +++ b/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc @@ -121,7 +121,7 @@ StreamSafeCustomDeviceAllocator::StreamSafeCustomDeviceAllocator( place_(std::move(place)), default_stream_(std::move(default_stream)) { std::lock_guard lock_guard(allocator_map_lock_); - allocator_map_[place].emplace_back(this); + allocator_map_[place_].emplace_back(this); } StreamSafeCustomDeviceAllocator::~StreamSafeCustomDeviceAllocator() { diff --git a/paddle/fluid/operators/math/beam_search.cc b/paddle/fluid/operators/math/beam_search.cc index 1e3f4f390f6..be8734076da 100644 --- a/paddle/fluid/operators/math/beam_search.cc +++ b/paddle/fluid/operators/math/beam_search.cc @@ -130,10 +130,14 @@ class BeamSearchFunctor { ((score == in.score) && (offset < in.offset)); } - inline void operator=(const Item &in) { - offset = in.offset; - id = in.id; - score = in.score; + inline Item &operator=(const Item &in) { + if (this != &in) { + this->offset = in.offset; + this->id = in.id; + this->score = in.score; + return *this; + } + return *this; } std::string ToString() { diff --git a/paddle/phi/core/sparse_coo_tensor.cc b/paddle/phi/core/sparse_coo_tensor.cc index 42360a1d21b..4d34c8f1d73 100644 --- a/paddle/phi/core/sparse_coo_tensor.cc +++ b/paddle/phi/core/sparse_coo_tensor.cc @@ -57,11 +57,15 @@ SparseCooTensor::SparseCooTensor(const SparseCooTensor& other) { set_meta(other.meta()); } -SparseCooTensor SparseCooTensor::operator=(const SparseCooTensor& other) { - this->non_zero_elements_ = other.non_zero_elements_; - this->non_zero_indices_ = other.non_zero_indices_; - this->coalesced_ = other.coalesced_; - set_meta(other.meta()); +SparseCooTensor& SparseCooTensor::operator=(const SparseCooTensor& other) { + if (this != &other) { + this->non_zero_elements_ = other.non_zero_elements_; + this->non_zero_indices_ = other.non_zero_indices_; + this->coalesced_ = other.coalesced_; + set_meta(other.meta()); + return *this; + } + return *this; } diff --git a/paddle/phi/core/sparse_coo_tensor.h b/paddle/phi/core/sparse_coo_tensor.h index 97db6cb8c26..48b001cbf16 100644 --- a/paddle/phi/core/sparse_coo_tensor.h +++ b/paddle/phi/core/sparse_coo_tensor.h @@ -58,7 +58,7 @@ class SparseCooTensor : public TensorBase, SparseCooTensor(SparseCooTensor&& other); /// \brief SparseCooTensor shallow copy assignment. - SparseCooTensor operator=(const SparseCooTensor& other); + SparseCooTensor& operator=(const SparseCooTensor& other); /// \brief Destroy the tensor object and release exclusive resources. virtual ~SparseCooTensor() = default; diff --git a/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc b/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc index cc2b6cdbdf2..713aad7332d 100644 --- a/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc +++ b/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc @@ -35,10 +35,16 @@ struct GraphWeightedNode { } GraphWeightedNode(T node_id, float weight_key, T eid = 0) : node_id(node_id), weight_key(weight_key), eid(eid) {} - void operator=(const GraphWeightedNode& other) { - node_id = other.node_id; - weight_key = other.weight_key; - eid = other.eid; + + GraphWeightedNode& operator=(const GraphWeightedNode& other) { + if (this != &other) { + this->node_id = other.node_id; + this->weight_key = other.weight_key; + this->eid = other.eid; + return *this; + } + + return *this; } friend bool operator>(const GraphWeightedNode& n1, const GraphWeightedNode& n2) { diff --git a/test/cpp/fluid/reader/reader_blocking_queue_test.cc b/test/cpp/fluid/reader/reader_blocking_queue_test.cc index 75e5d127771..7db47f07618 100644 --- a/test/cpp/fluid/reader/reader_blocking_queue_test.cc +++ b/test/cpp/fluid/reader/reader_blocking_queue_test.cc @@ -204,7 +204,13 @@ struct MyClass { explicit MyClass(int val) : val_(val) {} MyClass(const MyClass& b) { val_ = b.val_; } MyClass(MyClass&& b) { val_ = b.val_; } - void operator=(const MyClass& b) { val_ = b.val_; } + MyClass& operator=(const MyClass& b) { + if (this != &b) { + val_ = b.val_; + return *this; + } + return *this; + } int val_; }; @@ -212,7 +218,7 @@ struct MyClass { TEST(BlockingQueue, MyClassTest) { BlockingQueue q(2); MyClass a(200); - q.Send(std::move(a)); + q.Send(a); MyClass b; q.Receive(&b); EXPECT_EQ(a.val_, b.val_); diff --git a/test/cpp/phi/core/test_string_tensor.cc b/test/cpp/phi/core/test_string_tensor.cc index f775dd737ec..fb943d79212 100644 --- a/test/cpp/phi/core/test_string_tensor.cc +++ b/test/cpp/phi/core/test_string_tensor.cc @@ -94,7 +94,7 @@ TEST(pstring, func) { CHECK_EQ(copy_nchar_str, "AAAAA"); // Test Move Ctor - pstring move_nchar_str(std::move(nchar_str)); + pstring move_nchar_str(nchar_str); CHECK_EQ(move_nchar_str, "AAAAA"); pstring std_str(std::string("BBBB")); CHECK_EQ(std_str, "BBBB"); -- GitLab