Skip to content

Commit 1af129f

Browse files
derekmaurocopybara-github
authored andcommitted
Use C++17 [[nodiscard]] instead of the deprecated ABSL_MUST_USE_RESULT
In this change, //absl/status is intentionally excluded because of complication with SWIG compatibility. This may be handled separately. PiperOrigin-RevId: 731387819 Change-Id: I71bf2e02f3a477d65575d467f5e5ab163846d31e
1 parent 8e1f92a commit 1af129f

File tree

26 files changed

+134
-150
lines changed

26 files changed

+134
-150
lines changed

absl/base/internal/spinlock.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
8989
// acquisition was successful. If the lock was not acquired, false is
9090
// returned. If this SpinLock is free at the time of the call, TryLock
9191
// will return true with high probability.
92-
ABSL_MUST_USE_RESULT inline bool TryLock()
93-
ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
92+
[[nodiscard]] inline bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
9493
ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_try_lock);
9594
bool res = TryLockImpl();
9695
ABSL_TSAN_MUTEX_POST_LOCK(
@@ -121,7 +120,7 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
121120
// Determine if the lock is held. When the lock is held by the invoking
122121
// thread, true will always be returned. Intended to be used as
123122
// CHECK(lock.IsHeld()).
124-
ABSL_MUST_USE_RESULT inline bool IsHeld() const {
123+
[[nodiscard]] inline bool IsHeld() const {
125124
return (lockword_.load(std::memory_order_relaxed) & kSpinLockHeld) != 0;
126125
}
127126

@@ -203,16 +202,7 @@ class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED SpinLock {
203202

204203
// Corresponding locker object that arranges to acquire a spinlock for
205204
// the duration of a C++ scope.
206-
//
207-
// TODO(b/176172494): Use only [[nodiscard]] when baseline is raised.
208-
// TODO(b/6695610): Remove forward declaration when #ifdef is no longer needed.
209-
#if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
210-
class [[nodiscard]] SpinLockHolder;
211-
#else
212-
class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI SpinLockHolder;
213-
#endif
214-
215-
class ABSL_SCOPED_LOCKABLE SpinLockHolder {
205+
class ABSL_SCOPED_LOCKABLE [[nodiscard]] SpinLockHolder {
216206
public:
217207
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
218208
: lock_(l) {

absl/cleanup/cleanup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace absl {
7878
ABSL_NAMESPACE_BEGIN
7979

8080
template <typename Arg, typename Callback = void()>
81-
class ABSL_MUST_USE_RESULT Cleanup final {
81+
class [[nodiscard]] Cleanup final {
8282
static_assert(cleanup_internal::WasDeduced<Arg>(),
8383
"Explicit template parameters are not supported.");
8484

absl/container/internal/inlined_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class AllocationTransaction {
229229
return result.data;
230230
}
231231

232-
ABSL_MUST_USE_RESULT Allocation<A> Release() && {
232+
[[nodiscard]] Allocation<A> Release() && {
233233
Allocation<A> result = {GetData(), GetCapacity()};
234234
Reset();
235235
return result;

absl/debugging/internal/demangle_rust.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class RustSymbolParser {
8484
// structure was not recognized or exceeded implementation limits, such as by
8585
// nesting structures too deep. In either case *this should not be used
8686
// again.
87-
ABSL_MUST_USE_RESULT bool Parse() && {
87+
[[nodiscard]] bool Parse() && {
8888
// Recursively parses the grammar production named by callee, then resumes
8989
// execution at the next statement.
9090
//
@@ -564,7 +564,7 @@ class RustSymbolParser {
564564

565565
// If the next input character is the given character, consumes it and returns
566566
// true; otherwise returns false without consuming a character.
567-
ABSL_MUST_USE_RESULT bool Eat(char want) {
567+
[[nodiscard]] bool Eat(char want) {
568568
if (encoding_[pos_] != want) return false;
569569
++pos_;
570570
return true;
@@ -573,7 +573,7 @@ class RustSymbolParser {
573573
// Provided there is enough remaining output space, appends c to the output,
574574
// writing a fresh NUL terminator afterward, and returns true. Returns false
575575
// if the output buffer had less than two bytes free.
576-
ABSL_MUST_USE_RESULT bool EmitChar(char c) {
576+
[[nodiscard]] bool EmitChar(char c) {
577577
if (silence_depth_ > 0) return true;
578578
if (out_end_ - out_ < 2) return false;
579579
*out_++ = c;
@@ -584,7 +584,7 @@ class RustSymbolParser {
584584
// Provided there is enough remaining output space, appends the C string token
585585
// to the output, followed by a NUL character, and returns true. Returns
586586
// false if not everything fit into the output buffer.
587-
ABSL_MUST_USE_RESULT bool Emit(const char* token) {
587+
[[nodiscard]] bool Emit(const char* token) {
588588
if (silence_depth_ > 0) return true;
589589
const size_t token_length = std::strlen(token);
590590
const size_t bytes_to_copy = token_length + 1; // token and final NUL
@@ -598,7 +598,7 @@ class RustSymbolParser {
598598
// of disambiguator (if it's nonnegative) or "?" (if it's negative) to the
599599
// output, followed by a NUL character, and returns true. Returns false if
600600
// not everything fit into the output buffer.
601-
ABSL_MUST_USE_RESULT bool EmitDisambiguator(int disambiguator) {
601+
[[nodiscard]] bool EmitDisambiguator(int disambiguator) {
602602
if (disambiguator < 0) return EmitChar('?'); // parsed but too large
603603
if (disambiguator == 0) return EmitChar('0');
604604
// Convert disambiguator to decimal text. Three digits per byte is enough
@@ -618,7 +618,7 @@ class RustSymbolParser {
618618
// On success returns true and fills value with the encoded value if it was
619619
// not too big, otherwise with -1. If the optional disambiguator was omitted,
620620
// value is 0. On parse failure returns false and sets value to -1.
621-
ABSL_MUST_USE_RESULT bool ParseDisambiguator(int& value) {
621+
[[nodiscard]] bool ParseDisambiguator(int& value) {
622622
value = -1;
623623

624624
// disambiguator = s base-62-number
@@ -639,7 +639,7 @@ class RustSymbolParser {
639639
// On success returns true and fills value with the encoded value if it was
640640
// not too big, otherwise with -1. On parse failure returns false and sets
641641
// value to -1.
642-
ABSL_MUST_USE_RESULT bool ParseBase62Number(int& value) {
642+
[[nodiscard]] bool ParseBase62Number(int& value) {
643643
value = -1;
644644

645645
// base-62-number = (digit | lower | upper)* _
@@ -686,7 +686,7 @@ class RustSymbolParser {
686686
// A nonzero uppercase_namespace specifies the character after the N in a
687687
// nested-identifier, e.g., 'C' for a closure, allowing ParseIdentifier to
688688
// write out the name with the conventional decoration for that namespace.
689-
ABSL_MUST_USE_RESULT bool ParseIdentifier(char uppercase_namespace = '\0') {
689+
[[nodiscard]] bool ParseIdentifier(char uppercase_namespace = '\0') {
690690
// identifier -> disambiguator? undisambiguated-identifier
691691
int disambiguator = 0;
692692
if (!ParseDisambiguator(disambiguator)) return false;
@@ -703,7 +703,7 @@ class RustSymbolParser {
703703
//
704704
// At other appearances of undisambiguated-identifier in the grammar, this
705705
// treatment is not applicable, and the call site omits both arguments.
706-
ABSL_MUST_USE_RESULT bool ParseUndisambiguatedIdentifier(
706+
[[nodiscard]] bool ParseUndisambiguatedIdentifier(
707707
char uppercase_namespace = '\0', int disambiguator = 0) {
708708
// undisambiguated-identifier -> u? decimal-number _? bytes
709709
const bool is_punycoded = Eat('u');
@@ -766,7 +766,7 @@ class RustSymbolParser {
766766
// Consumes a decimal number like 0 or 123 from the input. On success returns
767767
// true and fills value with the encoded value. If the encoded value is too
768768
// large or otherwise unparsable, returns false and sets value to -1.
769-
ABSL_MUST_USE_RESULT bool ParseDecimalNumber(int& value) {
769+
[[nodiscard]] bool ParseDecimalNumber(int& value) {
770770
value = -1;
771771
if (!IsDigit(Peek())) return false;
772772
int encoded_number = Take() - '0';
@@ -788,7 +788,7 @@ class RustSymbolParser {
788788
// Consumes a binder of higher-ranked lifetimes if one is present. On success
789789
// returns true and discards the encoded lifetime count. On parse failure
790790
// returns false.
791-
ABSL_MUST_USE_RESULT bool ParseOptionalBinder() {
791+
[[nodiscard]] bool ParseOptionalBinder() {
792792
// binder -> G base-62-number
793793
if (!Eat('G')) return true;
794794
int ignored_binding_count;
@@ -802,7 +802,7 @@ class RustSymbolParser {
802802
// things we omit from output, such as the entire contents of generic-args.
803803
//
804804
// On parse failure returns false.
805-
ABSL_MUST_USE_RESULT bool ParseOptionalLifetime() {
805+
[[nodiscard]] bool ParseOptionalLifetime() {
806806
// lifetime -> L base-62-number
807807
if (!Eat('L')) return true;
808808
int ignored_de_bruijn_index;
@@ -811,14 +811,14 @@ class RustSymbolParser {
811811

812812
// Consumes a lifetime just like ParseOptionalLifetime, but returns false if
813813
// there is no lifetime here.
814-
ABSL_MUST_USE_RESULT bool ParseRequiredLifetime() {
814+
[[nodiscard]] bool ParseRequiredLifetime() {
815815
if (Peek() != 'L') return false;
816816
return ParseOptionalLifetime();
817817
}
818818

819819
// Pushes ns onto the namespace stack and returns true if the stack is not
820820
// full, else returns false.
821-
ABSL_MUST_USE_RESULT bool PushNamespace(char ns) {
821+
[[nodiscard]] bool PushNamespace(char ns) {
822822
if (namespace_depth_ == kNamespaceStackSize) return false;
823823
namespace_stack_[namespace_depth_++] = ns;
824824
return true;
@@ -830,7 +830,7 @@ class RustSymbolParser {
830830

831831
// Pushes position onto the position stack and returns true if the stack is
832832
// not full, else returns false.
833-
ABSL_MUST_USE_RESULT bool PushPosition(int position) {
833+
[[nodiscard]] bool PushPosition(int position) {
834834
if (position_depth_ == kPositionStackSize) return false;
835835
position_stack_[position_depth_++] = position;
836836
return true;
@@ -845,7 +845,7 @@ class RustSymbolParser {
845845
// beginning of the backref target. Returns true on success. Returns false
846846
// if parsing failed, the stack is exhausted, or the backref target position
847847
// is out of range.
848-
ABSL_MUST_USE_RESULT bool BeginBackref() {
848+
[[nodiscard]] bool BeginBackref() {
849849
// backref = B base-62-number (B already consumed)
850850
//
851851
// Reject backrefs that don't parse, overflow int, or don't point backward.

absl/flags/flag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ using Flag = flags_internal::Flag<T>;
9494
// // FLAGS_firstname is a Flag of type `std::string`
9595
// std::string first_name = absl::GetFlag(FLAGS_firstname);
9696
template <typename T>
97-
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
97+
[[nodiscard]] T GetFlag(const absl::Flag<T>& flag) {
9898
return flags_internal::FlagImplPeer::InvokeGet<T>(flag);
9999
}
100100

absl/flags/internal/flag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ template <typename T>
5757
using Flag = flags_internal::Flag<T>;
5858

5959
template <typename T>
60-
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag);
60+
[[nodiscard]] T GetFlag(const absl::Flag<T>& flag);
6161

6262
template <typename T>
6363
void SetFlag(absl::Flag<T>* flag, const T& v);

absl/hash/hash_testing.h

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,20 @@ ABSL_NAMESPACE_BEGIN
141141
// }
142142
//
143143
template <int&... ExplicitBarrier, typename Container>
144-
ABSL_MUST_USE_RESULT testing::AssertionResult
145-
VerifyTypeImplementsAbslHashCorrectly(const Container& values);
144+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
145+
const Container& values);
146146

147147
template <int&... ExplicitBarrier, typename Container, typename Eq>
148-
ABSL_MUST_USE_RESULT testing::AssertionResult
149-
VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals);
148+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
149+
const Container& values, Eq equals);
150150

151151
template <int&..., typename T>
152-
ABSL_MUST_USE_RESULT testing::AssertionResult
153-
VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values);
152+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
153+
std::initializer_list<T> values);
154154

155155
template <int&..., typename T, typename Eq>
156-
ABSL_MUST_USE_RESULT testing::AssertionResult
157-
VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values,
158-
Eq equals);
156+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
157+
std::initializer_list<T> values, Eq equals);
159158

160159
namespace hash_internal {
161160

@@ -184,8 +183,8 @@ struct ExpandVisitor {
184183
};
185184

186185
template <typename Container, typename Eq>
187-
ABSL_MUST_USE_RESULT testing::AssertionResult
188-
VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals) {
186+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
187+
const Container& values, Eq equals) {
189188
using V = typename Container::value_type;
190189

191190
struct Info {
@@ -343,32 +342,31 @@ struct DefaultEquals {
343342
} // namespace hash_internal
344343

345344
template <int&..., typename Container>
346-
ABSL_MUST_USE_RESULT testing::AssertionResult
347-
VerifyTypeImplementsAbslHashCorrectly(const Container& values) {
345+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
346+
const Container& values) {
348347
return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
349348
hash_internal::ContainerAsVector<Container>::Do(values),
350349
hash_internal::DefaultEquals{});
351350
}
352351

353352
template <int&..., typename Container, typename Eq>
354-
ABSL_MUST_USE_RESULT testing::AssertionResult
355-
VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals) {
353+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
354+
const Container& values, Eq equals) {
356355
return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
357356
hash_internal::ContainerAsVector<Container>::Do(values), equals);
358357
}
359358

360359
template <int&..., typename T>
361-
ABSL_MUST_USE_RESULT testing::AssertionResult
362-
VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values) {
360+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
361+
std::initializer_list<T> values) {
363362
return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
364363
hash_internal::ContainerAsVector<std::initializer_list<T>>::Do(values),
365364
hash_internal::DefaultEquals{});
366365
}
367366

368367
template <int&..., typename T, typename Eq>
369-
ABSL_MUST_USE_RESULT testing::AssertionResult
370-
VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values,
371-
Eq equals) {
368+
testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(
369+
std::initializer_list<T> values, Eq equals) {
372370
return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
373371
hash_internal::ContainerAsVector<std::initializer_list<T>>::Do(values),
374372
equals);

absl/log/die_if_null.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ namespace log_internal {
6060

6161
// Helper for `ABSL_DIE_IF_NULL`.
6262
template <typename T>
63-
ABSL_MUST_USE_RESULT T DieIfNull(const char* file, int line,
64-
const char* exprtext, T&& t) {
63+
[[nodiscard]] T DieIfNull(const char* file, int line, const char* exprtext,
64+
T&& t) {
6565
if (ABSL_PREDICT_FALSE(t == nullptr)) {
6666
// Call a non-inline helper function for a small code size improvement.
6767
DieBecauseNull(file, line, exprtext);

absl/log/globals.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ABSL_NAMESPACE_BEGIN
4343
//
4444
// Returns the value of the Minimum Log Level parameter.
4545
// This function is async-signal-safe.
46-
ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast MinLogLevel();
46+
[[nodiscard]] absl::LogSeverityAtLeast MinLogLevel();
4747

4848
// SetMinLogLevel()
4949
//
@@ -82,7 +82,7 @@ class ScopedMinLogLevel final {
8282
//
8383
// Returns the value of the Stderr Threshold parameter.
8484
// This function is async-signal-safe.
85-
ABSL_MUST_USE_RESULT absl::LogSeverityAtLeast StderrThreshold();
85+
[[nodiscard]] absl::LogSeverityAtLeast StderrThreshold();
8686

8787
// SetStderrThreshold()
8888
//
@@ -118,8 +118,7 @@ class ScopedStderrThreshold final {
118118
//
119119
// Returns true if we should log a backtrace at the specified location.
120120
namespace log_internal {
121-
ABSL_MUST_USE_RESULT bool ShouldLogBacktraceAt(absl::string_view file,
122-
int line);
121+
[[nodiscard]] bool ShouldLogBacktraceAt(absl::string_view file, int line);
123122
} // namespace log_internal
124123

125124
// SetLogBacktraceLocation()
@@ -145,7 +144,7 @@ void ClearLogBacktraceLocation();
145144
//
146145
// Returns the value of the Prepend Log Prefix option.
147146
// This function is async-signal-safe.
148-
ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
147+
[[nodiscard]] bool ShouldPrependLogPrefix();
149148

150149
// EnableLogPrefix()
151150
//

absl/log/internal/proto.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ bool EncodeBytesTruncate(uint64_t tag, absl::Span<const char> value,
123123
return true;
124124
}
125125

126-
ABSL_MUST_USE_RESULT absl::Span<char> EncodeMessageStart(
127-
uint64_t tag, uint64_t max_size, absl::Span<char> *buf) {
126+
[[nodiscard]] absl::Span<char> EncodeMessageStart(uint64_t tag,
127+
uint64_t max_size,
128+
absl::Span<char> *buf) {
128129
const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
129130
const size_t tag_type_size = VarintSize(tag_type);
130131
max_size = std::min<uint64_t>(max_size, buf->size());

0 commit comments

Comments
 (0)