Skip to content

Commit bbbc7b9

Browse files
Suppress ReturnValueIgnored errorprone issues
To be able to run this check on protobuf generated sources. Also changed string_field.cc from using a variable to using the SuppressWarnings annotation, because that takes less bytecode. PiperOrigin-RevId: 676534547
1 parent c47de0f commit bbbc7b9

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/google/protobuf/compiler/java/lite/enum_field.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ void SetEnumVariables(
109109
variables->insert({"unknown", (*variables)["default"]});
110110
}
111111

112-
// We use `x.getClass()` as a null check because it generates less bytecode
113-
// than an `if (x == null) { throw ... }` statement.
114-
(*variables)["null_check"] = "value.getClass();\n";
115112
// Calls to Annotate() use variable ranges to know which text to annotate.
116113
(*variables)["{"] = "";
117114
(*variables)["}"] = "";
@@ -693,17 +690,19 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
693690
WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER,
694691
context_->options());
695692
printer->Print(variables_,
693+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
696694
"private void set$capitalized_name$(\n"
697695
" int index, $type$ value) {\n"
698-
" $null_check$"
696+
" value.getClass(); // minimal bytecode null check\n"
699697
" ensure$capitalized_name$IsMutable();\n"
700698
" $name$_.setInt(index, value.getNumber());\n"
701699
"}\n");
702700
WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
703701
context_->options());
704702
printer->Print(variables_,
703+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
705704
"private void add$capitalized_name$($type$ value) {\n"
706-
" $null_check$"
705+
" value.getClass(); // minimal bytecode null check\n"
707706
" ensure$capitalized_name$IsMutable();\n"
708707
" $name$_.addInt(value.getNumber());\n"
709708
"}\n");

src/google/protobuf/compiler/java/lite/message_field.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ void SetMessageVariables(
8686
(*variables)["set_has_field_bit_to_local"] =
8787
GenerateSetBitToLocal(messageBitIndex);
8888

89-
// We use `x.getClass()` as a null check because it generates less bytecode
90-
// than an `if (x == null) { throw ... }` statement.
91-
(*variables)["null_check"] = "value.getClass();\n";
9289
// Annotations often use { and } to determine ranges.
9390
(*variables)["{"] = "";
9491
(*variables)["}"] = "";
@@ -178,8 +175,9 @@ void ImmutableMessageFieldLiteGenerator::GenerateMembers(
178175
// Field.Builder setField(Field value)
179176
WriteFieldDocComment(printer, descriptor_, context_->options());
180177
printer->Print(variables_,
178+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
181179
"private void set$capitalized_name$($type$ value) {\n"
182-
" $null_check$"
180+
" value.getClass(); // minimal bytecode null check\n"
183181
" $name$_ = value;\n"
184182
" $set_has_field_bit_message$\n"
185183
" }\n");
@@ -188,9 +186,10 @@ void ImmutableMessageFieldLiteGenerator::GenerateMembers(
188186
WriteFieldDocComment(printer, descriptor_, context_->options());
189187
printer->Print(
190188
variables_,
191-
"@java.lang.SuppressWarnings({\"ReferenceEquality\"})\n"
189+
"@java.lang.SuppressWarnings({\"ReferenceEquality\", "
190+
"\"ReturnValueIgnored\"})\n"
192191
"private void merge$capitalized_name$($type$ value) {\n"
193-
" $null_check$"
192+
" value.getClass(); // minimal bytecode null check\n"
194193
" if ($name$_ != null &&\n"
195194
" $name$_ != $type$.getDefaultInstance()) {\n"
196195
" $name$_ =\n"
@@ -384,8 +383,9 @@ void ImmutableMessageOneofFieldLiteGenerator::GenerateMembers(
384383
// Field.Builder setField(Field value)
385384
WriteFieldDocComment(printer, descriptor_, context_->options());
386385
printer->Print(variables_,
386+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
387387
"private void set$capitalized_name$($type$ value) {\n"
388-
" $null_check$"
388+
" value.getClass(); // minimal bytecode null check\n"
389389
" $oneof_name$_ = value;\n"
390390
" $set_oneof_case_message$;\n"
391391
"}\n");
@@ -394,8 +394,9 @@ void ImmutableMessageOneofFieldLiteGenerator::GenerateMembers(
394394
WriteFieldDocComment(printer, descriptor_, context_->options());
395395
printer->Print(
396396
variables_,
397+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
397398
"private void merge$capitalized_name$($type$ value) {\n"
398-
" $null_check$"
399+
" value.getClass(); // minimal bytecode null check\n"
399400
" if ($has_oneof_case_message$ &&\n"
400401
" $oneof_name$_ != $type$.getDefaultInstance()) {\n"
401402
" $oneof_name$_ = $type$.newBuilder(($type$) $oneof_name$_)\n"
@@ -599,28 +600,31 @@ void RepeatedImmutableMessageFieldLiteGenerator::GenerateMembers(
599600
// Builder setRepeatedField(int index, Field value)
600601
WriteFieldDocComment(printer, descriptor_, context_->options());
601602
printer->Print(variables_,
603+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
602604
"private void set$capitalized_name$(\n"
603605
" int index, $type$ value) {\n"
604-
" $null_check$"
606+
" value.getClass(); // minimal bytecode null check\n"
605607
" ensure$capitalized_name$IsMutable();\n"
606608
" $name$_.set(index, value);\n"
607609
"}\n");
608610

609611
// Builder addRepeatedField(Field value)
610612
WriteFieldDocComment(printer, descriptor_, context_->options());
611613
printer->Print(variables_,
614+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
612615
"private void add$capitalized_name$($type$ value) {\n"
613-
" $null_check$"
616+
" value.getClass(); // minimal bytecode null check\n"
614617
" ensure$capitalized_name$IsMutable();\n"
615618
" $name$_.add(value);\n"
616619
"}\n");
617620

618621
// Builder addRepeatedField(int index, Field value)
619622
WriteFieldDocComment(printer, descriptor_, context_->options());
620623
printer->Print(variables_,
624+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
621625
"private void add$capitalized_name$(\n"
622626
" int index, $type$ value) {\n"
623-
" $null_check$"
627+
" value.getClass(); // minimal bytecode null check\n"
624628
" ensure$capitalized_name$IsMutable();\n"
625629
" $name$_.add(index, value);\n"
626630
"}\n");

src/google/protobuf/compiler/java/lite/string_field.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ void SetPrimitiveVariables(
5757
absl::StrCat(static_cast<int32_t>(WireFormat::MakeTag(descriptor)));
5858
(*variables)["tag_size"] = absl::StrCat(
5959
WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
60-
// We use `x.getClass()` as a null check because it generates less bytecode
61-
// than an `if (x == null) { throw ... }` statement.
62-
(*variables)["null_check"] =
63-
" java.lang.Class<?> valueClass = value.getClass();\n";
6460

6561
// TODO: Add @deprecated javadoc when generating javadoc is supported
6662
// by the proto compiler
@@ -228,9 +224,10 @@ void ImmutableStringFieldLiteGenerator::GenerateMembers(
228224
WriteFieldAccessorDocComment(printer, descriptor_, SETTER,
229225
context_->options());
230226
printer->Print(variables_,
227+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
231228
"private void set$capitalized_name$(\n"
232229
" java.lang.String value) {\n"
233-
"$null_check$"
230+
" value.getClass(); // minimal bytecode null check\n"
234231
" $set_has_field_bit_message$\n"
235232
" $name$_ = value;\n"
236233
"}\n");
@@ -447,9 +444,10 @@ void ImmutableStringOneofFieldLiteGenerator::GenerateMembers(
447444
WriteFieldAccessorDocComment(printer, descriptor_, SETTER,
448445
context_->options());
449446
printer->Print(variables_,
447+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
450448
"private void ${$set$capitalized_name$$}$(\n"
451449
" java.lang.String value) {\n"
452-
"$null_check$"
450+
" value.getClass(); // minimal bytecode null check\n"
453451
" $set_oneof_case_message$;\n"
454452
" $oneof_name$_ = value;\n"
455453
"}\n");
@@ -665,18 +663,20 @@ void RepeatedImmutableStringFieldLiteGenerator::GenerateMembers(
665663
WriteFieldAccessorDocComment(printer, descriptor_, LIST_INDEXED_SETTER,
666664
context_->options());
667665
printer->Print(variables_,
666+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
668667
"private void set$capitalized_name$(\n"
669668
" int index, java.lang.String value) {\n"
670-
"$null_check$"
669+
" value.getClass(); // minimal bytecode null check\n"
671670
" ensure$capitalized_name$IsMutable();\n"
672671
" $name$_.set(index, value);\n"
673672
"}\n");
674673
WriteFieldAccessorDocComment(printer, descriptor_, LIST_ADDER,
675674
context_->options());
676675
printer->Print(variables_,
676+
"@java.lang.SuppressWarnings(\"ReturnValueIgnored\")\n"
677677
"private void add$capitalized_name$(\n"
678678
" java.lang.String value) {\n"
679-
"$null_check$"
679+
" value.getClass(); // minimal bytecode null check\n"
680680
" ensure$capitalized_name$IsMutable();\n"
681681
" $name$_.add(value);\n"
682682
"}\n");

0 commit comments

Comments
 (0)