简体   繁体   English

鱿鱼:S2583 –使用开关时,Sonarqube误报

[英]squid:S2583 – Sonarqube false positive when using switch

For the following contrived example (the real code actually makes sense, I promise), Sonarqube 5.3 with Java plugin 2.10 will give me the dreaded “Change this condition so that it does not always evaluate to "true"”: 对于以下人为的示例(我保证真实的代码实际上是有道理的),具有Java插件2.10的Sonarqube 5.3将给我带来可怕的“更改此条件,以便它并不总是评估为“ true””:

public String sonarLint(DayOfWeek dow) {
    boolean one = false;
    boolean two = false;
    switch (dow) {
    case MONDAY:
        one = true;
    case TUESDAY:
        two = true;
        break;
    default:
        // nothing
    }
    return one && two ? "yes" : "no";
}

As far as I can see, when dow is MONDAY , the condition is true, while it's false otherwise (IntelliJ agrees, BTW, telling me that two whill always be true when it is evaluated at all). 据我所知,当dowMONDAY ,条件为true,否则为false(IntelliJ同意,顺便说一句,告诉我,当对www求值时, two ww始终为真)。 Did I hit a bug in Sonarqube here? 我在这里打过Sonarqube的虫子吗?

Sonar should report the error for missing break statement as well. 声纳也应报告错误,以防丢失break语句。

Switch cases should end with an unconditional "break" statement (Squid:S128). 切换用例应以无条件的“ break”语句结尾 (Squid:S128)。

Once you have the break statement in both the cases, then statement will never be executed as true. 一旦在两种情况下都拥有break语句,则该语句将永远不会被执行为true。

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

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