【问题标题】:Sonarlint false positive NullPointer warning (S2259) when using ResponseEntity使用 ResponseEntity 时 Sonarlint 误报 NullPointer 警告 (S2259)
【发布时间】:2022-01-22 22:16:18
【问题描述】:

对于一般的restTemplate操作,如:

ResponseEntity<ResponseVO> response = restTemplate.postForEntity(url, entity, ResponseVO.class);
if (response.getBody() != null) {
    String url = response.getBody().getUrl();

重复使用 ResponseEntity 时,我收到了错误的声纳警告:

A "NullPointerException" could be thrown; "getBody()" can return null. sonarlint(java:S2259)

另外,如果我重构并引入一个变量,则不会出现警告:

ResponseVO body = response.getBody();
if (body != null) {
    String url = body.getUrl();

我不需要添加response != null,如果添加会有不同的(ok)警告:

Remove this expression which always evaluates to "true" [+3 locations]sonarlint(java:S2589)

是 SonarLint 错误吗?为什么它会警告 NullPointerException?

我怀疑它与 HttpEntity @Nullable 定义有关

@Nullable
public T getBody() {

我在jira.sonarsource没有发现类似的问题

【问题讨论】:

  • 好吧,至少,如果结果ResponseVO 是可变的并且某些其他线程更改了它的值,那么对getBody() 的两个后续调用可能返回不同的结果。如果您先将其存储在局部变量中,这是不可能的。但是,我不知道这是否是 SonarLint 在这里发出警告的原因。
  • @MCEmperor 我不认为它与 HttpEntity 的private final T body相关

标签: java nullpointerexception sonarqube nullable sonarlint


【解决方案1】:

在 Sonar 社区提出 false-positive 并由 @DamienUrruty 回答

确实,它看起来像是误报。也就是说,支持这种情况可能会使规则更加复杂

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 2016-10-09
    • 2017-05-17
    • 2021-06-29
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2022-01-14
    • 2017-03-24
    相关资源
    最近更新 更多