2017-09-19 97 views
0

有谁知道为什么SonarQube检测到违反规则“在以下示例中应该可以访问有条件执行的块”(squid:S2583)?这是否为假?Sonar似乎忽略在catch块中设置的变量

在这段Java代码中,读取文件并在读取输入流的过程中可能会发生EOFException(或其中多个)。因此,异常被捕获并处理,并设置一个标志以记住它发生了。然而,声纳不考虑行exHappened = true;在第一个catch块,并声称变量始终是false

public static boolean doSomething() { 
    boolean exHappened = false; 
    try (DataInputStream s = new DataInputStream(new FileInputStream("test"))) { 
     LOGGER.info("Doing something..."); 
    } 
    catch (EOFException eof) { // this Exception can definitely happen 
     exHappened = true; 
    } 
    catch (IOException io) { 
     LOGGER.error("sorry", io); 
    } 

    if (exHappened) {   // Sonar thinks this condition is always false 
     return false; 
    } 
    else { 
     return true; 
    } 
} 

为了使它显得更加清晰,增加throw new EOFException()try { },那么情况将总是是真实的和声纳仍然声称,它总是假的... ...

(我使用SonarQube 5.6.6和SonarJava插件4.13.0.11627)

+1

据https://www.sonarqube.org/community/feedback/,SonarQube谷歌集团是一个更好的地方报告错误,他们的JIRA可能最好的。 –

+0

你说得对。下次我会发布有关SonarQube Google Group中可能出现误报的问题(顺便提一下,他们的JIRA是只读的)。 – dokaspar

+0

您可以将问题发布到Google群组或这里,这两个渠道都由社区进行监控。您正在使用的SonarJava插件和SonarQube的版本是什么? –

回答

1

这似乎是在catch块期间如何处理的问题数据flo在SonarJava中进行分析。没有考虑在被调用方法的throws声明中声明的异常的catch块捕获子类型,因此引擎永远不会看到该变量的赋值。

我创建以下票来解决这个问题https://jira.sonarsource.com/browse/SONARJAVA-2483