diff options
author | Abseil Team <absl-team@google.com> | 2022-08-04 05:19:56 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-08-04 05:20:40 -0700 |
commit | 751ade00ee347abef5dac7248db851e3f2012e14 (patch) | |
tree | d5946590986f065406933c68bda8fd6af3013473 /absl/status | |
parent | 07360899e64ded32e9a5e304bd6a3b6a0ff266bc (diff) |
Fix "unsafe narrowing" warnings in absl, 3/n.
Addresses failures with the following, in some files:
-Wshorten-64-to-32
-Wimplicit-int-conversion
-Wsign-compare
-Wsign-conversion
-Wtautological-unsigned-zero-compare
(This specific CL focuses on .cc files in dirs n-t, except string.)
Bug: chromium:1292951
PiperOrigin-RevId: 465287204
Change-Id: I0fe98ff78bf3c08d86992019eb626755f8b6803e
Diffstat (limited to 'absl/status')
-rw-r--r-- | absl/status/status.cc | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/absl/status/status.cc b/absl/status/status.cc index 88e8eda9..bbf2335d 100644 --- a/absl/status/status.cc +++ b/absl/status/status.cc @@ -16,6 +16,7 @@ #include <errno.h> #include <cassert> +#include <utility> #include "absl/base/internal/raw_logging.h" #include "absl/base/internal/strerror.h" @@ -77,15 +78,17 @@ std::ostream& operator<<(std::ostream& os, StatusCode code) { namespace status_internal { -static int FindPayloadIndexByUrl(const Payloads* payloads, - absl::string_view type_url) { - if (payloads == nullptr) return -1; +static absl::optional<size_t> FindPayloadIndexByUrl( + const Payloads* payloads, + absl::string_view type_url) { + if (payloads == nullptr) + return absl::nullopt; for (size_t i = 0; i < payloads->size(); ++i) { if ((*payloads)[i].type_url == type_url) return i; } - return -1; + return absl::nullopt; } // Convert canonical code to a value known to this binary. @@ -119,8 +122,10 @@ absl::StatusCode MapToLocalCode(int value) { absl::optional<absl::Cord> Status::GetPayload( absl::string_view type_url) const { const auto* payloads = GetPayloads(); - int index = status_internal::FindPayloadIndexByUrl(payloads, type_url); - if (index != -1) return (*payloads)[index].payload; + absl::optional<size_t> index = + status_internal::FindPayloadIndexByUrl(payloads, type_url); + if (index.has_value()) + return (*payloads)[index.value()].payload; return absl::nullopt; } @@ -135,10 +140,10 @@ void Status::SetPayload(absl::string_view type_url, absl::Cord payload) { rep->payloads = absl::make_unique<status_internal::Payloads>(); } - int index = + absl::optional<size_t> index = status_internal::FindPayloadIndexByUrl(rep->payloads.get(), type_url); - if (index != -1) { - (*rep->payloads)[index].payload = std::move(payload); + if (index.has_value()) { + (*rep->payloads)[index.value()].payload = std::move(payload); return; } @@ -146,10 +151,11 @@ void Status::SetPayload(absl::string_view type_url, absl::Cord payload) { } bool Status::ErasePayload(absl::string_view type_url) { - int index = status_internal::FindPayloadIndexByUrl(GetPayloads(), type_url); - if (index != -1) { + absl::optional<size_t> index = + status_internal::FindPayloadIndexByUrl(GetPayloads(), type_url); + if (index.has_value()) { PrepareToModify(); - GetPayloads()->erase(GetPayloads()->begin() + index); + GetPayloads()->erase(GetPayloads()->begin() + index.value()); if (GetPayloads()->empty() && message().empty()) { // Special case: If this can be represented inlined, it MUST be // inlined (EqualsSlow depends on this behavior). |