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 switch-case with pattern matching does not work as expected. #927

Closed
pompiuses opened this issue Mar 5, 2024 · 1 comment · Fixed by #928
Closed

Comments

@pompiuses
Copy link

Environment:
NullAway version: 0.10.23
Java 21 on Mac M1

I've got the following code:

public class OnsetValidator {
    public static Optional<FhirProfileValidationError> validateOnset(@Nullable Type onset) {
        return switch (onset) {
            case null -> Optional.empty(); // Null is validated here.
            case DateTimeType d -> validateOnsetDate(d);
            case Age a -> validateOnsetAge(a);
            // NullAway complains in default: OnsetValidator.java:[17,97] [NullAway] dereferenced expression onset is @Nullable
            default -> Optional.of(new FhirProfileValidationError("Invalid onset type: " + onset.getClass().getName()));
        };
    }

    private static Optional<FhirProfileValidationError> validateOnsetAge(Age a) {
        return Optional.empty(); // Some validation.
    }

    private static Optional<FhirProfileValidationError> validateOnsetDate(DateTimeType d) {
        return Optional.empty(); // Some validation.
    }
}

Even though null is validated in the switch, NullAway gives an error for the default case. I assume this is a bug?

@msridhar
Copy link
Collaborator

msridhar commented Mar 6, 2024

Yes I believe there is a bug here. What should happen is that since there is a case null, NullAway should learn that onset cannot be null in the default case.

I'll look into this, but it's possible a fix is blocked on google/error-prone#4119.

msridhar added a commit that referenced this issue Mar 19, 2024
Fixes #927. We also extract some common logic in
`AccessPathNullnessPropagation` around handling equality comparisons to
avoid duplication.
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

Successfully merging a pull request may close this issue.

2 participants