使用 ResponseEntity 时的 Sonarlint 误报 NullPointer 警告(S2259)
Sonarlint false positive NullPointer warning (S2259) when using ResponseEntity
对于一般的restTemplate操作,如:
ResponseEntity<ResponseVO> response = restTemplate.postForEntity(url, entity, ResponseVO.class);
if (response.getBody() != null) {
String url = response.getBody().getUrl();
我在重复使用 ResponseEntity
时收到错误的声纳警告:
A "NullPointerException" could be thrown; "getBody()" can return null. sonarlint(java:S2259)
此外,如果我重构并引入变量,则不会出现警告:
ResponseVO body = response.getBody();
if (body != null) {
String url = body.getUrl();
我不需要添加 response != null
,如果添加会有不同的(好的)警告:
Remove this expression which always evaluates to "true" [+3 locations]sonarlint(java:S2589)
是 SonarLint 错误吗?为什么它会警告 NullPointerException?
我怀疑它与 HttpEntity @Nullable
定义有关
@Nullable
public T getBody() {
中没有发现类似的问题
在 Sonar 社区提出 false-positive 并由@DamienUrruty 回答
indeed it looks like a false-positive. That said it might make the rule more complex to support this case
对于一般的restTemplate操作,如:
ResponseEntity<ResponseVO> response = restTemplate.postForEntity(url, entity, ResponseVO.class);
if (response.getBody() != null) {
String url = response.getBody().getUrl();
我在重复使用 ResponseEntity
时收到错误的声纳警告:
A "NullPointerException" could be thrown; "getBody()" can return null. sonarlint(java:S2259)
此外,如果我重构并引入变量,则不会出现警告:
ResponseVO body = response.getBody();
if (body != null) {
String url = body.getUrl();
我不需要添加 response != null
,如果添加会有不同的(好的)警告:
Remove this expression which always evaluates to "true" [+3 locations]sonarlint(java:S2589)
是 SonarLint 错误吗?为什么它会警告 NullPointerException?
我怀疑它与 HttpEntity @Nullable
定义有关
@Nullable
public T getBody() {
中没有发现类似的问题
在 Sonar 社区提出 false-positive 并由@DamienUrruty 回答
indeed it looks like a false-positive. That said it might make the rule more complex to support this case