Skip to content

Treat fields accessed only through VarHandles or field updaters as used in UnusedVariable. - #6198

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_994507920
Oct 27, 2026
Merged

copybara-service[bot] merged 1 commit into
masterfrom
test_994507920

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Treat fields accessed only through VarHandles or field updaters as used 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.

…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
copybara-service Bot merged commit 6bbeb80 into master Oct 27, 2026
8 checks passed
@copybara-service
copybara-service Bot deleted the test_994507920 branch October 27, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant