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


(Andreas Hubold) #3

Hi,

I still got no answer here, isn’t this the correct place to report bugs? Or is there something missing in the problem description?

@ganncamp, can you have a look please?

Thank you,
Andreas


(Elena Vilchik) #5

@ahubold I confirm the FP, ticket is created https://jira.sonarsource.com/browse/SONARJAVA-3035

Thanks for this feedback and sorry it took us so long!