Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to FallThrough to skip generated/mutated AST nodes (e.g. Lombok) #1573

Conversation

lazaroclapp
Copy link
Contributor

@lazaroclapp lazaroclapp commented Apr 15, 2020

There is a general issue with Lombok in that, by default, it modifies the AST on the fly.
In particular, this causes trouble for EP checks which query state. getSourceCode() and
try to match it to Tree objects they see. This is a hard problem to fix in general, but
this PR fixes a particular instance of this involving the FallThrough checker and
@lombok.Builder on @lombok.Singular map fields.

In particular, we skip the check whenever we see that the end position of a case statement's
Tree seems to be less than 0 (e.g. not in the source).

I tried to construct a repro/test case for this, but it's not easy to include within core/src/test
as it would require the code to be built using the Lombok annotation processor in addition to
the EP test compiler.

For reference, the code showing the issue looks something like this:

@lombok.Builder(toBuilder = true)
@lombok.Data
public class Test {
  @Singular private @Nullable Map<String, String> m;
  ...
}

Where Lombok constructs the initializer for m as something including a new switch
statement at the AST level (without matching source).

@googlebot
Copy link
Collaborator

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

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@lazaroclapp
Copy link
Contributor Author

@googlebot I signed it!

@googlebot
Copy link
Collaborator

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@cushon cushon self-assigned this May 12, 2020
kluever pushed a commit that referenced this pull request May 14, 2020
(e.g. Lombok)

Fixes #1573

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311375182
@kluever kluever mentioned this pull request May 14, 2020
kluever pushed a commit that referenced this pull request May 14, 2020
(e.g. Lombok)

Fixes #1573

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311375182
@lazaroclapp lazaroclapp deleted the lazaro_fix_fallthrough_w_lombok_builder branch May 18, 2020 20:50
j-sandy added a commit to j-sandy/orca that referenced this pull request Nov 26, 2021
While building orca with upgraded spring boot version 2.3.12, google error-prone package throwing IndexOutOfBoundException as given below:

orca/orca-api/src/main/java/com/netflix/spinnaker/orca/api/pipeline/TaskResult.java:32: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
@builder
    ^
     Please report this at https://github.com/google/error-prone/issues/new and include the following:

     error-prone version: 2.4.0
     BugPattern: FallThrough
     Stack Trace:
     java.lang.IndexOutOfBoundsException
        at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:633)
        at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:41)
        at com.google.errorprone.bugpatterns.FallThrough.matchSwitch(FallThrough.java:70)
        at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:451)
        at com.google.errorprone.scanner.ErrorProneScanner.visitSwitch(ErrorProneScanner.java:825)
        at com.google.errorprone.scanner.ErrorProneScanner.visitSwitch(ErrorProneScanner.java:152)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:1229)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)

The error is due to Lombok lib upgrade from 1.18.16 to 1.18.20 as transitive dependency of Spring boot. Similar to the issue mentioned below
google/error-prone#2575

Further investigation reveal that bug pattern "FallThrough" for lombok annotations (like @Singular) could be a false positive, because Lombok  modifies the AST on the fly causing trouble to errorprone checks, as mentioned in below issue and PR.
google/error-prone#613
google/error-prone#1573

Considering the above points, we can suppress warning of "FallThrough" bug pattern reported by errorprone for @Builder/@Singular annotations.
mergify bot pushed a commit to spinnaker/orca that referenced this pull request Nov 29, 2021
…#4194)

* Remove @Singular annotation
While building orca with upgraded spring boot version 2.3.12, google error-prone package throwing IndexOutOfBoundException as given below:

orca/orca-api/src/main/java/com/netflix/spinnaker/orca/api/pipeline/TaskResult.java:32: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
@builder
^
     Please report this at https://github.com/google/error-prone/issues/new and include the following:

     error-prone version: 2.4.0
     BugPattern: FallThrough
     Stack Trace:
     java.lang.IndexOutOfBoundsException
        at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:633)
        at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:41)
        at com.google.errorprone.bugpatterns.FallThrough.matchSwitch(FallThrough.java:70)
        at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:451)
        at com.google.errorprone.scanner.ErrorProneScanner.visitSwitch(ErrorProneScanner.java:825)
        at com.google.errorprone.scanner.ErrorProneScanner.visitSwitch(ErrorProneScanner.java:152)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:1229)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)

The error is due to Lombok lib upgrade from 1.18.16 to 1.18.20 as transitive dependency of Spring boot. Similar to the issue mentioned below
google/error-prone#2575

Fix:
To remove @Singular annotation from classes using Lombok builder pattern.

Reference:
projectlombok/lombok#2221

* Revert "Remove @Singular annotation"

This reverts commit e0d57a2.

* Suppress warning for "FallThrough" bug pattern reported by errorprone
While building orca with upgraded spring boot version 2.3.12, google error-prone package throwing IndexOutOfBoundException as given below:

orca/orca-api/src/main/java/com/netflix/spinnaker/orca/api/pipeline/TaskResult.java:32: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
@builder
    ^
     Please report this at https://github.com/google/error-prone/issues/new and include the following:

     error-prone version: 2.4.0
     BugPattern: FallThrough
     Stack Trace:
     java.lang.IndexOutOfBoundsException
        at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:633)
        at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:41)
        at com.google.errorprone.bugpatterns.FallThrough.matchSwitch(FallThrough.java:70)
        at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:451)
        at com.google.errorprone.scanner.ErrorProneScanner.visitSwitch(ErrorProneScanner.java:825)
        at com.google.errorprone.scanner.ErrorProneScanner.visitSwitch(ErrorProneScanner.java:152)
        at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitch.accept(JCTree.java:1229)
        at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)

The error is due to Lombok lib upgrade from 1.18.16 to 1.18.20 as transitive dependency of Spring boot. Similar to the issue mentioned below
google/error-prone#2575

Further investigation reveal that bug pattern "FallThrough" for lombok annotations (like @Singular) could be a false positive, because Lombok  modifies the AST on the fly causing trouble to errorprone checks, as mentioned in below issue and PR.
google/error-prone#613
google/error-prone#1573

Considering the above points, we can suppress warning of "FallThrough" bug pattern reported by errorprone for @Builder/@Singular annotations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants