Java 冗余空校验

Java 冗余空校验,java,eclipse,Java,Eclipse,以下代码: public void test() { @Nullable T first = null; Graph<T> cycleG = new Graph<>(); ArrayList<Graph<T>> segments = GraphFactory.makeSegments(this, cycleG); Graph<T> seg = segments.get(0); for (T

以下代码:

  public void test()
  {
    @Nullable T first = null;
    Graph<T> cycleG = new Graph<>();
    ArrayList<Graph<T>> segments = GraphFactory.makeSegments(this, cycleG);
    Graph<T> seg = segments.get(0);
    for (T v : cycleG.vertices)
    {
      if (seg.hasVertex(v))
      {
        if (first == null) // redundant null check here
        {
          first = v;
        }
      }
    }
    assert first != null;
  }
公共无效测试()
{
@可为null的T first=null;
Graph cycleG=新图形();
ArrayList segments=GraphFactory.makeSegments(this,cycleG);
图seg=段。获取(0);
对于(tV:循环顶点)
{
if(分段顶点(v))
{
if(first==null)//此处检查冗余的null
{
第一个=v;
}
}
}
先断言!=null;
}
在指示行上显示“冗余空检查”警告。“T”是一个类型参数,类定义为

public class Graph<T extends Comparable<T>> implements IGraph
公共类图实现IGraph
这是虫子吗?一个已知的?还是我忽略了什么。有什么避免的建议吗

注意:这段代码没有意义,它只是重现问题的一小部分。在循环后使用“first”不会删除警告

这是Java8,EclipseLuna 4.4.1。

这看起来像是一个例子


有时,像这样的代码分析警告确实存在误报,特别是在复杂的循环中,但是对代码的一个小调整可能会导致此类误报消失。或者,用
@SuppressWarnings(“null”)
装饰该方法将使其关闭。

您将其声明为
null
,并且在声明和检查之间没有其他内容覆盖它。您不需要检查null,因为您(您自己)已声明为null。@ochi这仅在第一次迭代中是正确的。应添加产生此警告的程序名和版本。@ochi:由于第一次为null,它将被赋值为v!这就是if的全部要点。即使它将被第二次分配——这是“运行时”逻辑。我相信引起此警告的验证器并不关心运行时。-viewen作为静态代码,由于ochi的第一条注释,null检查已经过时。谢谢,这就是我要找的。尽管这个循环并不复杂:)