uber/NullAway

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

pompiuses opened this issue · 1 comments

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?

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.