Skip to content

fix(UnusedVariable): skip synthetic fields with no explicit source position - #6018

Open
waterWang wants to merge 1 commit into
google:masterfrom
waterWang:master
Open

fix(UnusedVariable): skip synthetic fields with no explicit source position#6018
waterWang wants to merge 1 commit into
google:masterfrom
waterWang:master

Conversation

@waterWang

Copy link
Copy Markdown

Problem

When Lombok generates a field (e.g., the log field from @Slf4j), the AST node has a synthetic source position (Position.NOPOS). The UnusedVariable checker flags these fields as unused and attempts to build a fix, but replaceIncludingComments throws a SourcePositionException because the position doesn't map to any actual source code.

Stack trace: SourcePositionException: invalid source position at UnusedVariable.buildUnusedVarFixes.

Fix

Add a hasExplicitSource check in handleVariable to skip variables that don't have a valid source position. This is the same pattern already used in buildUnusedParameterFixes to handle enum constructors with bogus positions.

hasExplicitSource returns false when the start or end position is Position.NOPOS or when they are equal, which is exactly the case for Lombok-generated nodes.

References

Fixes #5964

…sition

When Lombok generates a field (e.g., the 'log' field from @slf4j),
the AST node has a synthetic source position (Position.NOPOS).
The UnusedVariable checker would flag these fields as unused and
attempt to build a fix, but replaceIncludingComments throws a
SourcePositionException because the position doesn't map to any
actual source code.

Fix: add a hasExplicitSource check in handleVariable to skip
variables that don't have a valid source position. This is the
same pattern already used in buildUnusedParameterFixes (line 610)
to handle enum constructors with bogus positions.

Fixes google#5964
@google-cla

google-cla Bot commented Aug 11, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

Lombok causes com.google.errorprone.SourcePositionException in UnusedVariable

1 participant