diff --git a/aten/src/ATen/core/List_test.cpp b/aten/src/ATen/core/List_test.cpp index 77b4281d362..45aa36cca3a 100644 --- a/aten/src/ATen/core/List_test.cpp +++ b/aten/src/ATen/core/List_test.cpp @@ -38,7 +38,6 @@ TEST(ListTestIValueBasedList, whenCallingGetWithExistingPosition_thenReturnsElem TEST(ListTestIValueBasedList, whenCallingGetWithNonExistingPosition_thenThrowsException) { List list({"3", "4"}); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.get(2), std::out_of_range); } @@ -56,7 +55,6 @@ TEST(ListTestIValueBasedList, whenCallingExtractWithExistingPosition_thenListEle TEST(ListTestIValueBasedList, whenCallingExtractWithNonExistingPosition_thenThrowsException) { List list({"3", "4"}); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.extract(2), std::out_of_range); } @@ -79,14 +77,12 @@ TEST(ListTestIValueBasedList, whenCallingMovingSetWithExistingPosition_thenChang TEST(ListTestIValueBasedList, whenCallingCopyingSetWithNonExistingPosition_thenThrowsException) { List list({"3", "4"}); string value = "5"; - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.set(2, value), std::out_of_range); } TEST(ListTestIValueBasedList, whenCallingMovingSetWithNonExistingPosition_thenThrowsException) { List list({"3", "4"}); string value = "5"; - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.set(2, std::move(value)), std::out_of_range); } @@ -122,7 +118,6 @@ TEST(ListTestIValueBasedList, whenSwappingFromAccessOperator_thenSwapsElements) TEST(ListTestIValueBasedList, whenCallingAccessOperatorWithNonExistingPosition_thenThrowsException) { List list({"3", "4"}); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list[2], std::out_of_range); } @@ -586,7 +581,6 @@ TEST(ListTestNonIValueBasedList, whenCallingGetWithExistingPosition_thenReturnsE TEST(ListTestNonIValueBasedList, whenCallingGetWithNonExistingPosition_thenThrowsException) { List list({3, 4}); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.get(2), std::out_of_range); } @@ -598,7 +592,6 @@ TEST(ListTestNonIValueBasedList, whenCallingExtractWithExistingPosition_thenRetu TEST(ListTestNonIValueBasedList, whenCallingExtractWithNonExistingPosition_thenThrowsException) { List list({3, 4}); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.extract(2), std::out_of_range); } @@ -622,14 +615,12 @@ TEST(ListTestNonIValueBasedList, whenCallingMovingSetWithExistingPosition_thenCh TEST(ListTestNonIValueBasedList, whenCallingCopyingSetWithNonExistingPosition_thenThrowsException) { List list({3, 4}); int64_t value = 5; - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list.set(2, value), std::out_of_range); } TEST(ListTestNonIValueBasedList, whenCallingMovingSetWithNonExistingPosition_thenThrowsException) { List list({3, 4}); int64_t value = 5; - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,performance-move-const-arg,hicpp-avoid-goto) EXPECT_THROW(list.set(2, std::move(value)), std::out_of_range); } @@ -665,7 +656,6 @@ TEST(ListTestNonIValueBasedList, whenSwappingFromAccessOperator_thenSwapsElement TEST(ListTestNonIValueBasedList, whenCallingAccessOperatorWithNonExistingPosition_thenThrowsException) { List list({3, 4}); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto) EXPECT_THROW(list[2], std::out_of_range); } @@ -1134,12 +1124,11 @@ TEST(ListTest, canAccessOptionalStringByReference) { "List> access should be by const reference"); std::optional str1 = list[1]; std::optional str2 = list[2]; - decltype(auto) strRef1 = listRef[1]; - decltype(auto) strRef2 = listRef[2]; - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - EXPECT_EQ("two", str1.value()); + auto const& strRef1 = listRef[1]; + auto const& strRef2 = listRef[2]; + EXPECT_EQ("two", str1); EXPECT_FALSE(str2.has_value()); - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) + EXPECT_TRUE(strRef1.has_value()); EXPECT_EQ("two", strRef1.value().get()); EXPECT_FALSE(strRef2.has_value()); } diff --git a/aten/src/ATen/core/dynamic_type.cpp b/aten/src/ATen/core/dynamic_type.cpp index 7d11eae1834..543c6f830f4 100644 --- a/aten/src/ATen/core/dynamic_type.cpp +++ b/aten/src/ATen/core/dynamic_type.cpp @@ -80,7 +80,8 @@ DynamicType::~DynamicType() { std::shared_ptr DynamicType::create(const Type& other) { if (auto dynRaw = other.castRaw()) { - TORCH_INTERNAL_ASSERT(!dynRaw->weak_from_this().expired(), + TORCH_INTERNAL_ASSERT( + !dynRaw->weak_from_this().expired(), "Error creating dynamic type instance not managed by shared_ptr: ", other.str()); } @@ -92,7 +93,8 @@ std::shared_ptr DynamicType::create(const Type& other) { DynamicTypePtr DynamicType::create(Type& other) { if (auto dynRaw = other.castRaw()) { - TORCH_INTERNAL_ASSERT(!dynRaw->weak_from_this().expired(), + TORCH_INTERNAL_ASSERT( + !dynRaw->weak_from_this().expired(), "Error creating dynamic type instance not managed by shared_ptr: ", other.str()); } @@ -262,7 +264,7 @@ TypePtr DynamicType::fallback() const { fields.reserve(arguments_.elems.size()); for (const auto& elem : arguments_.elems) { // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - fields.emplace_back(*elem.label); + fields.emplace_back(elem.label.value()); } return TupleType::createNamed(*name_, fields, fallbacks); } @@ -292,7 +294,7 @@ TypePtr DynamicType::fallback() const { return StorageType::get(); case Tag::Var: // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - return VarType::create(*name_); + return VarType::create(name_.value()); case Tag::AnyClass: return AnyClassType::get(); case Tag::QScheme: diff --git a/aten/src/ATen/core/jit_type.h b/aten/src/ATen/core/jit_type.h index 387b8850a80..0ef321ef7a5 100644 --- a/aten/src/ATen/core/jit_type.h +++ b/aten/src/ATen/core/jit_type.h @@ -592,8 +592,8 @@ struct TORCH_API TensorType : public SharedType { static TensorTypePtr create( std::optional scalar_type, std::optional device, - const SymbolicShape& sizes, - const VaryingShape& stride_, + SymbolicShape sizes, + VaryingShape stride_, std::optional requires_grad, std::optional undefined = false); diff --git a/aten/src/ATen/core/library.cpp b/aten/src/ATen/core/library.cpp index 7cf23d93af3..8657cd9274f 100644 --- a/aten/src/ATen/core/library.cpp +++ b/aten/src/ATen/core/library.cpp @@ -73,7 +73,7 @@ Library::Library(Kind kind, std::string ns, std::optional k, c registrars_.emplace_back( c10::Dispatcher::singleton().registerLibrary( // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - *ns_, debugString(file_, line_) + ns_.value(), debugString(file_, line_) ) ); [[fallthrough]]; @@ -207,12 +207,10 @@ at::OperatorName Library::_parseNameForLib(const char* name_str) const { // This is a copy paste of Library::_impl if (ns_opt.has_value()) { // See Note [Redundancy in registration code is OK] - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - TORCH_CHECK(*ns_opt == *ns_, + TORCH_CHECK(ns_opt == ns_, IMPL_PRELUDE, - "Explicitly provided namespace (", *ns_opt, ") in operator name " - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - "does not match namespace of enclosing ", toString(kind_), " block (", *ns_, "). " + "Explicitly provided namespace (", ns_opt, ") in operator name " + "does not match namespace of enclosing ", toString(kind_), " block (", ns_, "). " "Move this definition to the ", toString(kind_), " block corresponding to this namespace " "(and consider deleting the namespace from your schema string.) ", ERROR_CONTEXT diff --git a/aten/src/ATen/core/tensor_type.cpp b/aten/src/ATen/core/tensor_type.cpp index af99dbcdc95..b4b860a7d5a 100644 --- a/aten/src/ATen/core/tensor_type.cpp +++ b/aten/src/ATen/core/tensor_type.cpp @@ -75,9 +75,9 @@ std::ostream& operator<<(std::ostream& out, const VaryingShape& vs) { if (i > 0) { out << ", "; } - if (vs[i].has_value()) { - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - out << vs[i].value(); + auto const& v = vs[i]; + if (v.has_value()) { + out << v.value(); } else { out << "*"; } @@ -98,20 +98,20 @@ std::ostream& operator<<( const SymbolicShape& ss) { // TODO: Unranked SymbolicShape printing is ambiguous with that of // dynamic-shaped vector. - if(!ss.rank()) { + auto rank_opt = ss.rank(); + if(!rank_opt.has_value()) { os << "(*)"; return os; } - - auto sizes = ss.sizes().value(); + auto sizes_opt = ss.sizes(); os << "("; - for (size_t i = 0; i < ss.rank().value(); i++) { + for (size_t i = 0; i < rank_opt.value(); i++) { if (i > 0) { os << ", "; } - if(sizes[i].is_static()) { - os << sizes[i]; + if(sizes_opt.has_value() && sizes_opt.value()[i].is_static()) { + os << sizes_opt.value()[i]; } else { os << "*"; } @@ -280,25 +280,21 @@ TensorTypePtr TensorType::create( const VaryingShape& strides, std::optional requires_grad, std::optional undefined, bool tensor_contiguity) { - if(strides.concrete_sizes() && strides.concrete_sizes().has_value()){ + const auto stride_concrete_sizes = strides.concrete_sizes(); + if(stride_concrete_sizes.has_value()){ + const auto size_concrete_sizes = sizes.concrete_sizes(); // handles case where strides are set - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - TORCH_INTERNAL_ASSERT(sizes.concrete_sizes()->size() == strides.concrete_sizes()->size()); - auto sprops = strides.concrete_sizes().has_value() - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - ? computeStrideProps(*sizes.concrete_sizes(), *strides.concrete_sizes(), tensor_contiguity) - : VaryingShape(); - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - auto symbol_sizes = SymbolicShape(*sizes.concrete_sizes()); + TORCH_INTERNAL_ASSERT(size_concrete_sizes.has_value() && size_concrete_sizes->size() == stride_concrete_sizes->size()); + auto sprops = + computeStrideProps(*size_concrete_sizes, *stride_concrete_sizes, tensor_contiguity); + auto symbol_sizes = SymbolicShape(*size_concrete_sizes); return TensorType::create( scalar_type, device, symbol_sizes, sprops, requires_grad, undefined); } else { // strides are all null, but still have number of strides equal to number of ranks TORCH_INTERNAL_ASSERT(sizes.sizes() && sizes.size()); - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) auto symbol_sizes = SymbolicShape(*sizes.sizes()); return TensorType::create( - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) scalar_type, device, symbol_sizes, VaryingShape(*sizes.size()), requires_grad, undefined); } } @@ -306,12 +302,12 @@ TensorTypePtr TensorType::create( TensorTypePtr TensorType::create( std::optional scalar_type, std::optional device, - const SymbolicShape& sizes, - const VaryingShape& strides, + SymbolicShape sizes, + VaryingShape strides, std::optional requires_grad, std::optional undefined) { auto pt = TensorTypePtr(new TensorType( - scalar_type, device, sizes, strides, requires_grad, undefined)); + scalar_type, device, std::move(sizes), std::move(strides), requires_grad, undefined)); return pt; } @@ -371,7 +367,7 @@ TensorTypePtr TensorType::merge(const TensorType& other, bool merge_sizes) const } template -bool is_null_or_equal(std::optional a, c10::IntArrayRef b) { +static bool is_null_or_equal(std::optional a, c10::IntArrayRef b) { return !a.has_value() || a.value() == b; } @@ -414,15 +410,16 @@ bool TensorType::equals(const c10::Type& rhs) const { } VaryingShape TensorType::strides() const { - if (!strides_.size().has_value()) { + auto const strides_sizes = strides_.sizes(); + if (!strides_sizes.has_value()) { return VaryingShape(); } - std::vector> ss(*strides_.size()); - for (size_t i = 0; i < *strides_.size(); i++) { - if (!strides_[i].has_value()) { + std::vector> ss(strides_sizes->size()); + for (auto const& stride:strides_sizes.value()) { + if (!stride.has_value()) { continue; } - auto s = *strides_[i]; + const auto& s = *stride; if (s.stride_index_.has_value() && s.stride_.has_value()) { ss[*s.stride_index_] = *s.stride_; } diff --git a/aten/src/ATen/core/union_type.cpp b/aten/src/ATen/core/union_type.cpp index cc39616a10e..dc4cb788721 100644 --- a/aten/src/ATen/core/union_type.cpp +++ b/aten/src/ATen/core/union_type.cpp @@ -186,7 +186,7 @@ OptionalType::OptionalType(const TypePtr& contained) std::vector to_subtract{NoneType::get()}; auto without_none = subtractTypeSetFrom(to_subtract, types_); // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - contained_ = UnionType::create({*without_none}); + contained_ = UnionType::create({std::move(without_none.value())}); } has_free_variables_ = contained_->hasFreeVariables(); } diff --git a/torch/csrc/Size.cpp b/torch/csrc/Size.cpp index 322c7a4e090..07c50db11b6 100644 --- a/torch/csrc/Size.cpp +++ b/torch/csrc/Size.cpp @@ -76,8 +76,7 @@ PyObject* THPSize_NewFromSymSizes(const at::Tensor& self_) { throw python_error(); PyTuple_SET_ITEM(ret.get(), i, py_size_tensor); } else { - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - PyTuple_SET_ITEM(ret.get(), i, THPUtils_packInt64(*m)); + PyTuple_SET_ITEM(ret.get(), i, THPUtils_packInt64(m.value())); } } } diff --git a/torch/csrc/autograd/engine.cpp b/torch/csrc/autograd/engine.cpp index c4f52f60505..24e53c4fad4 100644 --- a/torch/csrc/autograd/engine.cpp +++ b/torch/csrc/autograd/engine.cpp @@ -262,7 +262,6 @@ auto ReadyQueue::pop() -> NodeTask { // Lock mutex for accesses to heap_ std::unique_lock lock(mutex_); not_empty_.wait(lock, [this] { return !heap_.empty(); }); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) auto task = std::move(const_cast(heap_.top())); heap_.pop(); return task; @@ -735,14 +734,14 @@ void GraphTask::exec_post_processing() { // the stashed streams should be enough. If leaf_stream.device_index() // happens to be for a new device, operator* on the std::nullopt should // throw an error. - const auto caller_current_stream = - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - *caller_current_streams_[leaf_stream.device_index()]; + const auto& caller_current_stream = + caller_current_streams_[leaf_stream.device_index()]; - if (caller_current_stream != leaf_stream) { + if (caller_current_stream.has_value() && + caller_current_stream != leaf_stream) { auto event = c10::Event{leaf_stream.device_type()}; event.record(leaf_stream); - caller_current_stream.wait(event); + caller_current_stream->wait(event); } } diff --git a/torch/csrc/autograd/input_buffer.cpp b/torch/csrc/autograd/input_buffer.cpp index 4263d28340b..f127ab3c04d 100644 --- a/torch/csrc/autograd/input_buffer.cpp +++ b/torch/csrc/autograd/input_buffer.cpp @@ -158,15 +158,15 @@ void InputBuffer::add( // the consumer or producer. // Accumulation happens on the var device's default stream. - TORCH_INTERNAL_ASSERT(device_of(var)); + auto const device = device_of(var); + TORCH_INTERNAL_ASSERT(device.has_value()); std::optional opt_accumulate_stream = std::nullopt; - const auto device_type = device_of(var).value().type(); - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - if (device_of(var)->is_cuda() || device_of(var)->is_privateuseone()) { + const auto device_type = device->type(); + if (device->is_cuda() || device->is_privateuseone()) { const auto on_producer = - opt_producer_stream && device_of(var) == opt_producer_stream->device(); + opt_producer_stream && device == opt_producer_stream->device(); const auto on_consumer = - opt_consumer_stream && device_of(var) == opt_consumer_stream->device(); + opt_consumer_stream && device == opt_consumer_stream->device(); if (on_producer && on_consumer) { // (2a) @@ -192,8 +192,7 @@ void InputBuffer::add( opt_sync_stream = opt_producer_stream; } else { // (5) - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - opt_accumulate_stream = guard.getDefaultStream(*device_of(var)); + opt_accumulate_stream = guard.getDefaultStream(*device); } if (opt_sync_stream && (opt_accumulate_stream != opt_sync_stream)) { // (3b), (4b) @@ -217,7 +216,7 @@ void InputBuffer::add( } else { // (1) non-CUDA/privateuse1 variable // Accumulation happens on variable's device - c10::OptionalDeviceGuard device_guard{device_of(var)}; + c10::OptionalDeviceGuard device_guard{device}; accumulate(buffer, pos, std::move(var)); } } diff --git a/torch/csrc/autograd/profiler_python.cpp b/torch/csrc/autograd/profiler_python.cpp index 4b9232eff76..a46b33009af 100644 --- a/torch/csrc/autograd/profiler_python.cpp +++ b/torch/csrc/autograd/profiler_python.cpp @@ -457,8 +457,9 @@ ExtraFields::args_t ValueCache::load< OptimizerInfo info{ key, cls, cache.cls_names_.at(cls), cls_and_parameters.parameters_}; return { - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - /*frame_state_=*/std::get(state_).at(*cache.location_), + /*frame_state_=*/std::get(state_).at( + // NOLINTNEXTLINE(bugprone-unchecked-optional-access) + cache.location_.value()), /*module_info_=*/std::nullopt, /*optimizer_info_=*/std::move(info)}; } diff --git a/torch/csrc/autograd/variable.h b/torch/csrc/autograd/variable.h index c190f34d9e8..a0a170ca710 100644 --- a/torch/csrc/autograd/variable.h +++ b/torch/csrc/autograd/variable.h @@ -726,6 +726,7 @@ struct TORCH_API DifferentiableViewMeta : public AutogradMeta { const ViewInfo& get_backward_view() const { TORCH_CHECK( has_bw_view(), "backward view info can only exist for backward views."); + // NOLINTNEXTLINE(bugprone-unchecked-optional-access) return backward_info_.value(); } @@ -763,6 +764,7 @@ struct TORCH_API DifferentiableViewMeta : public AutogradMeta { TORCH_CHECK( !shared_view_info_ || has_bw_view(), "forward view info can only exist for forward views."); + // NOLINTNEXTLINE(bugprone-unchecked-optional-access) return shared_view_info_ ? backward_info_.value() : forward_info_.value(); } diff --git a/torch/csrc/distributed/c10d/reducer.cpp b/torch/csrc/distributed/c10d/reducer.cpp index cba800c9503..e332f5ba65e 100644 --- a/torch/csrc/distributed/c10d/reducer.cpp +++ b/torch/csrc/distributed/c10d/reducer.cpp @@ -1162,8 +1162,8 @@ void Reducer::initialize_buckets( // Make gradient type in the reduced precision if mixed precision is // enabled. This ensures that the type is correct when e.g. rebuilding // buckets. - if (mixed_precision_param_dtype_) { - options = options.dtype(*mixed_precision_param_dtype_); + if (mixed_precision_param_dtype_.has_value()) { + options = options.dtype(mixed_precision_param_dtype_); } bucket.gradients = at::empty({static_cast(offset)}, options); @@ -1625,8 +1625,9 @@ void Reducer::finalize_backward() { // sparse metadata is set so the bucket should have sparse_tensor_indices if (sparse_metadata_) { REDUCER_CHECK( - bucket.sparse_tensor_indices.value().numel() == - bucket.gradients.sizes()[0], + bucket.sparse_tensor_indices.has_value() && + bucket.sparse_tensor_indices.value().numel() == + bucket.gradients.sizes()[0], logger_, "Sparse metadata and gradient size mismatch"); auto sparse_result = at::sparse_coo_tensor( @@ -1689,7 +1690,7 @@ void Reducer::finalize_backward() { void Reducer::runGradCallbackForVariable( at::Tensor& variable, - GradCallback&& cb) { + const GradCallback& cb) { #ifdef _WIN32 cb(variable.mutable_grad()); #else diff --git a/torch/csrc/distributed/c10d/reducer.hpp b/torch/csrc/distributed/c10d/reducer.hpp index 26237d61d54..55a0576a6d8 100644 --- a/torch/csrc/distributed/c10d/reducer.hpp +++ b/torch/csrc/distributed/c10d/reducer.hpp @@ -308,7 +308,7 @@ class TORCH_API Reducer { GradCallback, torch::distributed::autograd::DistAutogradContext::GradCallback>); #endif - void runGradCallbackForVariable(at::Tensor& variable, GradCallback&& cb); + void runGradCallbackForVariable(at::Tensor& variable, const GradCallback& cb); // This function is called inside `initialize_buckets()`. It initializes both // `bucket_views_in` and `bucket_views_out` with views for each variable's diff --git a/torch/csrc/distributed/c10d/sequence_num.cpp b/torch/csrc/distributed/c10d/sequence_num.cpp index 3807d629d83..d4f75b1fef7 100644 --- a/torch/csrc/distributed/c10d/sequence_num.cpp +++ b/torch/csrc/distributed/c10d/sequence_num.cpp @@ -23,7 +23,7 @@ uint64_t SequenceNum::get() const { void SequenceNum::increment() { std::lock_guard lock(lock_); - TORCH_CHECK(num_ != std::nullopt); + TORCH_CHECK(num_.has_value()); num_ = ++(*num_); } @@ -32,7 +32,7 @@ void SequenceNum::increment() { uint64_t SequenceNum::getAndIncrement() { uint64_t curVal = 0; std::lock_guard lock(lock_); - TORCH_CHECK(num_ != std::nullopt); + TORCH_CHECK(num_.has_value()); curVal = *num_; num_ = ++(*num_); return curVal; @@ -45,7 +45,7 @@ void SequenceNum::set(const uint64_t num) { bool SequenceNum::isSet() const { std::lock_guard lock(lock_); - return num_ != std::nullopt; + return num_.has_value(); } SequenceNum& SequenceNum::operator=(const SequenceNum& other) { diff --git a/torch/csrc/distributed/rpc/script_call.cpp b/torch/csrc/distributed/rpc/script_call.cpp index 5417244aa9d..7c7a870bee1 100644 --- a/torch/csrc/distributed/rpc/script_call.cpp +++ b/torch/csrc/distributed/rpc/script_call.cpp @@ -35,7 +35,7 @@ bool ScriptCall::hasQualifiedName() const { } const c10::QualifiedName& ScriptCall::qualifiedName() const { - return *qualifiedName_; + return qualifiedName_.value(); } const std::vector& ScriptCall::stack() const { diff --git a/torch/csrc/lazy/core/tensor.cpp b/torch/csrc/lazy/core/tensor.cpp index ba922621bf8..4f153399b1e 100644 --- a/torch/csrc/lazy/core/tensor.cpp +++ b/torch/csrc/lazy/core/tensor.cpp @@ -122,8 +122,9 @@ BackendDataPtr LazyTensor::GetDataHandle() { if (data()->ir_value) { ApplyPendingGraph(); } else { - TORCH_CHECK(data()->tensor_data); - data()->handle = TensorToDataHandle(*data()->tensor_data, GetDevice()); + auto const& tensor_data = data()->tensor_data; + TORCH_CHECK(tensor_data.has_value()); + data()->handle = TensorToDataHandle(*tensor_data, GetDevice()); } return data()->handle; diff --git a/torch/csrc/lazy/core/tensor_impl.cpp b/torch/csrc/lazy/core/tensor_impl.cpp index 6ee69a9f671..04730d55295 100644 --- a/torch/csrc/lazy/core/tensor_impl.cpp +++ b/torch/csrc/lazy/core/tensor_impl.cpp @@ -205,8 +205,9 @@ bool LTCTensorImpl::is_contiguous_custom(c10::MemoryFormat _unused) const { // TODO(ezyang): I don't think this branch is actually necessary // TODO(ezyang): I don't think this logic is right, shouldn't we pass on // the memory format? - if (tensor_->CurrentTensorData()) { - return tensor_->CurrentTensorData()->is_contiguous(); + const auto data = tensor_->CurrentTensorData(); + if (data.has_value()) { + return data->is_contiguous(); } // Only check that the storage is already contiguous. TORCH_CHECK(is_contiguous_, "Non-contiguous storage for lazy tensor"); diff --git a/torch/csrc/profiler/data_flow.cpp b/torch/csrc/profiler/data_flow.cpp index 96b143d23bd..6738db961d9 100644 --- a/torch/csrc/profiler/data_flow.cpp +++ b/torch/csrc/profiler/data_flow.cpp @@ -128,7 +128,7 @@ void calculateUniqueTensorIDs( for (const auto& t : tensors) { if (t.impl_ != NoTensorImpl) { // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - tensor_set.insert(*t.allocation_id_ref_.get()); + tensor_set.insert(t.allocation_id_ref_.get().value()); } } tensors.erase( @@ -136,7 +136,7 @@ void calculateUniqueTensorIDs( tensors.begin(), tensors.end(), [&tensor_set](const auto& i) { - auto it = tensor_set.find(*i.allocation_id_ref_.get()); + auto it = tensor_set.find(i.allocation_id_ref_.get().value()); return it == tensor_set.end(); }), tensors.end()); @@ -188,7 +188,7 @@ void calculateUniqueTensorIDs( // -------------------------------------------------------------------------- for (const auto& t : tensors) { // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - const auto id = id_map.at(*t.allocation_id_ref_.get()); + const auto id = id_map.at(t.allocation_id_ref_.get().value()); t.id_ref_.get().emplace(TensorID(id)); } } diff --git a/torch/csrc/utils/out_types.cpp b/torch/csrc/utils/out_types.cpp index 4799f0ed47e..088fe2e6749 100644 --- a/torch/csrc/utils/out_types.cpp +++ b/torch/csrc/utils/out_types.cpp @@ -14,13 +14,11 @@ void check_out_type_matches( if (scalarType_is_none && !layout && device_is_none) { // common case return; } - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - if (!scalarType_is_none && result.scalar_type() != scalarType.value()) { + if (!scalarType_is_none && result.scalar_type() != scalarType) { TORCH_CHECK( false, "dtype ", - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - *scalarType, + scalarType, " does not match dtype of out parameter (", result.scalar_type(), ")"); diff --git a/torch/csrc/utils/pybind.cpp b/torch/csrc/utils/pybind.cpp index 9433e1540be..2ff645b7593 100644 --- a/torch/csrc/utils/pybind.cpp +++ b/torch/csrc/utils/pybind.cpp @@ -58,7 +58,7 @@ py::handle type_caster::cast( } else { auto m = si.maybe_as_int(); // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - return py::cast(*m).release(); + return py::cast(m.value()).release(); } }