Repository navigation
Treat fields accessed only through VarHandles or field updaters as used in UnusedVariable. - #6198
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_994507920
branch
from
October 27, 2026 17:56
6f650b0 to
e7d5963
Compare
…ed in UnusedVariable.
`UnusedVariable` only recognises a field as used when it sees a direct identifier or member-select reference to it. Fields in lock-free code are often accessed exclusively through a `VarHandle` obtained from `MethodHandles.Lookup.findVarHandle(Foo.class, "bar", ...)` (or an `Atomic*FieldUpdater`), where the only mention of the field is its name in a string literal. Such fields were reported as "never read", producing false positives on private fields and on fields of private nested classes.
When scanning for usages, recognise invocations of the `MethodHandles.Lookup` field accessors (`findVarHandle`, `findStaticVarHandle`, `findGetter`, `findSetter`, `findStaticGetter`, `findStaticSetter`) and `Atomic{Integer,Long,Reference}FieldUpdater.newUpdater`, resolve the class literal and string constant arguments to the named field, and treat that field as used. Lookups with a non-constant name or non-literal class are ignored, so the field remains subject to the check.
PiperOrigin-RevId: 1008249188
copybara-service
Bot
force-pushed
the
test_994507920
branch
from
October 27, 2026 18:25
e7d5963 to
6bbeb80
Compare
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.
Treat fields accessed only through VarHandles or field updaters as used in UnusedVariable.
UnusedVariableonly recognises a field as used when it sees a direct identifier or member-select reference to it. Fields in lock-free code are often accessed exclusively through aVarHandleobtained fromMethodHandles.Lookup.findVarHandle(Foo.class, "bar", ...)(or anAtomic*FieldUpdater), where the only mention of the field is its name in a string literal. Such fields were reported as "never read", producing false positives on private fields and on fields of private nested classes.When scanning for usages, recognise invocations of the
MethodHandles.Lookupfield accessors (findVarHandle,findStaticVarHandle,findGetter,findSetter,findStaticGetter,findStaticSetter) andAtomic{Integer,Long,Reference}FieldUpdater.newUpdater, resolve the class literal and string constant arguments to the named field, and treat that field as used. Lookups with a non-constant name or non-literal class are ignored, so the field remains subject to the check.