uber / NullAway

A tool to help eliminate NullPointerExceptions (NPEs) in your Java code with low build-time overhead
MIT License
3.64k stars 296 forks source link

@NotNull annotation on a local variable is ignored #756

Closed mitasov-ra closed 1 year ago

mitasov-ra commented 1 year ago

Consider the following example:

public class Main {
  public static void main(String[] args) {
    @NotNull String a = foo(); // line 3

    bar(a); // line 5

    System.out.println(a);
  }

  @Nullable
  private static String foo() {
    return null;
  }

  @Nullable
  private static String bar(@NotNull String bar) {
    return null;
  }
}

The expected behaviour for me is NullAway raising an error on line 3, but it raises on line 5.

And if I change method bar to accept @Nullable parameter, NullAway doesn't raise anything at all.

Meta

I'm using com.uber.nullaway:nullaway:0.10.10 with errorprone 3.0.1

Here's my Gradle configuration (inside compileJava):

import net.ltgt.gradle.errorprone.CheckSeverity

plugins {
    id 'java'
    id 'net.ltgt.errorprone' version '3.0.1'
}

dependencies {
    // ...

    // nullaway
    annotationProcessor 'com.uber.nullaway:nullaway:0.10.10'
    compileOnly 'com.uber.nullaway:nullaway-annotations:0.10.10'

    errorprone "com.google.errorprone:error_prone_core:2.18.0"

}

tasks.named('compileJava', JavaCompile) {
    options.encoding 'UTF-8'
    options.errorprone {
        disableWarningsInGeneratedCode.set(true)
        // invalid null check raise error log
        check("NullAway", CheckSeverity.ERROR)
        // base-package for analyse annotated classes
        option("NullAway:AnnotatedPackages", "my.package") // ! line changed due to NDA
        // exclude generated classes with @Generated
        option("NullAway:TreatGeneratedAsUnannotated", true)
        // exclude classes with given annotation
        option("NullAway:ExcludedClassAnnotations", "lombok.Builder")
        // take assertions into consideration
        option("NullAway:AssertsEnabled", true)
        // exclude unit-tests from checking
        option("ExcludedPaths", ".*/src/test/(java|kotlin)/.*")
        // annotation is taken not from super class
        option("NullAway:ExhaustiveOverride", true)
        // libs are scanned and defaults for unannotated
        // methods is @NotNull Object method(@Nullable param)
        option("NullAway:AcknowledgeRestrictiveAnnotations", true)
        disable(
            "ImmutableEnumChecker",
        )
    }
}
mitasov-ra commented 1 year ago

Sorry, just figured out this is duplicate of #561

msridhar commented 1 year ago

Duplicate of #561