BugCheckerRefactoringTestHelper file path prefixes (#248)
Error Prone Support
Error Prone Support is a Picnic-opinionated extension of Google's Error Prone. It aims to improve code quality, focussing on maintainability, consistency and avoidance of common gotchas.
Error Prone is a static analysis tool for Java that catches common programming mistakes at compile-time.
⚡ Getting started
Installation
This library is built on top of Error Prone. To use it:
-
First, follow Error Prone's installation guide.
-
Next, edit your
pom.xmlfile to add one or more Error Prone Support modules to theannotationProcessorPathsof themaven-compiler-plugin:<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <!-- Error Prone itself. --> <path> <groupId>com.google.errorprone</groupId> <artifactId>error_prone_core</artifactId> <version>${error-prone.version}</version> </path> <!-- Error Prone Support's additional bug checkers. --> <path> <groupId>tech.picnic.error-prone-support</groupId> <artifactId>error-prone-contrib</artifactId> <version>${error-prone-support.version}</version> </path> <!-- Error Prone Support's Refaster templates. --> <path> <groupId>tech.picnic.error-prone-support</groupId> <artifactId>refaster-runner</artifactId> <version>${error-prone-support.version}</version> </path> </annotationProcessorPaths> <compilerArgs> <arg> -Xplugin:ErrorProne <!-- Add other Error Prone flags here. See https://errorprone.info/docs/flags. --> </arg> <arg>-XDcompilePolicy=simple</arg> </compilerArgs> <!-- Some checks raise warnings rather than errors. --> <showWarnings>true</showWarnings> <!-- Enable this if you'd like to fail your build upon warnings. --> <!-- <failOnWarning>true</failOnWarning> --> </configuration> </plugin> </plugins> </pluginManagement> </build>
Seeing it in action
Consider the following example code:
import com.google.common.collect.ImmutableSet;
import java.math.BigDecimal;
public class Example {
static BigDecimal getNumber() {
return BigDecimal.valueOf(0);
}
public ImmutableSet<Integer> getSet() {
ImmutableSet<Integer> set = ImmutableSet.of(1);
return ImmutableSet.copyOf(set);
}
}
If the installation was successful, then building the above code with Maven should yield two compiler warnings:
$ mvn clean install
...
[INFO] -------------------------------------------------------------
[WARNING] COMPILATION WARNING :
[INFO] -------------------------------------------------------------
[WARNING] Example.java:[9,34] [tech.picnic.errorprone.refastertemplates.BigDecimalTemplates.BigDecimalZero]
Did you mean 'return BigDecimal.ZERO;'?
[WARNING] Example.java:[14,35] [IdentityConversion] This method invocation appears redundant; remove it or suppress this warning and add a comment explaining its purpose
Did you mean 'return set;' or '@SuppressWarnings("IdentityConversion") public ImmutableSet<Integer> getSet() {'?
[INFO] 2 warnings
[INFO] -------------------------------------------------------------
...
Two things are kicking in here:
- An Error Prone
BugCheckerthat flags unnecessary identity conversions. - A Refaster template capable of
rewriting expressions of the form
BigDecimal.valueOf(0)andnew BigDecimal(0)toBigDecimal.ZERO.
Be sure to check out all bug checks and refaster templates.
👷 Building
This is a Maven project, so running mvn clean install
performs a full clean build. Some relevant flags:
-Dverification.warnmakes the warnings and errors emitted by various plugins and the Java compiler non-fatal, where possible.-Dverification.skipdisables various non-essential plugins and compiles the code with minimal checks (i.e. without linting, Error Prone checks, etc.).-Dversion.error-prone=some-versionruns the build using the specified version of Error Prone. This is useful e.g. when testing a locally built Error Prone SNAPSHOT.-Perror-prone-forkruns the build using Picnic's Error Prone fork, hosted on Jitpack. This fork generally contains a few changes on top of the latest Error Prone release.-Pself-checkruns the checks defined by this project against itself. Pending a release of google/error-prone#3301, this flag must currently be used in combination with-Perror-prone-fork.
Some other commands one may find relevant:
mvn fmt:formatformats the code usinggoogle-java-format../run-mutation-tests.shruns mutation tests using PIT. The results can be reviewed by opening the respectivetarget/pit-reports/index.htmlfiles. For more information check the PIT Maven plugin../apply-error-prone-suggestions.shapplies Error Prone and Error Prone Support code suggestions to this project. Before running this command, make sure to have installed the project (mvn clean install) and make sure that the current working directory does not contain unstaged or uncommited changes.
When running the project's tests in IntelliJ IDEA, you might see the following error:
java: exporting a package from system module jdk.compiler is not allowed with --release
If this happens, go to Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler and deselect the option Use '--release' option for cross-compilation (Java 9 and later). See IDEA-288052 for details.
💡 How it works
This project provides additional BugChecker
implementations.
✍️ Contributing
Want to report or fix a bug, suggest or add a new feature, or improve the documentation? That's awesome! Please read our contribution guidelines.