小编mag*_*way的帖子

声纳不考虑注释@Contract

如果我们有代码:

@Contract("null, _, _ -> fail")
static void ifNull(Object object, ErrorType errorType, Object... args) throws ServiceException {
    if (object == null) {
        ExceptionFactory.throwServiceException(errorType, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

private boolean checkSeqValue(Sequence sequence, @Nonnull Number value) {
...
}
Run Code Online (Sandbox Code Playgroud)

和某处:

ifNull(value, ErrorType.NOT_FOUND, "Value for a sequence", "name = " + seqName);
if (checkSeqValue(sequence, value)) {
        result = value;
}
Run Code Online (Sandbox Code Playgroud)

然后 SonarLint 插件显示了一个严重错误:

鱿鱼:S2637“@NonNull”值不应设置为空

对于此调用的 checkSeqValue 参数 2 已标记javax.annotation.Nonnull但传递了 null

直接检查:

    if (value == null) {
        throw new ServiceException();
    }            
    if …
Run Code Online (Sandbox Code Playgroud)

java intellij-idea sonarqube sonarlint sonarlint-intellij

5
推荐指数
0
解决办法
363
查看次数