@Nullable 和 SonarQube 'Conditionally executed blocks should be reachable' 警告

@Nullable and SonarQube 'Conditionally executed blocks should be reachable' warning

包裹有以下包裹-info.java:

@ParametersAreNonnullByDefault
package foo;
import javax.annotation.ParametersAreNonnullByDefault;

Class有以下方法:

private static String toIsoString(@Nullable Instant dateTime) {
  return dateTime == null ? null : dateTime.toString();
}

SonarQube(版本 6.2,SonarJava 4.14.0.11784)给出以下警告(鱿鱼:S2583):

我怎样才能让 SonarQube 相信代码实际上是正确的?

有趣的是,Idea 中的 SonarLint 插件 (3.0.0.2041) 不会生成相同的警告。

JavaDoc of @Nullable 说(强调我的)

This annotation is useful mostly for overriding a Nonnull annotation. Static analysis tools should generally treat the annotated items as though they had no annotation, unless they are configured to minimize false negatives.

相应地,SonarJava 忽略注解。

如果您想挑战在 SonarJava 中采取的操作过程,请open a thread :-)

显然,这个问题是由于我们使用了sonar-scanner而没有指定sonar.java.libraries造成的。由于它是多模块 Maven 项目,我们不清楚如何正确指定 sonar.java.libraries

来自 SonarSource 的 Nicolas Peru 建议我们应该使用 sonar maven 插件,而不是 sonar-scanner,因为该插件可以访问项目的构建类路径。确实为我们解决了这个问题。