Skip to content

Commit bc60e2e

Browse files
ckennellycopybara-github
authored andcommitted
Mark mutable repeated fields/maps as [[nodiscard]]
Since these fields have implicit presence, these are no-ops if the returned pointer is unused. These are logically constant, but named "mutable_..." and are non-const methods of the class in order to return the non-const references. Ignoring the return value likely indicates an omission on the part of the caller, an error by confusing it with explicit presence fields (like scalar submessages or strings), or a call that can be cleaned up. PiperOrigin-RevId: 839401150
1 parent 9a50829 commit bc60e2e

File tree

10 files changed

+126
-84
lines changed

10 files changed

+126
-84
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ void RepeatedEnum::GenerateAccessorDeclarations(io::Printer* p) const {
409409
$DEPRECATED$ void $set_name$(int index, $Enum$ value);
410410
$DEPRECATED$ void $add_name$($Enum$ value);
411411
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedField<int>& $name$() const;
412-
$DEPRECATED$ $pb$::RepeatedField<int>* $nonnull$ $mutable_name$();
412+
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedField<int>* $nonnull$ $mutable_name$();
413413
414414
private:
415415
const $pb$::RepeatedField<int>& $_internal_name$() const;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void Map::GenerateAccessorDeclarations(io::Printer* p) const {
206206
io::AnnotationCollector::kAlias));
207207
p->Emit(R"cc(
208208
[[nodiscard]] $DEPRECATED$ const $Map$& $name$() const;
209-
$DEPRECATED$ $Map$* $nonnull$ $mutable_name$();
209+
[[nodiscard]] $DEPRECATED$ $Map$* $nonnull$ $mutable_name$();
210210
211211
private:
212212
const $Map$& $_internal_name$() const;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,9 @@ void RepeatedMessage::GenerateAccessorDeclarations(io::Printer* p) const {
758758
io::AnnotationCollector::kAlias));
759759

760760
p->Emit(R"cc(
761-
$DEPRECATED$ $Submsg$* $nonnull$ $mutable_name$(int index);
762-
$DEPRECATED$ $pb$::RepeatedPtrField<$Submsg$>* $nonnull$ $mutable_name$();
761+
[[nodiscard]] $DEPRECATED$ $Submsg$* $nonnull$ $mutable_name$(int index);
762+
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedPtrField<$Submsg$>* $nonnull$
763+
$mutable_name$();
763764
764765
private:
765766
const $pb$::RepeatedPtrField<$Submsg$>& $_internal_name$() const;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ void RepeatedString::GenerateAccessorDeclarations(io::Printer* p) const {
848848
$DEPRECATED$ void $add_name$(Arg_&& value, Args_... args);
849849
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedPtrField<::std::string>&
850850
$name$() const;
851-
$DEPRECATED$ $pb$::RepeatedPtrField<::std::string>* $nonnull$ $mutable_name$();
851+
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedPtrField<::std::string>* $nonnull$
852+
$mutable_name$();
852853
853854
private:
854855
const $pb$::RepeatedPtrField<::std::string>& _internal_$name$() const;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ void RepeatedStringView::GenerateAccessorDeclarations(io::Printer* p) const {
719719
$DEPRECATED$ void add_$name$(Arg_&& value);
720720
[[nodiscard]] $DEPRECATED$ const $pb$::RepeatedPtrField<::std::string>&
721721
$name$() const;
722-
$DEPRECATED$ $pb$::RepeatedPtrField<::std::string>* $nonnull$ $mutable_name$();
722+
[[nodiscard]] $DEPRECATED$ $pb$::RepeatedPtrField<::std::string>* $nonnull$
723+
$mutable_name$();
723724
724725
private:
725726
const $pb$::RepeatedPtrField<::std::string>& _internal_$name$() const;

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

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

0 commit comments

Comments
 (0)