Leen Droogendijk
Leen Droogendijk

Reputation: 151

Redundant null check

Following code:

  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;
  }

gives me a "redundant null check" warning at the indicated line. "T" is a type parameter, the class definition is

public class Graph<T extends Comparable<T>> implements IGraph

Is this a bug? A known one? Or am I overlooking something. Any tips on avoiding it?

Note: this code does not make sense, it is just a small subset that reproduces the problem. Using 'first' after the loop does not remove the warning.

This is java 8, Eclipse Luna 4.4.1.

Upvotes: 3

Views: 8662

Answers (1)

Boann
Boann

Reputation: 50021

This looks like a case of Eclipse bug 467482.

Sometimes the code analysis warnings like this do have false positives, particularly in tricky loops, but a small tweak to the code can cause such false warnings to evaporate. Alternatively, decorating the method with @SuppressWarnings("null") will shut it up.

Upvotes: 6

Related Questions