简体   繁体   English

Java :(错误?)SonarQube 6.7.1 LTS中的肯定S2637

[英]Java: (false?) positive S2637 in SonarQube 6.7.1 LTS

I am using SonarQube 6.7.1 LTS on a Java project and I analyze it using Maven sonar plugin. 我在Java项目上使用SonarQube 6.7.1 LTS,并使用Maven声纳插件对其进行了分析。

I have the following code example, which finds an S2637 issue in method testGregorianCalendar2 : 我有以下代码示例,该示例在方法testGregorianCalendar2发现一个S2637问题:

public static final @Nonnull Calendar testGregorianCalendar1() {
    return GregorianCalendar.getInstance();
}

public static final @Nonnull Calendar testGregorianCalendar2() {
    return setTimeZone2(GregorianCalendar.getInstance());
}

public static final Calendar setTimeZone2(final Calendar inputCalendar) {
    if (null == inputCalendar) {
        return null;
    }
    return inputCalendar;
}

public static final @Nonnull Calendar testGregorianCalendar3() {
    return setTimeZone3(GregorianCalendar.getInstance());
}

public static final Calendar setTimeZone3(final Calendar inputCalendar) {
    return inputCalendar;
}

In the methods testGregorianCalendar1 and testGregorianCalendar3 , that issue is not found by Sonar. 在方法testGregorianCalendar1testGregorianCalendar3 ,Sonar找不到该问题。 I think in testGregorianCalendar2 , it should also not be found, or it should be found in all 3 methods testGregorianCalendar* , because: 我认为在testGregorianCalendar2 ,也不应该找到它,或者应该在所有3种方法testGregorianCalendar*找到它,因为:

Sonar should decide for itself, if GregorianCalendar.getInstance() can return null or not. Sonar应该自行决定是否GregorianCalendar.getInstance()可以返回null。 If yes, all 3 methods can return null and Sonar should find the issue in all of these methods. 如果是,则所有3个方法都可以返回null,Sonar应该在所有这些方法中找到问题。

If no, then the method setTimeZone2 is called with a non-null value, and it will then return a non-null value, so testGregorianCalendar2 will return a non-null value. 如果为否,则使用非空值调用方法setTimeZone2 ,然后它将返回非空值,因此testGregorianCalendar2将返回非空值。

What is going wrong there? 那里出了什么问题?

I don't know for sure, but here's what I would guess is happening: setTimeZone2 has a conditional return null , therefore returning the return value from setTimeZone2 from a @Nonnull method is not allowed without a check for that null value. 我不确定,但是这是我想发生的事情: setTimeZone2有条件的return null ,因此如果不检查该null值,则不允许从@Nonnull方法返回setTimeZone2的返回值。

That is, the analyzer sees: 也就是说,分析器看到:

public static final Calendar setTimeZone2(final Calendar inputCalendar) {
    if (/* something */) {
        return null;
    }
    return /* null if inputCalendar is null */;
}

And it determines: the return value of setTimeZone2 is (potentially) @Nullable . 它确定: setTimeZone2的返回值(可能)是@Nullable

Then it sees: 然后看到:

public static final @Nonnull Calendar testGregorianCalendar2() {
    return setTimeZone2(/* something */);
}

And it sees a potentially @Nullable value being returned from a method marked @Nonnull , which produces a warning. 并且它看到一个潜在的@Nullable值从标记为@Nonnull的方法返回,从而产生警告。

You and I can reason that setTimeZone2 will never return null unless inputCalendar is null, and that inputCalendar can never be null because GregorianCalendar.getInstance() will never return null, but the analyzer probably isn't evaluating under what condition setTimeZone2 can return null, just that it can under some condition. 您和我可以setTimeZone2除非inputCalendar为null,否则inputCalendar将永远不会返回null;而由于GregorianCalendar.getInstance()将永远不会返回null,则inputCalendar永远不会为null,但是分析器可能不会评估setTimeZone2什么条件下可以返回null,只是在某些情况下可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM