Skip to content

Commit

Permalink
prefer *std::move(std::optional{...}) to std::move(*std::optional{...})
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676119820
Change-Id: Iebc54df1c6880c52149c8913185c59a96ac701aa
  • Loading branch information
laramiel authored and copybara-github committed Sep 18, 2024
1 parent 012fd80 commit 151183d
Show file tree
Hide file tree
Showing 43 changed files with 122 additions and 65 deletions.
2 changes: 1 addition & 1 deletion python/tensorstore/batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace py = ::pybind11;
Batch ValidateOptionalBatch(std::optional<Batch> batch) {
if (!batch) return no_batch;
if (!*batch) throw py::value_error("batch was already submitted");
return std::move(*batch);
return *std::move(batch);
}

namespace {
Expand Down
4 changes: 2 additions & 2 deletions python/tensorstore/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Constructs a default context.
std::optional<ContextImplPtr> parent) {
if (!parent) parent.emplace();
return Access::impl(Context(WrapImpl(std::move(spec)),
WrapImpl(std::move(*parent))));
WrapImpl(*std::move(parent))));
}),
R"(
Constructs a context from a parsed spec.
Expand All @@ -122,7 +122,7 @@ Constructs a context from a parsed spec.
py::init([](::nlohmann::json json, std::optional<ContextImplPtr> parent) {
if (!parent) parent.emplace();
return Access::impl(Context(ValueOrThrow(Context::Spec::FromJson(json)),
WrapImpl(std::move(*parent))));
WrapImpl(*std::move(parent))));
}),
R"(
Constructs a context from its :json:schema:`JSON representation<Context>`.
Expand Down
2 changes: 1 addition & 1 deletion python/tensorstore/kvstore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ py::bytes CordToPython(const absl::Cord& value) {
std::optional<absl::Cord> OptionalCordFromPython(
std::optional<std::string_view> value) {
if (!value) return std::nullopt;
return absl::Cord(std::move(*value));
return absl::Cord(*value);
}

namespace kvstore_spec_setters {
Expand Down
2 changes: 1 addition & 1 deletion python/tensorstore/tensorstore_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ Associated transaction used for read/write operations.
if (!transaction) transaction.emplace();
return ValueOrThrow(
self.value |
internal::TransactionState::ToTransaction(std::move(*transaction)));
internal::TransactionState::ToTransaction(*std::move(transaction)));
},
R"(Returns a transaction-bound view of this TensorStore.
Expand Down
4 changes: 2 additions & 2 deletions tensorstore/context_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class ResourceProviderImpl : public ResourceProviderImplBase {
return std::move(result).status();
}
return ResourceImplStrongPtr(
new ResourceImpl(ResourceSpecImplPtr(this), std::move(*result)));
new ResourceImpl(ResourceSpecImplPtr(this), *std::move(result)));
}

Result<::nlohmann::json> ToJson(JsonSerializationOptions options) override {
Expand Down Expand Up @@ -448,7 +448,7 @@ class ResourceProviderImpl : public ResourceProviderImplBase {
auto result =
internal_json_binding::FromJson<Spec>(j, traits_.JsonBinder(), options);
if (!result) return std::move(result).status();
return ResourceSpecImplPtr(new SpecImpl(std::move(*result)));
return ResourceSpecImplPtr(new SpecImpl(*std::move(result)));
}

void AcquireContextReference(ResourceImplBase& resource) const override {
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/driver/array/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ Result<tensorstore::Spec> SpecFromArray(SharedOffsetArrayView<const void> array,
if (!zero_origin_array->valid() && zero_origin_array->num_elements() != 0) {
return absl::InvalidArgumentError("Array is not valid");
}
driver_spec->array = std::move(*zero_origin_array);
driver_spec->array = *std::move(zero_origin_array);
impl.driver_spec = std::move(driver_spec);
return spec;
}
Expand Down
8 changes: 4 additions & 4 deletions tensorstore/driver/image/driver_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ class ImageCache : public internal::KvsBackedCache<ImageCache<Specialization>,
}
auto options = GetOwningCache(*this).specialization_;
GetOwningCache(*this).executor()(
[value = std::move(value), receiver = std::move(receiver),
[value = *std::move(value), receiver = std::move(receiver),
options = std::move(options)]() mutable {
auto decode_result = options.DecodeImage(std::move(*value));
auto decode_result = options.DecodeImage(std::move(value));
if (!decode_result.ok()) {
execution::set_error(receiver, decode_result.status());
} else {
execution::set_value(receiver, std::make_shared<ReadData>(
std::move(*decode_result)));
*std::move(decode_result)));
}
});
}
Expand All @@ -239,7 +239,7 @@ class ImageCache : public internal::KvsBackedCache<ImageCache<Specialization>,
if (!encode_result.ok()) {
execution::set_error(receiver, encode_result.status());
} else {
execution::set_value(receiver, std::move(*encode_result));
execution::set_value(receiver, *std::move(encode_result));
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions tensorstore/driver/json/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class JsonCache
return;
}
execution::set_value(receiver, std::make_shared<::nlohmann::json>(
std::move(*decode_result)));
*std::move(decode_result)));
});
}
void DoEncode(std::shared_ptr<const ReadData> data,
Expand Down Expand Up @@ -173,7 +173,7 @@ class JsonCache
? *existing_json
: ::nlohmann::json(::nlohmann::json::value_t::discarded));
if (result.ok()) {
new_json = std::move(*result);
new_json = *std::move(result);
} else {
execution::set_error(receiver, std::move(result).status());
return;
Expand Down
20 changes: 10 additions & 10 deletions tensorstore/driver/kvs_backed_chunk_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ struct ResolveBoundsForDeleteAndResizeContinuation {
std::shared_ptr<const void> new_metadata;
if (auto result = ValidateNewMetadata(*state->driver, state->transaction);
result.ok()) {
new_metadata = std::move(*result);
new_metadata = *std::move(result);
} else {
promise.SetResult(std::move(result).status());
return;
Expand Down Expand Up @@ -702,7 +702,7 @@ Future<IndexTransform<>> KvsChunkedDriverBase::Resize(
/*.transaction=*/std::move(request.transaction),
/*.component_index=*/component_index(),
/*.transform=*/std::move(request.transform),
/*.resize_parameters=*/std::move(*resize_parameters),
/*.resize_parameters=*/*std::move(resize_parameters),
};
if ((request.options.mode & resize_metadata_only) == resize_metadata_only ||
(request.options.mode & expand_only) == expand_only) {
Expand Down Expand Up @@ -861,7 +861,7 @@ Result<internal::Driver::Handle> CreateTensorStoreFromMetadata(
return nullptr;
}
DataCacheInitializer initializer;
initializer.store = std::move(*store_result);
initializer.store = *std::move(store_result);
initializer.metadata_cache_entry = base.metadata_cache_entry_;
initializer.metadata = metadata;
initializer.cache_pool = state->cache_pool();
Expand Down Expand Up @@ -978,7 +978,7 @@ struct HandleReadMetadata {
if (auto result =
base.metadata_cache_entry_->GetMetadata(base.transaction_);
result.ok()) {
metadata = std::move(*result);
metadata = *std::move(result);
} else {
promise.SetResult(std::move(result).status());
return;
Expand Down Expand Up @@ -1151,10 +1151,10 @@ void MetadataCache::Entry::DoDecode(std::optional<absl::Cord> value,
receiver = std::move(receiver)]() mutable {
MetadataPtr new_metadata;
if (value) {
if (auto result =
GetOwningCache(*this).DecodeMetadata(this->key(), *value);
if (auto result = GetOwningCache(*this).DecodeMetadata(this->key(),
*std::move(value));
result.ok()) {
new_metadata = std::move(*result);
new_metadata = *std::move(result);
} else {
execution::set_error(
receiver, internal::ConvertInvalidArgumentToFailedPrecondition(
Expand Down Expand Up @@ -1188,7 +1188,7 @@ void MetadataCache::TransactionNode::DoApply(ApplyOptions options,
auto read_state = AsyncCache::ReadLock<void>(*this).read_state();
std::shared_ptr<const void> new_data;
if (auto result = this->GetUpdatedMetadata(read_state.data); result.ok()) {
new_data = std::move(*result);
new_data = *std::move(result);
} else {
execution::set_error(receiver, std::move(result).status());
return;
Expand Down Expand Up @@ -1219,7 +1219,7 @@ void MetadataCache::Entry::DoEncode(std::shared_ptr<const void> data,
auto& cache = GetOwningCache(entry);
if (auto encoded_result = cache.EncodeMetadata(entry.key(), data.get());
encoded_result.ok()) {
execution::set_value(receiver, std::move(*encoded_result));
execution::set_value(receiver, *std::move(encoded_result));
} else {
execution::set_error(receiver, std::move(encoded_result).status());
}
Expand Down Expand Up @@ -1272,7 +1272,7 @@ internal::CachePtr<MetadataCache> GetOrCreateMetadataCache(
if (auto result = state->GetMetadataKeyValueStore(
metadata_cache->base_store_);
result.ok()) {
metadata_cache->SetKvStoreDriver(std::move(*result));
metadata_cache->SetKvStoreDriver(*std::move(result));
} else {
metadata_cache_promise.SetResult(std::move(result).status());
}
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/driver/neuroglancer_precomputed/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class DataCacheBase : public internal_kvs_backed_chunk_driver::DataCache {
chunk_indices, metadata(), scale_index_, chunk_layout_czyx_,
std::move(data))) {
absl::InlinedVector<SharedArray<const void>, 1> components;
components.emplace_back(std::move(*result));
components.emplace_back(*std::move(result));
return components;
} else {
return absl::FailedPreconditionError(result.status().message());
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/driver/zarr/spec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Result<ZarrMetadataPtr> GetNewMetadata(
}
TENSORSTORE_RETURN_IF_ERROR(codec_spec->MergeFrom(schema.codec()));
if (codec_spec->compressor) {
metadata->compressor = std::move(*codec_spec->compressor);
metadata->compressor = *std::move(codec_spec->compressor);
} else {
TENSORSTORE_ASSIGN_OR_RETURN(metadata->compressor,
Compressor::FromJson({{"id", "blosc"}}));
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/driver/zarr3/codec/blosc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class BloscCodec : public ZarrBytesToBytesCodec {
return *std::move(output);
});
auto reader = std::make_unique<riegeli::ChainReader<riegeli::Chain>>(
output.ok() ? riegeli::Chain(std::move(*output)) : riegeli::Chain());
output.ok() ? riegeli::Chain(*std::move(output)) : riegeli::Chain());
if (!output.ok()) {
reader->Fail(std::move(output).status());
}
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/index_space/index_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ MakeCopy(const Array& array,
if (auto result = TransformArray<OriginKind>(array, transform,
{constraints, must_allocate})) {
return ConstDataTypeCast<std::remove_const_t<typename Array::Element>>(
std::move(*result));
*std::move(result));
} else {
return std::move(result).status();
}
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/index_space/transformed_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ inline Result<Array<ElementTag, Rank, OriginKind>> TryConvertToArrayImpl(
array.element_pointer() = ElementPointer<ElementTag>(
StaticDataTypeCast<typename ElementTagTraits<ElementTag>::Element,
unchecked>(
ConstDataTypeCast<void>(std::move(*element_pointer_result))));
ConstDataTypeCast<void>(*std::move(element_pointer_result))));
return array;
}

Expand Down
6 changes: 6 additions & 0 deletions tensorstore/internal/cache/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,18 @@ tensorstore_cc_library(
srcs = ["kvs_backed_chunk_cache.cc"],
hdrs = ["kvs_backed_chunk_cache.h"],
deps = [
":async_cache",
":cache",
":chunk_cache",
":kvs_backed_cache",
"//tensorstore:array",
"//tensorstore:index",
"//tensorstore/internal:memory",
"//tensorstore/util:result",
"//tensorstore/util:span",
"//tensorstore/util:status",
"//tensorstore/util/execution",
"@com_google_absl//absl/container:fixed_array",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/strings:cord",
],
Expand Down
4 changes: 2 additions & 2 deletions tensorstore/internal/cache/kvs_backed_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class KvsBackedCache : public Parent {
void set_cancel() { ABSL_UNREACHABLE(); } // COV_NF_LINE
void set_value(std::optional<absl::Cord> value) {
kvstore::ReadResult read_result =
value ? kvstore::ReadResult::Value(std::move(*value),
value ? kvstore::ReadResult::Value(*std::move(value),
std::move(update_stamp_))
: kvstore::ReadResult::Missing(std::move(update_stamp_));
execution::set_value(receiver_, std::move(read_result));
Expand Down Expand Up @@ -403,7 +403,7 @@ class KvsBackedCache : public Parent {
void KvsWritebackSuccess(TimestampedStorageGeneration new_stamp) override {
if (new_data_) {
this->WritebackSuccess(
AsyncCache::ReadState{std::move(*new_data_), std::move(new_stamp)});
AsyncCache::ReadState{*std::move(new_data_), std::move(new_stamp)});
} else {
// Unmodified.
this->WritebackSuccess(AsyncCache::ReadState{});
Expand Down
14 changes: 12 additions & 2 deletions tensorstore/internal/cache/kvs_backed_chunk_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@

#include "tensorstore/internal/cache/kvs_backed_chunk_cache.h"

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "absl/container/fixed_array.h"
#include "absl/container/inlined_vector.h"
#include "absl/strings/cord.h"
#include "tensorstore/array.h"
#include "tensorstore/index.h"
#include "tensorstore/internal/cache/async_cache.h"
#include "tensorstore/internal/cache/cache.h"
#include "tensorstore/internal/cache/chunk_cache.h"
#include "tensorstore/internal/cache/kvs_backed_cache.h"
#include "tensorstore/internal/memory.h"
#include "tensorstore/util/execution/execution.h"
#include "tensorstore/util/result.h"
#include "tensorstore/util/span.h"
#include "tensorstore/util/status.h"

namespace tensorstore {
namespace internal {
Expand All @@ -45,7 +55,7 @@ void KvsBackedChunkCache::Entry::DoDecode(std::optional<absl::Cord> value,
}
auto& cache = GetOwningCache(*this);
auto decoded_result =
cache.DecodeChunk(this->cell_indices(), std::move(*value));
cache.DecodeChunk(this->cell_indices(), *std::move(value));
if (!decoded_result.ok()) {
execution::set_error(receiver,
internal::ConvertInvalidArgumentToFailedPrecondition(
Expand Down Expand Up @@ -92,7 +102,7 @@ void KvsBackedChunkCache::Entry::DoEncode(std::shared_ptr<const ReadData> data,
execution::set_error(receiver, std::move(encoded_result).status());
return;
}
execution::set_value(receiver, std::move(*encoded_result));
execution::set_value(receiver, *std::move(encoded_result));
}

} // namespace internal
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/internal/compression/blosc_compressor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ std::unique_ptr<riegeli::Reader> BloscCompressor::GetReader(
return *std::move(output);
});
auto reader = std::make_unique<riegeli::ChainReader<riegeli::Chain>>(
output.ok() ? riegeli::Chain(std::move(*output)) : riegeli::Chain());
output.ok() ? riegeli::Chain(*std::move(output)) : riegeli::Chain());
if (!output.ok()) {
reader->Fail(std::move(output).status());
}
Expand Down
4 changes: 2 additions & 2 deletions tensorstore/internal/http/curl_factory_hook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ void CurlPtrHook(CurlPtr& handle) {
// Return either "SSL_CERT_FILE" or "SSL_CERT_DIR" if set.
if (auto default_cert_env = internal::GetEnv("SSL_CERT_FILE");
default_cert_env.has_value()) {
return CertFile{std::move(*default_cert_env)};
return CertFile{*std::move(default_cert_env)};
}
if (auto default_cert_env = internal::GetEnv("SSL_CERT_DIR");
default_cert_env.has_value()) {
return CertDirectory{std::move(*default_cert_env)};
return CertDirectory{*std::move(default_cert_env)};
}

// This check only happens on startup so that all the curl handles use
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/internal/http/http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class HttpRequestBuilder {
return AddHeader(std::string_view(header));
}
HttpRequestBuilder& AddHeader(std::optional<std::string> header) {
return header ? AddHeader(std::move(*header)) : *this;
return header ? AddHeader(*std::move(header)) : *this;
}

/// Adds a `range` header to the http request if the byte_range
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/internal/json/value_as.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ JsonRequireValueAs(const ::nlohmann::json& j, T* result, ValidateFn is_valid,
j, internal_json::GetTypeName(internal::type_identity<T>{}));
}
if (result != nullptr) {
*result = std::move(*value);
*result = *std::move(value);
}
return absl::OkStatus();
}
Expand Down
2 changes: 1 addition & 1 deletion tensorstore/internal/metrics/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::vector<CollectedMetric> MetricRegistry::CollectWithPrefix(
if (prefix.empty() || absl::StartsWith(kv.first, prefix)) {
auto opt_metric = kv.second.poly(CollectMetricTag{});
if (opt_metric.has_value()) {
all.emplace_back(std::move(*opt_metric));
all.emplace_back(*std::move(opt_metric));
assert(all.back().metric_name == kv.first);
}
}
Expand Down
3 changes: 2 additions & 1 deletion tensorstore/internal/oauth2/google_auth_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <fstream>
#include <functional>
#include <memory>
#include <new>
#include <optional>
#include <string>
#include <utility>
Expand Down Expand Up @@ -133,7 +134,7 @@ Result<std::unique_ptr<AuthProvider>> GetDefaultGoogleAuthProvider(
auto var = GetEnv(kGoogleAuthTokenForTesting);
if (var) {
ABSL_LOG(INFO) << "Using GOOGLE_AUTH_TOKEN_FOR_TESTING";
result.reset(new FixedTokenAuthProvider(std::move(*var)));
result.reset(new FixedTokenAuthProvider(*std::move(var)));
return std::move(result);
}

Expand Down
2 changes: 1 addition & 1 deletion tensorstore/kvstore/gcs_grpc/gcs_grpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ Future<TimestampedStorageGeneration> GcsGrpcKeyValueStore::Write(
task->driver_ = internal::IntrusivePtr<GcsGrpcKeyValueStore>(this);
task->options_ = std::move(options);
task->promise_ = std::move(op.promise);
task->Start(key, std::move(*value));
task->Start(key, *std::move(value));
}
return std::move(op.future);
}
Expand Down
Loading

0 comments on commit 151183d

Please sign in to comment.