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;
}
这篇关于声纳错误条件不应无条件地评估为“真".或“错误";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:声纳错误条件不应无条件地评估为“真".或“错误";
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01