【问题标题】:Sonar: Null pointers should not be dereferenced声纳:不应取消引用空指针
【发布时间】:2018-03-17 08:15:55
【问题描述】:

在其他 SO 帖子 (1 & 2) 中指出了误报,其中 nulls 涉及跨职能和过程。

我觉得我的情况没有那么复杂。

    // A and B are both Double[]

    if (A.length == 1 || (B != null && B.length == 1)) {
        Double vol;
        if (A.length == 1) {
            vol = A[0];
        } else {
            vol = B[0]; // Sonar Blocker: Null pointers should not be deferenced
        }
    }

我本可以通过将 else 更改为 else if (B != null && B.length == 1) 来解决此问题,但这样做会给我带来警告“condition(B != null && B.length == 1) 始终为真”。

我在这里错过了什么吗?

【问题讨论】:

  • 您使用的是哪个版本的 SonarQube?你有哪个版本的 SonarJava 插件?
  • @Tibor SonarQube 版本 5.6.6,IntelliJ SonarLint 插件 3.0.0.2041

标签: java sonarqube


【解决方案1】:

你的代码没问题。

当B为null时,只有当A的长度为1时,控件才到达main if块内部。但当A的长度为1时,控件永远不会到达else块。

PS:我还尝试了使用 Lint 的 android studio 中的代码,但没有收到任何警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2013-02-20
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多