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;
}
这篇关于声纳错误条件不应无条件地评估为“真".或“错误";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:声纳错误条件不应无条件地评估为“真".或“错误";
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
