diff --git a/aten/src/ATen/core/Dict.h b/aten/src/ATen/core/Dict.h index a9befba8276..b1f4ebe62e7 100644 --- a/aten/src/ATen/core/Dict.h +++ b/aten/src/ATen/core/Dict.h @@ -314,7 +314,7 @@ public: * * @return The number of elements removed. This is either '1' if an element with the key existed, or '0' if it didn't. */ - C10_NODISCARD size_t erase(const Key& key) const; + [[nodiscard]] size_t erase(const Key& key) const; /** * Returns the mapped value of the element with key equivalent to key. diff --git a/aten/src/ATen/core/Dict_inl.h b/aten/src/ATen/core/Dict_inl.h index 0419b3bd49e..c48d7ec38ae 100644 --- a/aten/src/ATen/core/Dict_inl.h +++ b/aten/src/ATen/core/Dict_inl.h @@ -142,8 +142,8 @@ void Dict::erase(iterator iter) const { impl_->dict.erase(iter.entryRef_.iterator_); } -template -C10_NODISCARD size_t Dict::erase(const Key& key) const { +template +[[nodiscard]] size_t Dict::erase(const Key& key) const { return impl_->dict.erase(key); } diff --git a/aten/src/ATen/core/function_schema.h b/aten/src/ATen/core/function_schema.h index 8dab896b141..081e38e49b8 100644 --- a/aten/src/ATen/core/function_schema.h +++ b/aten/src/ATen/core/function_schema.h @@ -108,7 +108,7 @@ struct TORCH_API Argument { return is_out_; } - C10_NODISCARD const AliasInfo* alias_info() const { + [[nodiscard]] const AliasInfo* alias_info() const { return alias_info_.get(); } diff --git a/aten/src/ATen/core/ivalue.h b/aten/src/ATen/core/ivalue.h index 98cb2baae1f..29659124184 100644 --- a/aten/src/ATen/core/ivalue.h +++ b/aten/src/ATen/core/ivalue.h @@ -522,7 +522,7 @@ struct TORCH_API IValue final { } c10::intrusive_ptr toTuple() &&; c10::intrusive_ptr toTuple() const&; - C10_NODISCARD ivalue::Tuple& toTupleRef() const; + [[nodiscard]] ivalue::Tuple& toTupleRef() const; // Double IValue(double d) : tag(Tag::Double) { diff --git a/aten/src/ATen/core/ivalue_inl.h b/aten/src/ATen/core/ivalue_inl.h index 2d30d3ba5ca..63b58d80444 100644 --- a/aten/src/ATen/core/ivalue_inl.h +++ b/aten/src/ATen/core/ivalue_inl.h @@ -500,7 +500,7 @@ struct TORCH_API TupleElements { return *this; } - C10_NODISCARD c10::ArrayRef asArrayRef() const { + [[nodiscard]] c10::ArrayRef asArrayRef() const { if (inlineSize_) { return c10::ArrayRef(elementsInline_, inlineSize_); } else { @@ -527,15 +527,15 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD bool empty() const { + [[nodiscard]] bool empty() const { return inlineSize_ ? false : elementsVector_.empty(); } - C10_NODISCARD size_t size() const { + [[nodiscard]] size_t size() const { return inlineSize_ ? inlineSize_ : elementsVector_.size(); } - C10_NODISCARD IValue& operator[](size_t idx) { + [[nodiscard]] IValue& operator[](size_t idx) { if (inlineSize_) { return elementsInline_[idx]; } else { @@ -543,7 +543,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD const IValue& operator[](size_t idx) const { + [[nodiscard]] const IValue& operator[](size_t idx) const { if (inlineSize_) { return elementsInline_[idx]; } else { @@ -551,7 +551,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD IValue& at(size_t idx) { + [[nodiscard]] IValue& at(size_t idx) { if (inlineSize_) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY(inlineSize_ <= 3); TORCH_CHECK(idx < inlineSize_, "TupleElements: invalid index Index = ", idx, "; Length = ", inlineSize_); @@ -561,7 +561,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD const IValue& at(size_t idx) const { + [[nodiscard]] const IValue& at(size_t idx) const { if (inlineSize_) { TORCH_INTERNAL_ASSERT_DEBUG_ONLY(inlineSize_ <= 3); TORCH_CHECK(idx < inlineSize_, "TupleElements: invalid index Index = ", idx, "; Length = ", inlineSize_); @@ -572,7 +572,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD iterator begin() { + [[nodiscard]] iterator begin() { if (inlineSize_) { return elementsInline_; } else { @@ -580,7 +580,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD iterator end() { + [[nodiscard]] iterator end() { if (inlineSize_) { return elementsInline_ + inlineSize_; } else { @@ -588,7 +588,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD const_iterator begin() const { + [[nodiscard]] const_iterator begin() const { if (inlineSize_) { return elementsInline_; } else { @@ -596,7 +596,7 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD const_iterator end() const { + [[nodiscard]] const_iterator end() const { if (inlineSize_) { return elementsInline_ + inlineSize_; } else { @@ -604,27 +604,27 @@ struct TORCH_API TupleElements { } } - C10_NODISCARD const_iterator cbegin() const { + [[nodiscard]] const_iterator cbegin() const { return begin(); } - C10_NODISCARD const_iterator cend() const { + [[nodiscard]] const_iterator cend() const { return end(); } - C10_NODISCARD std::vector vec() const & { + [[nodiscard]] std::vector vec() const& { return asArrayRef().vec(); } - C10_NODISCARD IValue& back() { + [[nodiscard]] IValue& back() { return *(end() - 1); } - C10_NODISCARD const IValue& back() const { + [[nodiscard]] const IValue& back() const { return *(end() - 1); } - C10_NODISCARD std::vector vec() && { + [[nodiscard]] std::vector vec() && { std::vector result; result.reserve(size()); for (auto&& iv : *this) { diff --git a/c10/core/Allocator.h b/c10/core/Allocator.h index 412412557a0..ca196cda364 100644 --- a/c10/core/Allocator.h +++ b/c10/core/Allocator.h @@ -103,7 +103,7 @@ class C10_API DataPtr { * be; be sure to read the source code of the Allocator * in question to confirm this. */ - C10_NODISCARD bool compare_exchange_deleter( + [[nodiscard]] bool compare_exchange_deleter( DeleterFnPtr expected_deleter, DeleterFnPtr new_deleter) { return ptr_.compare_exchange_deleter(expected_deleter, new_deleter); diff --git a/c10/core/DispatchKeySet.h b/c10/core/DispatchKeySet.h index ca54e1966c5..289a88312c9 100644 --- a/c10/core/DispatchKeySet.h +++ b/c10/core/DispatchKeySet.h @@ -349,10 +349,10 @@ class DispatchKeySet final { } // Add a DispatchKey to the DispatchKey set. Does NOT mutate, // returns the extended DispatchKeySet! - C10_NODISCARD constexpr DispatchKeySet add(DispatchKey t) const { + [[nodiscard]] constexpr DispatchKeySet add(DispatchKey t) const { return *this | DispatchKeySet(t); } - C10_NODISCARD constexpr DispatchKeySet add(DispatchKeySet ks) const { + [[nodiscard]] constexpr DispatchKeySet add(DispatchKeySet ks) const { return *this | ks; } @@ -380,7 +380,7 @@ class DispatchKeySet final { // // Instead, remove(DispatchKey.AutogradCPU) will only remove the "Autograd" // bit from the bitset. - C10_NODISCARD constexpr DispatchKeySet remove(DispatchKey t) const { + [[nodiscard]] constexpr DispatchKeySet remove(DispatchKey t) const { return DispatchKeySet( repr_ & ~(DispatchKeySet(t).repr_ & ~full_backend_mask)); } diff --git a/c10/core/TensorOptions.h b/c10/core/TensorOptions.h index f98a93302e1..2e05d8265ed 100644 --- a/c10/core/TensorOptions.h +++ b/c10/core/TensorOptions.h @@ -192,8 +192,8 @@ struct C10_API TensorOptions { /// Return a copy of `TensorOptions` with `device` set to the given one, or /// cleared if `device` is `nullopt`. - C10_NODISCARD TensorOptions - device(std::optional device) const noexcept { + [[nodiscard]] TensorOptions device( + std::optional device) const noexcept { TensorOptions r = *this; r.set_device(device); return r; @@ -203,7 +203,7 @@ struct C10_API TensorOptions { /// (This overload ensures that variadic template std::optional constructor /// for Device work correctly.) template - C10_NODISCARD TensorOptions device(Args&&... args) const noexcept { + [[nodiscard]] TensorOptions device(Args&&... args) const noexcept { return device( std::optional(std::in_place, std::forward(args)...)); } @@ -213,22 +213,22 @@ struct C10_API TensorOptions { /// /// TODO: This function encourages bad behavior (assuming CUDA is /// the only device that matters). Get rid of it / rename it. - C10_NODISCARD TensorOptions - device_index(c10::DeviceIndex device_index) const noexcept { + [[nodiscard]] TensorOptions device_index( + c10::DeviceIndex device_index) const noexcept { return device(Device::Type::CUDA, device_index); } /// Return a copy of `TensorOptions` with `dtype` set to the given one. - C10_NODISCARD TensorOptions - dtype(std::optional dtype) const noexcept { + [[nodiscard]] TensorOptions dtype( + std::optional dtype) const noexcept { TensorOptions r = *this; r.set_dtype(dtype); return r; } // legacy function to support ScalarType - C10_NODISCARD TensorOptions - dtype(std::optional dtype) const noexcept { + [[nodiscard]] TensorOptions dtype( + std::optional dtype) const noexcept { TensorOptions r = *this; r.set_dtype(dtype); return r; @@ -243,32 +243,32 @@ struct C10_API TensorOptions { } /// Sets the layout of the `TensorOptions`. - C10_NODISCARD TensorOptions - layout(std::optional layout) const noexcept { + [[nodiscard]] TensorOptions layout( + std::optional layout) const noexcept { TensorOptions r = *this; r.set_layout(layout); return r; } /// Sets the `requires_grad` property of the `TensorOptions`. - C10_NODISCARD TensorOptions - requires_grad(std::optional requires_grad) const noexcept { + [[nodiscard]] TensorOptions requires_grad( + std::optional requires_grad) const noexcept { TensorOptions r = *this; r.set_requires_grad(requires_grad); return r; } /// Sets the `pinned_memory` property on the `TensorOptions`. - C10_NODISCARD TensorOptions - pinned_memory(std::optional pinned_memory) const noexcept { + [[nodiscard]] TensorOptions pinned_memory( + std::optional pinned_memory) const noexcept { TensorOptions r = *this; r.set_pinned_memory(pinned_memory); return r; } /// Sets the `memory_format` property on `TensorOptions`. - C10_NODISCARD TensorOptions - memory_format(std::optional memory_format) const noexcept { + [[nodiscard]] TensorOptions memory_format( + std::optional memory_format) const noexcept { TensorOptions r = *this; r.set_memory_format(memory_format); return r; diff --git a/c10/macros/Macros.h b/c10/macros/Macros.h index cf39e97412e..dc5ac274bcd 100644 --- a/c10/macros/Macros.h +++ b/c10/macros/Macros.h @@ -115,9 +115,6 @@ #define C10_HAS_CPP_ATTRIBUTE(x) (0) #endif -/// C10_NODISCARD - Warn if a type or return value is discarded. -#define C10_NODISCARD [[nodiscard]] - // suppress an unused variable. #define C10_UNUSED [[maybe_unused]] diff --git a/c10/util/SmallVector.h b/c10/util/SmallVector.h index cbcfbc52cb8..d45b8c8616f 100644 --- a/c10/util/SmallVector.h +++ b/c10/util/SmallVector.h @@ -81,7 +81,7 @@ class C10_API SmallVectorBase { return Capacity; } - C10_NODISCARD bool empty() const { + [[nodiscard]] bool empty() const { return !Size; } @@ -710,7 +710,7 @@ class SmallVectorImpl : public SmallVectorTemplateBase { this->set_size(this->size() - NumItems); } - C10_NODISCARD T pop_back_val() { + [[nodiscard]] T pop_back_val() { T Result = ::std::move(this->back()); this->pop_back(); return Result; diff --git a/c10/util/UniqueVoidPtr.h b/c10/util/UniqueVoidPtr.h index f82de8c7059..175697f7f63 100644 --- a/c10/util/UniqueVoidPtr.h +++ b/c10/util/UniqueVoidPtr.h @@ -69,7 +69,7 @@ class UniqueVoidPtr { std::unique_ptr&& move_context() { return std::move(ctx_); } - C10_NODISCARD bool compare_exchange_deleter( + [[nodiscard]] bool compare_exchange_deleter( DeleterFnPtr expected_deleter, DeleterFnPtr new_deleter) { if (get_deleter() != expected_deleter) diff --git a/c10/util/string_view.h b/c10/util/string_view.h index 136e3cd154e..0a25e7dcd4a 100644 --- a/c10/util/string_view.h +++ b/c10/util/string_view.h @@ -149,7 +149,7 @@ class basic_string_view final { return std::numeric_limits::max(); } - C10_NODISCARD constexpr bool empty() const noexcept { + [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; } diff --git a/torch/csrc/distributed/c10d/init.cpp b/torch/csrc/distributed/c10d/init.cpp index b6796c510f9..c39ca299725 100644 --- a/torch/csrc/distributed/c10d/init.cpp +++ b/torch/csrc/distributed/c10d/init.cpp @@ -123,7 +123,7 @@ class IntrusivePtrNoGilDestructor { T* operator->() const noexcept { return impl_.get(); } - C10_NODISCARD T* get() const noexcept { + [[nodiscard]] T* get() const noexcept { return impl_.get(); } void reset() noexcept { diff --git a/torch/csrc/jit/mobile/nnc/context.h b/torch/csrc/jit/mobile/nnc/context.h index 3976d28ec89..b9633ea5bfa 100644 --- a/torch/csrc/jit/mobile/nnc/context.h +++ b/torch/csrc/jit/mobile/nnc/context.h @@ -22,10 +22,10 @@ struct TORCH_API InputSpec { explicit InputSpec(const c10::IValue& value); // Serialize the spec into an IValue. - C10_NODISCARD c10::IValue serialize() const; + [[nodiscard]] c10::IValue serialize() const; // Check whether the input tensor adheres to the spec. - C10_NODISCARD bool validate(const at::Tensor& input) const; + [[nodiscard]] bool validate(const at::Tensor& input) const; std::vector sizes_; c10::ScalarType dtype_{c10::ScalarType::Undefined}; @@ -40,10 +40,10 @@ struct TORCH_API OutputSpec { explicit OutputSpec(const c10::IValue& value); // Serialize the spec into an IValue. - C10_NODISCARD c10::IValue serialize() const; + [[nodiscard]] c10::IValue serialize() const; // Allocate an output tensor in accordance with the spec. - C10_NODISCARD at::Tensor allocate() const; + [[nodiscard]] at::Tensor allocate() const; std::vector sizes_; c10::ScalarType dtype_{c10::ScalarType::Undefined}; @@ -84,7 +84,7 @@ struct TORCH_API MemoryPlan { explicit MemoryPlan(const c10::IValue& value); - C10_NODISCARD c10::IValue serialize() const; + [[nodiscard]] c10::IValue serialize() const; void allocate(ExecutionState* state) const; @@ -207,10 +207,10 @@ class TORCH_API CompilationUnit { // Serialize all registered functions into an IValue. The IValue will be save // into the compiled TorchScript model file ahead-of-time on the host, and // will be deserialized at runtime on the target device. - C10_NODISCARD c10::IValue serialize() const; + [[nodiscard]] c10::IValue serialize() const; // Execute a registered function. - C10_NODISCARD c10::impl::GenericList run( + [[nodiscard]] c10::impl::GenericList run( const c10::QualifiedName& function_name, const c10::impl::GenericList& inputs) const; @@ -218,7 +218,7 @@ class TORCH_API CompilationUnit { void register_function(std::unique_ptr fn); private: - C10_NODISCARD Function* find_function(const c10::QualifiedName& qn) const; + [[nodiscard]] Function* find_function(const c10::QualifiedName& qn) const; std::unordered_map> functions_; }; diff --git a/torch/csrc/jit/mobile/type_parser.cpp b/torch/csrc/jit/mobile/type_parser.cpp index 8b92c91643e..5f15110d9fa 100644 --- a/torch/csrc/jit/mobile/type_parser.cpp +++ b/torch/csrc/jit/mobile/type_parser.cpp @@ -336,7 +336,7 @@ void TypeParser::advance() { lex(); } -C10_NODISCARD c10::string_view TypeParser::cur() const { +[[nodiscard]] c10::string_view TypeParser::cur() const { return next_token_; } diff --git a/torch/csrc/jit/mobile/type_parser.h b/torch/csrc/jit/mobile/type_parser.h index 420e43a5c40..e2cb66e3fe8 100644 --- a/torch/csrc/jit/mobile/type_parser.h +++ b/torch/csrc/jit/mobile/type_parser.h @@ -33,7 +33,7 @@ class TORCH_API TypeParser { std::string next(); c10::string_view nextView(); void advance(); - C10_NODISCARD c10::string_view cur() const; + [[nodiscard]] c10::string_view cur() const; std::string pythonStr_; size_t start_; diff --git a/torch/csrc/jit/runtime/static/ProcessedNodeInputs.h b/torch/csrc/jit/runtime/static/ProcessedNodeInputs.h index 1f81c67368c..81d4b06d156 100644 --- a/torch/csrc/jit/runtime/static/ProcessedNodeInputs.h +++ b/torch/csrc/jit/runtime/static/ProcessedNodeInputs.h @@ -43,7 +43,7 @@ class ProcessedNodeInputs { } } - C10_NODISCARD uint16_t size() const { + [[nodiscard]] uint16_t size() const { if (C10_LIKELY(repr_.is_inline())) { return repr_.inline_repr_.size; } else { @@ -51,7 +51,7 @@ class ProcessedNodeInputs { } } - C10_NODISCARD bool empty() const { + [[nodiscard]] bool empty() const { return size() == 0; } @@ -93,11 +93,11 @@ class ProcessedNodeInputs { HeapArrayPtr(HeapArrayPtr&&) noexcept = default; HeapArrayPtr& operator=(HeapArrayPtr&&) noexcept = default; - C10_NODISCARD bool empty() const { + [[nodiscard]] bool empty() const { return size() != 0; } - C10_NODISCARD uint16_t size() const { + [[nodiscard]] uint16_t size() const { return array_ ? array_[0] : 0; } @@ -137,7 +137,7 @@ class ProcessedNodeInputs { // awkward. #pragma pack(push, 2) union Repr { - C10_NODISCARD bool is_inline() const { + [[nodiscard]] bool is_inline() const { uint8_t tag = 0; // Use of reinterpret_cast to pointer to char or unsigned char // is defined behavior; see diff --git a/torch/csrc/jit/runtime/static/impl.h b/torch/csrc/jit/runtime/static/impl.h index eb8eceb41dc..7087d39f2e1 100644 --- a/torch/csrc/jit/runtime/static/impl.h +++ b/torch/csrc/jit/runtime/static/impl.h @@ -456,7 +456,7 @@ class TORCH_API StaticModule { return num_inputs() + num_constants() + num_intermediate_values(); } - C10_NODISCARD const std::vector& output_indices() const { + [[nodiscard]] const std::vector& output_indices() const { return output_indices_; } @@ -488,7 +488,7 @@ class TORCH_API StaticModule { }); } - C10_NODISCARD Node* findNodeWithKindForTesting(const std::string& kind) const; + [[nodiscard]] Node* findNodeWithKindForTesting(const std::string& kind) const; const std::optional& schema() const { return schema_; @@ -644,7 +644,7 @@ class TORCH_API BlockRunner { } // Output is readonly. The writing process happens inside ProcessedNodes - C10_NODISCARD const IValue& Output(uint32_t i) const { + [[nodiscard]] const IValue& Output(uint32_t i) const { DCHECK(i < outputs_.size()); return *outputs_[i]; } @@ -923,7 +923,7 @@ class TORCH_API ProcessedNode { } // Input is readonly - C10_NODISCARD const IValue& Input(uint32_t i) const { + [[nodiscard]] const IValue& Input(uint32_t i) const { return values_[inputs_[i]]; } @@ -933,7 +933,7 @@ class TORCH_API ProcessedNode { return values_[outputs_offset_ + i]; } - C10_NODISCARD const IValue& Output(uint32_t i) const { + [[nodiscard]] const IValue& Output(uint32_t i) const { DCHECK(i < num_outputs()); return values_[outputs_offset_ + i]; } @@ -943,12 +943,12 @@ class TORCH_API ProcessedNode { return static_cast(fn_->num_outputs()); } - C10_NODISCARD c10::ArrayRef outputs() const { + [[nodiscard]] c10::ArrayRef outputs() const { return c10::ArrayRef( values_ + outputs_offset_, num_outputs()); } - C10_NODISCARD uint16_t num_inputs() const { + [[nodiscard]] uint16_t num_inputs() const { return inputs_.size(); } @@ -990,7 +990,7 @@ class TORCH_API ProcessedNode { values_ = values; } - C10_NODISCARD uint16_t output_ivalue_index(uint16_t i) const { + [[nodiscard]] uint16_t output_ivalue_index(uint16_t i) const { DCHECK(i < num_outputs()); return outputs_offset_ + i; } @@ -1019,9 +1019,9 @@ class TORCH_API ProcessedNode { } private: - C10_NODISCARD bool verify_outputs_dont_overlap_each_other() const; + [[nodiscard]] bool verify_outputs_dont_overlap_each_other() const; - C10_NODISCARD bool verify_inputs_dont_overlap_outputs(bool force_check) const; + [[nodiscard]] bool verify_inputs_dont_overlap_outputs(bool force_check) const; Node* node_; const ProcessedFunction* fn_; diff --git a/torch/csrc/jit/runtime/static/memory_planner.h b/torch/csrc/jit/runtime/static/memory_planner.h index 8110a83dba9..018b8947a07 100644 --- a/torch/csrc/jit/runtime/static/memory_planner.h +++ b/torch/csrc/jit/runtime/static/memory_planner.h @@ -172,15 +172,15 @@ class MemoryPlanner { return managed_output_tensors_.size(); } - C10_NODISCARD size_t total_num_unmanaged() const { + [[nodiscard]] size_t total_num_unmanaged() const { return num_unmanaged_non_scalars() + num_unmanaged_scalars(); } - C10_NODISCARD size_t num_unmanaged_non_scalars() const { + [[nodiscard]] size_t num_unmanaged_non_scalars() const { return unmanaged_ivalues_.size() + unmanaged_borrowed_ivalues_.size(); } - C10_NODISCARD size_t num_unmanaged_scalars() const { + [[nodiscard]] size_t num_unmanaged_scalars() const { return num_unmanaged_scalar_ivalues_; } diff --git a/torch/csrc/jit/serialization/flatbuffer_serializer.h b/torch/csrc/jit/serialization/flatbuffer_serializer.h index 41fb52415a1..5474e48ccf1 100644 --- a/torch/csrc/jit/serialization/flatbuffer_serializer.h +++ b/torch/csrc/jit/serialization/flatbuffer_serializer.h @@ -32,15 +32,15 @@ class TORCH_API DetachedBuffer final { : data_(data), size_(size), data_owner_(internal_data_owner) {} /// Returns a pointer to the data. - C10_NODISCARD void* data() { + [[nodiscard]] void* data() { return data_; } /// Returns a pointer to the data. - C10_NODISCARD const void* data() const { + [[nodiscard]] const void* data() const { return data_; } /// Returns the size of the data, in bytes. - C10_NODISCARD size_t size() const { + [[nodiscard]] size_t size() const { return size_; }