考虑以下方法:
void foo(Map<String, String> map) { String v = map.get("key"); if (v == null) {} System.out.println(v.toUpperCase()); }
IntelliJ(根据公司标准,坚持使用版本14)非常正确地警告我 v.toUpperCase() 可能引发NullPointerException。但使用以下代码,即没有 if 声明,没有警告:
v.toUpperCase()
if
void foo(Map<String, String> map) { String v = map.get("key"); System.out.println(v.toUpperCase()); }
是否有一些配置可以阻止它,如果我不执行 如果 先检查?或者,在更高版本中是否已“修复”此未命中警告?
如果
此处的行为在2018.1版中保持不变。
如果添加空检查,代码检查将假定变量为 Nullable 因此,警告您有关此变量的进一步未经检查的调用。
Nullable
可以通过以下方式抑制此操作 @SuppressWarnings("ConstantConditions")
@SuppressWarnings("ConstantConditions")