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

-XepDisableWarningsInGeneratedCode ignored by some bug checkers #1165

Closed
Stephan202 opened this issue Nov 4, 2018 · 1 comment · Fixed by #1216
Closed

-XepDisableWarningsInGeneratedCode ignored by some bug checkers #1165

Stephan202 opened this issue Nov 4, 2018 · 1 comment · Fixed by #1216

Comments

@Stephan202
Copy link
Contributor

The -XepDisableWarningsInGeneratedCode flag suppresses warnings inside classes annotated with @Generated. However, this logic has no effect on CompilationUnitTreeMatchers, as a CompilationUnitTree encompasses one or more classes.

For example, consider the following class:

import javax.annotation.processing.Generated;
import java.util.Map;

@Generated("foo")
class Dummy {
  private String prop;

  Dummy(String prop) {
    this.prop = prop;
  }
}

Compile this code as follows:

javac \
  -XDcompilePolicy=simple \
  -processorpath error_prone_core-2.3.2-with-dependencies.jar \
  '-Xplugin:ErrorProne -XepDisableAllChecks -XepDisableWarningsInGeneratedCode -Xep:FieldCanBeFinal:WARN -Xep:RemoveUnusedImports:WARN' \
  Dummy.java

Despite the @Generated annotation, two warnings are emitted:

Dummy.java:6: warning: [FieldCanBeFinal] This field is only assigned during initialization; consider making it final
  private String prop;
                 ^
    (see https://errorprone.info/bugpattern/FieldCanBeFinal)
  Did you mean 'private final String prop;'?
Dummy.java:2: warning: [RemoveUnusedImports] Unused imports
import java.util.Map;
^
    (see https://errorprone.info/bugpattern/RemoveUnusedImports)
  Did you mean to remove this line?
2 warnings

One possible solution would be to descend into CompilationUnitTrees to look for @Generated type declarations, and skip analysis if any are found. I wonder what the Error Prone team thinks of this approach. Maybe I'm overlooking a much more elegant and/or efficient solution.

NB: annotating a whole package as @Generated may be a workaround for this issue (didn't check), but such a workaround would in any case not be suitable for annotation processors which generate code in the same package as their "source type", like Google Auto Value or Immutables.

@dclements
Copy link
Member

Same problem but with MethodCanBeStatic.

ronshapiro pushed a commit that referenced this issue Jan 18, 2019
…l classes when visiting CompilationUnitTrees.

This stops CompilationUnitTreeMatchers from firing on generated code when -XepDisableWarningsInGeneratedCode is passed.

Fixes #1165

RELNOTES: Suppress checks thoroughly when -XepDisableWarningsInGeneratedCode is passed.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=229526286
@ronshapiro ronshapiro mentioned this issue Jan 18, 2019
eaftan pushed a commit that referenced this issue Jan 18, 2019
…l classes when visiting CompilationUnitTrees.

This stops CompilationUnitTreeMatchers from firing on generated code when -XepDisableWarningsInGeneratedCode is passed.

Fixes #1165

RELNOTES: Suppress checks thoroughly when -XepDisableWarningsInGeneratedCode is passed.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=229526286
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 a pull request may close this issue.

2 participants