SonarJava S2583 False positive: java.lang.Closeable and nullable

java

(Andreas Hubold) #1

Hi,

Versions: SonarJava 5.9.1, SonarQube 6.7.3

The method ‘createFoo’ in the example below can return a null value. Sonar rule squid:S2583 assumes it to be non-null. It seems this is caused by the type of java.lang.Closeable

package com.example;

import edu.umd.cs.findbugs.annotations.Nullable;

import java.io.Closeable;
import java.io.IOException;

public class NullableClosable {

  private final Factory factory;

  public NullableClosable(Factory factory) {
    this.factory = factory;
  }

  boolean m() throws IOException {
    Closeable foo = factory.createFoo();
    if (foo == null) { // <-- squid:S2583 Change this condition so that it does not always evaluate to "false"
      return false;
    }
    foo.close();
    return true;
  }

  public interface Factory {
    @Nullable Closeable createFoo();
  }

}

Cheers,
Andreas


(Andreas Hubold) #2

Hi, can anyone please take a look?
Would be great if you could confirm this being a bug and provide a JIRA ticket number.

Thank you,
Andreas