Error-prone: lombok causes an IndexOutOfBoundsException in UnusedVariable

Created on 26 Mar 2019  路  8Comments  路  Source: google/error-prone

Description of the problem / feature request:

./gradlew clean compileJava throws this error

Service.java:1: error: An unhandled exception was thrown by the Error Prone static analysis plugin.                                                    
package io.mine.service.grpc;
^
     Please report this at https://github.com/google/error-prone/issues/new and include the following:

     error-prone version: 2.3.3
     BugPattern: UnusedVariable
     Stack Trace:
     java.lang.IndexOutOfBoundsException
        at java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:580)
        at java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:42)
        at com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments(SuggestedFixes.java:1021)
        at com.google.errorprone.bugpatterns.UnusedVariable.buildUnusedVarFixes(UnusedVariable.java:387)
        at com.google.errorprone.bugpatterns.UnusedVariable.matchCompilationUnit(UnusedVariable.java:239)
        at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:433)
        at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:541)
        at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:150)

Feature requests: what underlying problem are you trying to solve with this feature?

none, its applied by error-prone 2.3.3

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

What version of Error Prone are you using?

errorproneJavacVersion = "9+181-r4173-1"
errorproneVersion = "2.3.2"
errorpronePluginVersion = "0.7.1"

openjdk version "1.8.0_192"

Have you found anything relevant by searching the web?

reverting back to 2.3.2 fixes the problem.
I tried to exclude the BugPattern but that does not work:

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
        options.annotationProcessorPath = configurations.errorprone
        options.errorprone.enabled = true
        options.errorprone.disableWarningsInGeneratedCode = true
        options.errorprone.excludedPaths = ".*/build/gen.*/.*"
        options.errorprone.errorproneArgs = ["-Xep:ParameterName:OFF",
                                            "Xep:UnusedVariable:OFF"]
    }
}
lombok

Most helpful comment

@rspilker I think most of the crashes are happening because the source positions for the AST nodes lombok adds are incomplete (e.g. missing end positions), or they get out of sync with the source (e.g. from compilationUnit.getSourceFile().getCharContent(...)). I think https://github.com/rzwitserloot/lombok/issues/2691 would help, although I'm not sure it's enough by itself to prevent crashes, unless you're also able to update the contents of the FileObject and adjust the positions of other AST nodes in the file?

The other thing I can think of is that if @lombok.Generated was generated by default. We could rely on it to not process some of the generated code, which would also avoid crashes and false positives.

For @lombok.Generated, I think I'm seeing cases where lombok is editing AST nodes (which then don't have end positions) outside of regions that have a @lombok.Generated annotation. Is that expected?

All 8 comments

Title says 2.3.2 but should say 2.3.3?

I am also seeing this issue on 2.3.3, fixed by reverting to 2.3.2.

I can unblock myself by adding -Xep:ParameterName:OFF as suggested in:
https://github.com/google/error-prone/issues/780#issuecomment-451590154

-Xep:ParameterName:OFF does not work for me

This works though

gradle.projectsEvaluated {
    tasks.withType(JavaCompile).configureEach {
        options.encoding = "UTF-8"
        options.annotationProcessorPath = configurations.errorprone
        options.errorprone.disableWarningsInGeneratedCode = true
        options.errorprone.excludedPaths = ".*/build/gen.*/.*"
        options.errorprone {
            disable("ParameterName")
            disable("UnusedVariable")
        }
    }
}

re: https://github.com/google/error-prone/issues/1250#issuecomment-494385041, if -Xep:ParameterName:OFF works around the crash it's a different bug: #780

Re-opening to track crashes related to lombok and UnusedVariable.

import lombok.extern.flogger.Flogger;

@Flogger
public class I1250 {}
javac   -XDcompilePolicy=simple   -processorpath lombok-1.18.16.jar:error_prone_core-2.5.1-with-dependencies.jar:dataflow-shaded-3.7.1.jar:jFormatString-3.0.0.jar   '-Xplugin:ErrorProne -Xep:UnusedVariable:ERROR -Xep:ReferenceEquality:OFF' -cp lombok-1.18.16.jar:flogger-0.5.1.jar I1250.java
import lombok.extern.flogger.Flogger;
^

     error-prone version: 2.5.1
     BugPattern: UnusedVariable
     Stack Trace:
     java.lang.IndexOutOfBoundsException: Range [54, -1) out of bounds for length 70
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:76)
    at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:295)
    at java.base/java.util.Objects.checkFromToIndex(Objects.java:385)
    at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:677)
    at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:44)
    at com.google.errorprone.VisitorState.getOffsetTokens(VisitorState.java:585)
    at com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments(SuggestedFixes.java:1547)
    at com.google.errorprone.bugpatterns.UnusedVariable.buildUnusedVarFixes(UnusedVariable.java:381)

I am a Lombok maintainer. If there's anything we can do to help, please let us know. (apart from not generating a field that is not used...)

@rspilker I think most of the crashes are happening because the source positions for the AST nodes lombok adds are incomplete (e.g. missing end positions), or they get out of sync with the source (e.g. from compilationUnit.getSourceFile().getCharContent(...)). I think https://github.com/rzwitserloot/lombok/issues/2691 would help, although I'm not sure it's enough by itself to prevent crashes, unless you're also able to update the contents of the FileObject and adjust the positions of other AST nodes in the file?

The other thing I can think of is that if @lombok.Generated was generated by default. We could rely on it to not process some of the generated code, which would also avoid crashes and false positives.

For @lombok.Generated, I think I'm seeing cases where lombok is editing AST nodes (which then don't have end positions) outside of regions that have a @lombok.Generated annotation. Is that expected?

@cushon Thanks for adding some code to ignore generated stuff, that should fix most problems. Meanwhile I started to add start and end positions for everything lombok generates. If that is finished and merged all remaining problmes should be solved.

Was this page helpful?
0 / 5 - 0 ratings