javacommand-linelinterrorprone

Can Error Prone Auto-Apply Suggested Fixes?


From what I read about Error Prone, I see that it will actually suggest fixes for style errors in your code. i.e from https://errorprone.info/docs/installation:

ERROR: example/myproject/BUILD:29:1: Java compilation in rule '//example/myproject:hello'
examples/maven/error_prone_should_flag/src/main/java/Main.java:20: error: [DeadException] Exception created but not thrown
    new Exception();
    ^
    (see http://errorprone.info/bugpattern/DeadException)
  Did you mean 'throw new Exception();'?
1 error

What I do not see, is if there is a way to auto-apply these suggested changes. I am running error-prone from the command line. Any and all help is appreciated! Let me know if I can clarify anything.


Solution

  • There is not a way to auto-apply them directly.

    However, you can get Error Prone to spit out a patch file containing the fixes. Refer to the patching documentation:

    To apply the suggested fixes for checks built in to the Error Prone compiler, you’ll add two compiler flags to your compiler invocation:

    -XepPatchChecks:MissingOverride,DefaultCharset,DeadException
    -XepPatchLocation:/full/path/to/your/source/root
    

    ...

    You can inspect the patch file directly, and apply it to your source with:

    cd /full/path/to/your/source/root
    patch -p0 -u -i error-prone.patch
    

    (Note the disclaimer about this being experimental)