FP on squid:S2637 when class is @NonNullByDefault and @Nullable method returns "null"

java
sonarqube

(Markus Mitterauer) #1

Versions used

  • SonarQube 6.7.3 (build 38370)
  • SonarJava 5.3
  • Java JDK 1.8.0_162 (64b)
  • annotations: org.eclipse.jdt.annotation.source_2.1.100.v20170511-1408.jar

Minimal code sample to reproduce

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
 * Sample class to produce a false positive SonarQube issue when a class is annotated with {@code @NonNullByDefault} 
 * and a method of that class then marked as {@code @Nullable} and actually returns {@code null}.  
 */
@NonNullByDefault
class SonarQubeNullableMethodFalsePositiveRepro {

    /**
     * This method produces a false positive SonarQube issue for <em>squid:S2637</em>
     * (<q>"@NonNull" values should not be set to null (squid:S2637)</q>). 
     * 
     * @param value Some value
     * @return Some result
     */
    @Nullable
    String thisMethodProducesAFalsePositive(String value) {
        if (value != null && !value.trim().isEmpty()) {
            return value;
        }
        return null; // False positive: This method's return value is marked "org.eclipse.jdt.annotation.NonNullByDefault" but null is returned
    }

}

(Andrei Epure) #2

Hi @starfuryAll

Thank you for the detailed reproducer. I tried to reproduce with the latest SonarJava (SonarJava 5.9.1) and could not.

Could you please update to the latest sonar-java plugin (5.9.1) and tell us if the issue persists? You can do that from the SonarQube Marketplace - you need to have admin rights.

Thanks
Andrei