Sonar error Conditions should not unconditionally evaluate to quot;TRUEquot; or to quot;FALSEquot;(声纳错误条件不应无条件地评估为“真.或“错误;)
问题描述
我收到声纳违规:
<块引用>条件不应无条件地评估为TRUE"或FALSE""
下面的代码.
列表<媒体内容>savedList = source.getChildMediaContents();列出<媒体内容>供应商列表 = target.getChildMediaContents();//如果存在和传入都是空的如果(保存列表 == 空 && 供应商列表 == 空){返回假;}//如果一个为空,另一个不为空,则需要更新if(savedList == null && 供应商列表!= null){返回真;}if(savedList != null && supplierList == null){返回真;}在两个 if 块下面会报错
//如果一个为空,另一个不为空,则需要更新if(savedList == null && 供应商列表!= null){返回真;}if(savedList != null && supplierList == null){返回真;}if(savedList == null && supplierList == null){返回假;}if(savedList == null && 供应商列表!= null){条件
supplierList != null在达到时始终为真.由于 Java 中&&运算符的短路行为,在supplierList != null到达之前,savedList == null必须首先为真.但如果
savedList == null为真,那么我们从前面的条件知道supplierList不是null,所以这是一个没有意义的条件.另一方面,如果
savedList == null为假,然后由于短路行为,supplierList != null将不会被评估.因此,无论
savedList == null的结果如何,supplierList != null永远不会被评估,所以你可以简单地删除那个条件.if (savedList == null) {返回真;}下一步:
<块引用>if(savedList != null && supplierList == null){
感谢之前的简化,现在很明显 savedList 不能为 null.所以我们也可以去掉那个条件:
if (supplierList == null) {返回真;}简而言之,这相当于你发布的代码:
if (savedList == null && supplierList == null) {返回假;}if (savedList == null || 供应商列表 == null) {返回真;}I am getting sonar violation:
"Conditions should not unconditionally evaluate to "TRUE" or to "FALSE""
for the code below.
List<MediaContent> savedList = source.getChildMediaContents();
List<MediaContent> supplierList = target.getChildMediaContents();
// if existing and incoming both empty
if(savedList == null && supplierList == null){
return false;
}
// if one is null and other is not then update is required
if(savedList == null && supplierList != null){
return true;
}
if(savedList != null && supplierList == null){
return true;
}
Below the two if blocks it is giving an error
// if one is null and other is not then update is required
if(savedList == null && supplierList != null){
return true;
}
if(savedList != null && supplierList == null){
return true;
}
if(savedList == null && supplierList == null){ return false; } if(savedList == null && supplierList != null){
The condition supplierList != null is always true when reached.
Due to the short-circuiting behavior of the && operator in Java,
before supplierList != null is reached,
savedList == null must be true first.
But if savedList == null is true,
then we know from the previous condition that supplierList is not null, so it's a pointless condition.
On the other hand, if savedList == null is false,
then the due to the short-circuiting behavior,
the supplierList != null will not be evaluated.
Thus, regardless of the outcome of savedList == null,
supplierList != null will never be evaluated,
so you can simply remove that condition.
if (savedList == null) {
return true;
}
Next:
if(savedList != null && supplierList == null){
Thanks to the simplification earlier, now it's clear that savedList cannot be null. So we can remove that condition too:
if (supplierList == null) {
return true;
}
In short, this is equivalent to your posted code:
if (savedList == null && supplierList == null) {
return false;
}
if (savedList == null || supplierList == null) {
return true;
}
这篇关于声纳错误条件不应无条件地评估为“真".或“错误";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:声纳错误条件不应无条件地评估为“真".或“错误";
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
