声纳错误条件不应无条件地评估为“真".或“错误";
我收到声纳违规:
<块引用>条件不应无条件地评估为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;
}
相关文章