uber / NullAway

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

Validation of null in a method used in a boolean expression does not work as expected. #935

Closed pompiuses closed 6 months ago

pompiuses commented 6 months ago

Environment: NullAway version: 0.10.23 Java 21 on Mac M1

I've got the following code:

package my.package;

import org.jspecify.annotations.Nullable;

public class Demo {

    public static void main(String[] args) {
        System.out.println(isValidDisplay("test"));
    }

    static boolean isValidDisplay(@Nullable String display) {
        return isEmpty(display) || display.length() < 80; // Error: Demo.java:[12,43] [NullAway] dereferenced expression display is @Nullable
    }

    static boolean isEmpty(@Nullable String str) {
        return str == null || str.isEmpty();
    }
}

Even though null is validated in method isEmpty NullAway gives an error on display.length(). I assume this is a bug?

msridhar commented 6 months ago

Hi @pompiuses this is not a bug. NullAway deliberately does not analyze code across procedure boundaries, to preserve scalability and incrementality. We can handle this case if you add a @Contract("null -> true") annotation to isEmpty(). See the documentation and let us know if it could be clearer or more prominent.