Sonar 出于可疑原因检测到“对局部变量的无用分配”(S1854) Eclipse SonarLint 误报“应删除未使用的分配 (java:S1854)”
温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
Using Java 17, sonar detects 3 Code Smells
- Remove this useless assignment to local variable
attrs
. - Remove this useless assignment to local variable
ac
. - Remove this useless assignment to local variable
map
.
in the following code:
Map<String, String> attrs = tag.getAttributes();
String name = attrs.get("name");
if (propertyFilter.stream().anyMatch(name::matches)) {
AttributesContainer ac = attributes.computeIfAbsent(subject, t -> {
// Add new container when first property is discovered
AttributesContainer a = new AttributesContainer(new ArrayList<>());
this.builder.attributes(a);
return a;
});
// LinkedHashMap to make it is sorted for the ease of testing
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("key", name);
map.put("value", attrs.get("value"));
ac.attributes().add(map);
}
To the best of my knowledge, map
cannot be easily avoided. The ac
and attrs
can be inlined, but the former would hurt readability and the latter will introduce code duplicity (that I would expect sonar to also complain about).
Do I miss something, are those legit smells? If not, is here a way to relax this check to focus on more trivial cases?
I prefer not to turn this check off, as it can find real smells. Also, I do not want to spoil my code with countless //NOSONAR
comments.
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2301
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(195) 评论(0)