【问题标题】:Sonar Change this condition so that it does not always evaluate to "true". False positive声纳 更改此条件,使其不总是评估为“真”。假阳性
【发布时间】:2017-06-12 05:12:04
【问题描述】:

我们的声纳给出了一个Change this condition so that it does not always evaluate to "true" 问题,我认为这是一个误报

    for (Path file: stream) {
        FileDetails fileDetails = getFileDetails(path.toString() + '/' + file.getFileName().toString(), file);
        if (fileDetails != null) {
            fileList.add(fileDetails);
        }
    }
    // Process the list of non null files
    ...

稍后在类中定义的获取文件详细信息方法

private FileDetails getFileDetails(String absolutePathName, Path path) {
    FileDetails fileDetails = new FileDetails();
    fileDetails.setName(path.getFileName().toString());
    fileDetails.setAbsoluteName(absolutePathName);
    fileDetails.setDirectory(path.toFile().isDirectory());
    fileDetails.setFileStoreUri(fileStoreDetails.getUri());
    try {
        fileDetails.setSize(Files.size(path));
        fileDetails.setModifiedDate(new Date(Files.getLastModifiedTime(path).toMillis()));
    } catch (java.nio.file.AccessDeniedException e) {
        logger.info("Cannot get file details for " + path, e);
        fileDetails = null;
    } catch (IOException e) {
        // No need to throw a checked exception as we can't do anything with it.
        logger.error("Cannot get file details for " + path, e);
        throw new UncheckedIOException(e);
    }
    return fileDetails;
}

根据我的阅读,fileDetails 很可能是非空的,但在某些情况下它将是空的,因此需要检查。这里有我遗漏的技巧吗?

我们使用的是 SonarQube 6.2,带有 v4.4.0.8066 的 java 插件

【问题讨论】:

  • 很难回答,除非没有看到getFileDetails(String)方法的实现

标签: java sonarqube


【解决方案1】:

所以,java.nio.file.AccessDeniedExceptionIOException 的子类,不会直接抛出。在审查了该函数之后,我们决定为拒绝访问返回 null 是错误的做法。所以我们删除了这个捕获(保留 IOException 捕获)。这意味着该方法现在不能返回 null。正如 Sonar 告诉我们的那样,这使得 if 变得多余

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2018-05-07
    • 2018-02-10
    • 2018-08-17
    • 2016-05-14
    • 2020-01-11
    • 2018-08-01
    • 2020-06-01
    • 2021-01-05
    相关资源
    最近更新 更多