Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pompiuses opened this issue Mar 22, 2024 · 1 comment

Comments

@pompiuses
Copy link

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants