Keep wire-generated Kotlin clean under extra compiler checks - #3704
Open
oldergod wants to merge 1 commit into
Open
Keep wire-generated Kotlin clean under extra compiler checks#3704oldergod wants to merge 1 commit into
oldergod wants to merge 1 commit into
Conversation
Kotlin 2.1 added extra compiler checks (-Wextra). Two of them flag wire-generated code, which users cannot edit. VARIABLE_INITIALIZER_IS_REDUNDANT: hashCode() for mutable types initialized the result variable with 0, then the next line overwrote it. The variable now starts with unknownFields.hashCode(). CAN_BE_VAL_DELAYED_INITIALIZATION: encodedSize() for a message with no fields declared a var, never updated it, and returned it. The function now returns value.unknownFields.size directly. The golden files module now compiles with extraWarnings enabled, and these warning families fail the build. REDUNDANT_VISIBILITY_MODIFIER is disabled there: KotlinPoet emits explicit visibility modifiers on purpose, because consumers with explicit API mode need them. Generated code changes shape only. There is no runtime or .api change. Fixes #3700 Fixes #3701 Co-authored-by: Benoît Quenaudon <benoit@quenaudon.com> Signed-off-by: Benoît Quenaudon <benoit@quenaudon.com>
oldergod
marked this pull request as ready for review
September 1, 2026 10:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Kotlin 2.1 added extra compiler checks (
-Wextra). Two of them flag wire-generated code. Users cannot fix generated code, so the fix belongs in wire.Fixes #3700
Fixes #3701
Changes
hashCode() for mutable types (#3701, VARIABLE_INITIALIZER_IS_REDUNDANT). The generator emitted
var result = 0, and the next line overwrote the value withunknownFields.hashCode(). The generator now initializes the variable withunknownFields.hashCode()directly. The immutable path is unchanged: itssuper.hashCodeinitializer is read by the cache check.encodedSize() for messages with no fields (#3700, CAN_BE_VAL_DELAYED_INITIALIZATION). The body degenerated to
var size = value.unknownFields.size; return size. The generator now emitsreturn value.unknownFields.sizewith no local variable. KotlinPoet renders this as an expression body.Regression gate. wire-golden-files now compiles with
extraWarningsenabled. Four diagnostics fail the build via-Xwarning-level=<NAME>:error:VARIABLE_INITIALIZER_IS_REDUNDANT,CAN_BE_VAL_DELAYED_INITIALIZATION,CAN_BE_VAL, andCAN_BE_VAL_LATEINIT. PlainallWarningsAsErrorsis too broad here: it also fails on repo-wide compiler warnings that are unrelated to the goldens, for example the deprecated-Xjvm-defaultflag spelling and the deprecated language version 2.0.REDUNDANT_VISIBILITY_MODIFIERis disabled for this module: KotlinPoet emits explicit visibility modifiers on purpose, and consumers with explicit API mode need them. Without this, it fires 984 times on the goldens.Verification
:wire-golden-files:compileKotlinfails with exactly the four expected sites (three in hashCode, one in encodedSize). With the fixed generator, the build passes.:wire-kotlin-generator:test: 75 tests, 0 failures, forced fresh with--rerun-tasks --no-build-cache.spotlessCheckandapiCheckpass. The golden files change shape only. There is no runtime or.apichange.