Tyler R
Tyler R

Reputation: 504

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.

Upvotes: 4

Views: 1007

Answers (2)

Stephan202
Stephan202

Reputation: 61529

While not currently documented, it is possible to directly apply the suggested changes to the affected source code. One does this by passing -XepPatchLocation:IN_PLACE:

Modifying the original example, the invocation then becomes:

-XepPatchChecks:MissingOverride,DefaultCharset,DeadException
-XepPatchLocation:IN_PLACE

It is strongly recommended to use this feature only when the original file is managed by a version control system. The result can then easily be inspected using e.g. git diff and reverted using e.g. git checkout -- . .

Upvotes: 4

Andy Turner
Andy Turner

Reputation: 140318

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)

Upvotes: 4

Related Questions