Skip to content

Commit 60f60d0

Browse files
ckennellycopybara-github
authored andcommitted
Add [[nodiscard]] to const message field accessors.
This covers logically constant APIs (similar to the guidance in https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3122r1.html) where ignoring the return value likely indicates an omission on the part of the caller or a call that can be cleaned up. For example: my_message.mutable_child()->grandchild() Creates a child instance but grandchild() is a no-op. mutable_grandchild() may have been intended. PiperOrigin-RevId: 836249595
1 parent 244e1ea commit 60f60d0

File tree

3 files changed

+128
-94
lines changed

3 files changed

+128
-94
lines changed

src/google/protobuf/compiler/cpp/field_generators/message_field.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void SingularMessage::GenerateAccessorDeclarations(io::Printer* p) const {
156156
AnnotatedAccessors(field_, {"mutable_"}, AnnotationCollector::kAlias));
157157

158158
p->Emit(R"cc(
159-
$DEPRECATED$ const $Submsg$& $name$() const;
159+
$DEPRECATED$ [[nodiscard]] const $Submsg$& $name$() const;
160160
$DEPRECATED$ [[nodiscard]] $Submsg$* $nullable$ $release_name$();
161161
$DEPRECATED$ $Submsg$* $nonnull$ $mutable_name$();
162162
$DEPRECATED$ void $set_allocated_name$($Submsg$* $nullable$ value);
@@ -773,9 +773,10 @@ void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const {
773773
}
774774
p->Emit(R"cc(
775775
public:
776-
$DEPRECATED$ const $Submsg$& $name$(int index) const;
776+
[[nodiscard]] $DEPRECATED$ const $Submsg$& $name$(int index) const;
777777
$DEPRECATED$ $Submsg$* $nonnull$ $add_name$();
778-
$DEPRECATED$ const $pb$::RepeatedPtrField<$Submsg$>& $name$() const;
778+
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedPtrField<$Submsg$>& $name$()
779+
const;
779780
)cc");
780781
}
781782

src/google/protobuf/compiler/plugin.pb.h

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)