diff --git a/aten/src/ATen/MapAllocator.h b/aten/src/ATen/MapAllocator.h index db1258beee52..51fb4674edd0 100644 --- a/aten/src/ATen/MapAllocator.h +++ b/aten/src/ATen/MapAllocator.h @@ -112,6 +112,10 @@ class TORCH_API RefcountedMapAllocator : private RefcountedMapAllocatorArgCheck, size_t size); static RefcountedMapAllocator* fromDataPtr(const at::DataPtr&); + RefcountedMapAllocator(const RefcountedMapAllocator&) = delete; + RefcountedMapAllocator(RefcountedMapAllocator&&) = delete; + RefcountedMapAllocator& operator=(const RefcountedMapAllocator&) = delete; + RefcountedMapAllocator& operator=(RefcountedMapAllocator&&) = delete; static at::DataPtr makeDataPtr( const char* filename, int flags, diff --git a/c10/core/Allocator.cpp b/c10/core/Allocator.cpp index 491c85b081e8..ca02350ce7bf 100644 --- a/c10/core/Allocator.cpp +++ b/c10/core/Allocator.cpp @@ -87,8 +87,6 @@ void reportOutOfMemoryToProfiler( } } -MemoryReportingInfoBase::MemoryReportingInfoBase() = default; - void MemoryReportingInfoBase::reportOutOfMemory( int64_t /*alloc_size*/, size_t /*total_allocated*/, diff --git a/c10/core/Allocator.h b/c10/core/Allocator.h index ca196cda3641..bdb8c719fbc5 100644 --- a/c10/core/Allocator.h +++ b/c10/core/Allocator.h @@ -157,6 +157,7 @@ inline bool operator!=(std::nullptr_t, const DataPtr& dp) noexcept { // possible, or the raw interface will incorrectly reported as unsupported, // when it is actually possible. +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) struct C10_API Allocator { virtual ~Allocator() = default; @@ -223,10 +224,24 @@ struct C10_API Allocator { // allocation InefficientStdFunctionContext, on top of the dynamic // allocation which is implied by std::function itself. struct C10_API InefficientStdFunctionContext { - void* ptr_; + void* ptr_{nullptr}; std::function deleter_; InefficientStdFunctionContext(void* ptr, std::function deleter) : ptr_(ptr), deleter_(std::move(deleter)) {} + InefficientStdFunctionContext(const InefficientStdFunctionContext&) = delete; + InefficientStdFunctionContext(InefficientStdFunctionContext&& rhs) noexcept + : ptr_(std::exchange(rhs.ptr_, nullptr)), + deleter_(std::move(rhs.deleter_)) {} + InefficientStdFunctionContext& operator=( + const InefficientStdFunctionContext&) = delete; + // NOLINTNEXTLINE(performance-noexcept-move-constructor) + InefficientStdFunctionContext& operator=( + InefficientStdFunctionContext&& rhs) { + this->~InefficientStdFunctionContext(); + ptr_ = std::exchange(rhs.ptr_, nullptr); + deleter_ = std::move(rhs.deleter_); + return *this; + } ~InefficientStdFunctionContext() { if (deleter_) { deleter_(ptr_); @@ -270,9 +285,6 @@ struct AllocatorRegisterer { // An interface for reporting thread local memory usage // per device struct C10_API MemoryReportingInfoBase : public c10::DebugInfoBase { - MemoryReportingInfoBase(); - ~MemoryReportingInfoBase() override = default; - /** * alloc_size corresponds to the size of the ptr. * @@ -312,6 +324,7 @@ C10_API void reportOutOfMemoryToProfiler( Device device); // used to hold traceback information in allocators +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) struct GatheredContext { virtual ~GatheredContext() = default; }; diff --git a/c10/core/CPUAllocator.cpp b/c10/core/CPUAllocator.cpp index 98753babcf4e..cac00cd7b27d 100644 --- a/c10/core/CPUAllocator.cpp +++ b/c10/core/CPUAllocator.cpp @@ -75,9 +75,6 @@ ProfiledCPUMemoryReporter& profiledCPUMemoryReporter() { template class DefaultMobileCPUAllocator final : public at::Allocator { public: - DefaultMobileCPUAllocator() = default; - ~DefaultMobileCPUAllocator() override = default; - static void deleter(void* const pointer) { if (C10_UNLIKELY(!pointer)) { return; diff --git a/c10/core/DeviceGuard.h b/c10/core/DeviceGuard.h index 94b89bc31b72..7fa366049480 100644 --- a/c10/core/DeviceGuard.h +++ b/c10/core/DeviceGuard.h @@ -34,6 +34,8 @@ class DeviceGuard { const impl::DeviceGuardImplInterface* impl) : guard_(device, impl) {} + ~DeviceGuard() = default; + /// Copy is disallowed DeviceGuard(const DeviceGuard&) = delete; DeviceGuard& operator=(const DeviceGuard&) = delete; @@ -143,6 +145,7 @@ class OptionalDeviceGuard { const impl::DeviceGuardImplInterface* impl) : guard_(device, impl) {} + ~OptionalDeviceGuard() = default; /// Copy is disallowed OptionalDeviceGuard(const OptionalDeviceGuard&) = delete; OptionalDeviceGuard& operator=(const OptionalDeviceGuard&) = delete; diff --git a/c10/core/GeneratorImpl.h b/c10/core/GeneratorImpl.h index 6757b6de6f65..3b0b78ef4601 100644 --- a/c10/core/GeneratorImpl.h +++ b/c10/core/GeneratorImpl.h @@ -61,6 +61,7 @@ struct C10_API GeneratorImpl : public c10::intrusive_ptr_target { GeneratorImpl(const GeneratorImpl& other) = delete; GeneratorImpl(GeneratorImpl&& other) = delete; GeneratorImpl& operator=(const GeneratorImpl& other) = delete; + GeneratorImpl& operator=(GeneratorImpl&& other) = delete; ~GeneratorImpl() override = default; c10::intrusive_ptr clone() const; diff --git a/c10/core/GradMode.h b/c10/core/GradMode.h index d60add2cd2b0..a8f6329cf83b 100644 --- a/c10/core/GradMode.h +++ b/c10/core/GradMode.h @@ -16,6 +16,10 @@ struct C10_API AutoGradMode { AutoGradMode(bool enabled) : prev_mode(GradMode::is_enabled()) { GradMode::set_enabled(enabled); } + AutoGradMode(const AutoGradMode&) = delete; + AutoGradMode(AutoGradMode&&) = delete; + AutoGradMode& operator=(const AutoGradMode&) = delete; + AutoGradMode& operator=(AutoGradMode&&) = delete; ~AutoGradMode() { GradMode::set_enabled(prev_mode); } @@ -35,6 +39,10 @@ struct C10_API AutoFwGradMode { : prev_mode(AutogradState::get_tls_state().get_fw_grad_mode()) { AutogradState::get_tls_state().set_fw_grad_mode(enabled); } + AutoFwGradMode(const AutoFwGradMode&) = delete; + AutoFwGradMode(AutoFwGradMode&&) = delete; + AutoFwGradMode& operator=(const AutoFwGradMode&) = delete; + AutoFwGradMode& operator=(AutoFwGradMode&&) = delete; ~AutoFwGradMode() { AutogradState::get_tls_state().set_fw_grad_mode(prev_mode); } diff --git a/c10/core/InferenceMode.h b/c10/core/InferenceMode.h index 52541886c0ae..a9cf2f0bf32e 100644 --- a/c10/core/InferenceMode.h +++ b/c10/core/InferenceMode.h @@ -73,6 +73,11 @@ struct C10_API InferenceMode { c10::impl::_force_tls_local_dispatch_key_set(cur_keyset); } + InferenceMode(const InferenceMode&) = delete; + InferenceMode(InferenceMode&&) = delete; + InferenceMode& operator=(const InferenceMode&) = delete; + InferenceMode& operator=(InferenceMode&&) = delete; + ~InferenceMode() { AutogradState::set_tls_state(prev_mode); c10::impl::_force_tls_local_dispatch_key_set(prev_keyset); diff --git a/c10/core/SafePyObject.h b/c10/core/SafePyObject.h index bd6022e8c14d..6102aed8c0ba 100644 --- a/c10/core/SafePyObject.h +++ b/c10/core/SafePyObject.h @@ -81,9 +81,11 @@ template struct SafePyObjectT : private SafePyObject { SafePyObjectT(PyObject* data, c10::impl::PyInterpreter* pyinterpreter) : SafePyObject(data, pyinterpreter) {} + ~SafePyObjectT() = default; SafePyObjectT(SafePyObjectT&& other) noexcept : SafePyObject(other) {} SafePyObjectT(SafePyObjectT const&) = delete; SafePyObjectT& operator=(SafePyObjectT const&) = delete; + SafePyObjectT& operator=(SafePyObjectT&&) = delete; using SafePyObject::ptr; using SafePyObject::pyinterpreter; diff --git a/c10/core/StreamGuard.h b/c10/core/StreamGuard.h index db6dbd88cbd9..d3057823a5cd 100644 --- a/c10/core/StreamGuard.h +++ b/c10/core/StreamGuard.h @@ -27,6 +27,7 @@ namespace c10 { struct StreamGuard { /// No default constructor, see Note [Omitted default constructor from RAII] explicit StreamGuard() = delete; + ~StreamGuard() = default; /// Set the current device to the device associated with the passed stream, /// and set the current stream on that device to the passed stream. @@ -111,6 +112,7 @@ struct OptionalStreamGuard { // See Note [Move assignment for RAII guards is tricky] OptionalStreamGuard& operator=(OptionalStreamGuard&& other) = delete; + ~OptionalStreamGuard() = default; /// Resets the currently set stream to the original stream and /// the currently set device to the original device. Then, @@ -162,6 +164,7 @@ struct MultiStreamGuard { // See Note [Move assignment for RAII guards is tricky] MultiStreamGuard& operator=(MultiStreamGuard&& other) = delete; + ~MultiStreamGuard() = default; private: c10::impl::InlineMultiStreamGuard guard_; diff --git a/c10/core/SymbolicShapeMeta.h b/c10/core/SymbolicShapeMeta.h index 935f6481d02f..ce0769a8074f 100644 --- a/c10/core/SymbolicShapeMeta.h +++ b/c10/core/SymbolicShapeMeta.h @@ -22,7 +22,9 @@ class C10_API SymbolicShapeMeta { bool strides_valid_ = true; // e.g. for sparse where there are no strides SymbolicShapeMeta() = default; + ~SymbolicShapeMeta() = default; SymbolicShapeMeta(const SymbolicShapeMeta& other); + SymbolicShapeMeta(SymbolicShapeMeta&& other) = delete; SymbolicShapeMeta& operator=(const SymbolicShapeMeta& other) = delete; SymbolicShapeMeta& operator=(SymbolicShapeMeta&& other) = delete; diff --git a/c10/core/TensorImpl.h b/c10/core/TensorImpl.h index a8d05dddcfa2..888881ac2d74 100644 --- a/c10/core/TensorImpl.h +++ b/c10/core/TensorImpl.h @@ -133,6 +133,7 @@ struct C10_API PlacementDeleteContext { DataPtr data_ptr_; PlacementDtor placement_dtor_; size_t size_; + PlacementDeleteContext( DataPtr&& data_ptr, PlacementDtor placement_dtor, @@ -140,6 +141,11 @@ struct C10_API PlacementDeleteContext { : data_ptr_(std::move(data_ptr)), placement_dtor_(placement_dtor), size_(size) {} + + PlacementDeleteContext(PlacementDeleteContext&&) noexcept = delete; + PlacementDeleteContext(const PlacementDeleteContext&) = delete; + PlacementDeleteContext& operator=(const PlacementDeleteContext&) = delete; + PlacementDeleteContext& operator=(PlacementDeleteContext&&) = delete; static DataPtr makeDataPtr( DataPtr&& data_ptr, PlacementDtor placement_dtor, @@ -237,6 +243,7 @@ struct C10_API ExtraMeta { std::optional custom_storage_error_msg_ = std::nullopt; ExtraMeta() = default; + ~ExtraMeta() = default; ExtraMeta(const ExtraMeta& other) { if (other.symbolic_shape_meta_) { symbolic_shape_meta_ = diff --git a/c10/core/impl/InlineDeviceGuard.h b/c10/core/impl/InlineDeviceGuard.h index e0c6d4f1ca8f..a80ac550906a 100644 --- a/c10/core/impl/InlineDeviceGuard.h +++ b/c10/core/impl/InlineDeviceGuard.h @@ -62,7 +62,7 @@ class InlineDeviceGuard { // DeviceGuard which reads the current device and promises to // restore to that device on exit. However, most cases where you // would have written this, you probably meant to actually just - // use OptionalDeviceGuard (since you don't actually need the + // use DeviceGuard (since you don't actually need the // restore to happen if you don't ever actually set the device). // We remove the constructor here to encourage you to think about // what you actually want to happen. @@ -221,6 +221,7 @@ class InlineOptionalDeviceGuard { explicit InlineOptionalDeviceGuard() : guard_() // See Note [Explicit initialization of optional fields] {} + ~InlineOptionalDeviceGuard() = default; /// Set the current device to the passed Device, if it is not nullopt. explicit InlineOptionalDeviceGuard(std::optional device_opt) @@ -286,6 +287,7 @@ class InlineOptionalDeviceGuard { // It's in principle possible to raise an error when this occurs // by doing some extra thread-local bookkeeping. But why bother? // Just don't provide the constructor. + InlineOptionalDeviceGuard(const InlineOptionalDeviceGuard& other) = delete; InlineOptionalDeviceGuard(InlineOptionalDeviceGuard&& other) = delete; // Note [Move assignment for RAII guards is tricky] @@ -335,6 +337,8 @@ class InlineOptionalDeviceGuard { // // We could solve this with an extra thread-local variable. But no one is // actually using move-assignment. So just get rid of it. + InlineOptionalDeviceGuard& operator=(const InlineOptionalDeviceGuard& other) = + delete; InlineOptionalDeviceGuard& operator=(InlineOptionalDeviceGuard&& other) = delete; diff --git a/c10/core/impl/InlineStreamGuard.h b/c10/core/impl/InlineStreamGuard.h index 6d2b3c70678e..51c25e25ffa6 100644 --- a/c10/core/impl/InlineStreamGuard.h +++ b/c10/core/impl/InlineStreamGuard.h @@ -135,6 +135,7 @@ class InlineOptionalStreamGuard { explicit InlineOptionalStreamGuard() : guard_() // See Note [Explicit initialization of optional fields] {} + ~InlineOptionalStreamGuard() = default; /// Set the current device to the device associated with the passed stream, /// and set the current stream on that device to the passed stream, @@ -151,6 +152,9 @@ class InlineOptionalStreamGuard { explicit InlineOptionalStreamGuard(Args&&... args) : guard_(std::in_place, std::forward(args)...) {} + InlineOptionalStreamGuard(const InlineOptionalStreamGuard& other) = delete; + InlineOptionalStreamGuard& operator=(const InlineOptionalStreamGuard& other) = + delete; // See Note [Move construction for RAII guards is tricky] InlineOptionalStreamGuard(InlineOptionalStreamGuard&& other) = delete; diff --git a/c10/core/impl/LocalDispatchKeySet.h b/c10/core/impl/LocalDispatchKeySet.h index 176d0a6b6421..1232bd25eb3b 100644 --- a/c10/core/impl/LocalDispatchKeySet.h +++ b/c10/core/impl/LocalDispatchKeySet.h @@ -132,6 +132,11 @@ struct C10_API ForceDispatchKeyGuard { updated_set.excluded_ = exclude; c10::impl::_force_tls_local_dispatch_key_set(updated_set); } + + ForceDispatchKeyGuard(ForceDispatchKeyGuard&&) noexcept = delete; + ForceDispatchKeyGuard(const ForceDispatchKeyGuard&) = delete; + ForceDispatchKeyGuard& operator=(const ForceDispatchKeyGuard&) = delete; + ForceDispatchKeyGuard& operator=(ForceDispatchKeyGuard&&) = delete; ~ForceDispatchKeyGuard() { c10::impl::_force_tls_local_dispatch_key_set(saved_keyset_); } diff --git a/c10/core/impl/PythonDispatcherTLS.h b/c10/core/impl/PythonDispatcherTLS.h index 9016c3e11e15..12c0677f36fd 100644 --- a/c10/core/impl/PythonDispatcherTLS.h +++ b/c10/core/impl/PythonDispatcherTLS.h @@ -15,6 +15,7 @@ struct C10_API DisablePythonDispatcher { DisablePythonDispatcher() : old_(PythonDispatcherTLS::get_state()) { PythonDispatcherTLS::set_state({}); } + ~DisablePythonDispatcher() { PythonDispatcherTLS::set_state(old_); } diff --git a/c10/cuda/CUDAGuard.h b/c10/cuda/CUDAGuard.h index 08b7bb711373..4bde4ecc6507 100644 --- a/c10/cuda/CUDAGuard.h +++ b/c10/cuda/CUDAGuard.h @@ -147,6 +147,7 @@ struct CUDAStreamGuard { /// stream, and set the current CUDA stream on that device to the passed /// stream. Errors if the Stream is not a CUDA stream. explicit CUDAStreamGuard(Stream stream) : guard_(stream) {} + ~CUDAStreamGuard() = default; /// Copy is disallowed CUDAStreamGuard(const CUDAStreamGuard&) = delete; diff --git a/c10/test/util/small_vector_test.cpp b/c10/test/util/small_vector_test.cpp index ddc82a76b1c8..df8c7fb6a656 100644 --- a/c10/test/util/small_vector_test.cpp +++ b/c10/test/util/small_vector_test.cpp @@ -139,6 +139,7 @@ int Constructable::numMoveAssignmentCalls; struct NonCopyable { NonCopyable() = default; + ~NonCopyable() = default; NonCopyable(NonCopyable&&) noexcept = default; NonCopyable& operator=(NonCopyable&&) noexcept = default; @@ -883,9 +884,12 @@ TEST(SmallVectorCustomTest, NoAssignTest) { struct MovedFrom { bool hasValue{true}; MovedFrom() = default; + ~MovedFrom() = default; + MovedFrom(const MovedFrom& m) = delete; MovedFrom(MovedFrom&& m) noexcept : hasValue(m.hasValue) { m.hasValue = false; } + MovedFrom& operator=(const MovedFrom& m) = delete; MovedFrom& operator=(MovedFrom&& m) noexcept { hasValue = m.hasValue; m.hasValue = false; @@ -917,6 +921,7 @@ struct EmplaceableArg { EmplaceableArg(EmplaceableArg& X) : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {} + ~EmplaceableArg() = default; explicit EmplaceableArg(bool) : State(EAS_Arg) {} EmplaceableArg& operator=(EmplaceableArg&&) = delete; @@ -932,6 +937,7 @@ struct Emplaceable { EmplaceableState State; Emplaceable() : State(ES_Emplaced) {} + ~Emplaceable() = default; template explicit Emplaceable(A0Ty&& A0) diff --git a/c10/util/string_view.h b/c10/util/string_view.h index 0a25e7dcd4a5..083b4ef5449e 100644 --- a/c10/util/string_view.h +++ b/c10/util/string_view.h @@ -26,6 +26,7 @@ namespace c10 { * std::char_traits if we wanted to use it with our constexpr basic_string_view. */ template +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class basic_string_view final { public: using value_type = CharT; diff --git a/c10/util/typeid.h b/c10/util/typeid.h index 2c6ac38882f5..ebf44b327168 100644 --- a/c10/util/typeid.h +++ b/c10/util/typeid.h @@ -328,6 +328,7 @@ class C10_API TypeMeta final { * type, use TypeMeta::Make(). */ TypeMeta() noexcept; + ~TypeMeta() = default; /** * Copy constructor. @@ -339,6 +340,7 @@ class C10_API TypeMeta final { */ TypeMeta& operator=(const TypeMeta& src) noexcept = default; + TypeMeta& operator=(TypeMeta&& src) noexcept = default; TypeMeta(TypeMeta&& rhs) noexcept = default; inline TypeMeta& operator=(ScalarType scalar_type) noexcept { diff --git a/torch/csrc/autograd/saved_variable.h b/torch/csrc/autograd/saved_variable.h index 2866b5671560..0d28c95e19a2 100644 --- a/torch/csrc/autograd/saved_variable.h +++ b/torch/csrc/autograd/saved_variable.h @@ -29,7 +29,9 @@ class TORCH_API SavedVariable { const std::optional& variable, bool is_output, bool is_inplace_on_view = false); + SavedVariable(const SavedVariable&) = delete; SavedVariable(SavedVariable&&) = default; + SavedVariable& operator=(const SavedVariable&) = delete; SavedVariable& operator=(SavedVariable&&) = default; ~SavedVariable() { if (fw_grad_) { diff --git a/torch/csrc/utils/python_torch_function_mode.h b/torch/csrc/utils/python_torch_function_mode.h index f0e6bb9acbe9..56d632937873 100644 --- a/torch/csrc/utils/python_torch_function_mode.h +++ b/torch/csrc/utils/python_torch_function_mode.h @@ -11,6 +11,12 @@ struct StashTorchFunctionModeGuard { ~StashTorchFunctionModeGuard() { at::impl::PythonTorchFunctionTLS::push_onto_stack(cur_mode_); } + StashTorchFunctionModeGuard(const StashTorchFunctionModeGuard&) = delete; + StashTorchFunctionModeGuard(StashTorchFunctionModeGuard&&) = delete; + StashTorchFunctionModeGuard& operator=(const StashTorchFunctionModeGuard&) = + delete; + StashTorchFunctionModeGuard& operator=(StashTorchFunctionModeGuard&&) = + delete; const std::shared_ptr& get_cur_mode() { return cur_mode_; diff --git a/torch/library.h b/torch/library.h index bfda4955eadd..2761573e2ccc 100644 --- a/torch/library.h +++ b/torch/library.h @@ -206,6 +206,9 @@ class TORCH_API CppFunction final { ~CppFunction(); + CppFunction(const CppFunction&) = delete; + CppFunction& operator=(const CppFunction&) = delete; + CppFunction(CppFunction&&) noexcept = default; CppFunction& operator=(CppFunction&&) = default; @@ -563,6 +566,7 @@ class TORCH_API Library final { Library& operator=(const Library&) = delete; Library(Library&&) = default; Library& operator=(Library&&) = default; + ~Library() = default; // Some notes about the API design here. We had the following constraints: //