-
Notifications
You must be signed in to change notification settings - Fork 41.8k
Remove unnecessary null check against configuration properties #48933
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
base: main
Are you sure you want to change the base?
Conversation
Thanks to JSpecify. Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
|
Thanks for this, @quaff. I'm curious to know how you found these. Could you let us know what you did? I'm wondering if there's something we can enable in our NullAway configuration to detect them automatically. |
I used IDEA to search the source files using regex |
|
The following configuration should handle this automatically: //JavaConventions.java
private void configureNullability(Project project) {
project.getPlugins().apply(NullabilityPlugin.class);
NullabilityPluginExtension extension = project.getExtensions().getByType(NullabilityPluginExtension.class);
String nullAwayVersion = (String) project.findProperty("nullAwayVersion");
if (nullAwayVersion != null) {
extension.getNullAwayVersion().set(nullAwayVersion);
}
String errorProneVersion = (String) project.findProperty("errorProneVersion");
if (errorProneVersion != null) {
extension.getErrorProneVersion().set(errorProneVersion);
}
project.getTasks().withType(JavaCompile.class).configureEach((javaCompile) -> {
CompileOptions options = javaCompile.getOptions();
if (options instanceof ExtensionAware extensionAware) {
extensionAware.getExtensions()
.configure(ErrorProneOptions.class,
(errorProneOptions) -> errorProneOptions.check("RedundantNullCheck", CheckSeverity.ERROR));
}
});
}
Documentation: https://errorprone.info/bugpattern/RedundantNullCheck |
|
Nice! Thanks very much, @nosan. I went looking for a NullAway option but didn't think to check Error Prone. I've opened spring-gradle-plugins/nullability-plugin#30. |
|
I think we should be a little careful with where we remove existing We certainly get away with it for configuration properties, but if these were regular classes removing a check like: if (this.properties.getCloseTimeout() != null) {
factory.setCloseTimeout((int) this.properties.getCloseTimeout().toMillis());
}Means that if someone did |
IIRC, configuration properties class is not public API, that's why this PR focus on |
|
Indeed, that's what I meant by "We certainly get away with it for configuration properties". |
|
After some experimentation with Error Prone's redundant null check I've closed spring-gradle-plugins/nullability-plugin#30. It's too blunt an instrument and doesn't work well for libraries, resulting in many false positives for redundant checks. |
|
Some of the proposed changes are completely safe because they're accessing a final field that's always initialised. |
Thanks to JSpecify.