From b337c3273feb6a72d9a64b441e044be183b43d21 Mon Sep 17 00:00:00 2001 From: waterWang Date: Tue, 11 Aug 2026 10:16:44 +0800 Subject: [PATCH] fix(UnusedVariable): skip synthetic fields with no explicit source position 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/error-prone#5964 --- .../com/google/errorprone/bugpatterns/UnusedVariable.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java index dca3245af04..43f0421e2aa 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java @@ -704,6 +704,11 @@ && exemptedFieldBySuperType(getType(variableTree), state)) { if (wellKnownKeep.shouldKeep(variableTree)) { return; } + // Skip variables that don't have an explicit source position + // (e.g., Lombok-generated fields like @Slf4j's log field). + if (!hasExplicitSource(variableTree, state)) { + return; + } switch (symbol.getKind()) { case FIELD -> { // We are only interested in private fields and those which are not special.