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

Null if check and failing function to taken in account #943

Closed
sdeleuze opened this issue Mar 26, 2024 · 2 comments · Fixed by #946
Closed

Null if check and failing function to taken in account #943

sdeleuze opened this issue Mar 26, 2024 · 2 comments · Fixed by #946

Comments

@sdeleuze
Copy link

Code with if (obj == null) and a failing function seems not taken in account properly.

Object obj = model.get(modelName);
if (obj == null) {
	fail("Model attribute with name '" + modelName + "' is null");
}
assertTrue(obj.getClass().getName(), expectedType.isAssignableFrom(obj.getClass()));

With

@Contract("_ -> fail")
public static void fail(String message) {
	throw new AssertionError(message);
}

Generates this irrelevant error:

[NullAway] dereferenced expression obj is @Nullable
                                obj.getClass().getName(), expectedType.isAssignableFrom(obj.getClass()));
@msridhar
Copy link
Collaborator

Hi @sdeleuze thanks for the report; we can add support for this case. As a workaround, if you have a fail method that takes both a boolean and a String, that should already be supported. See this new test:

https://github.com/uber/NullAway/pull/945/files

Maybe that is not an option here for performance reasons?

@sdeleuze
Copy link
Author

Thanks, the main reason why I don't have a workaround here is that fail(String message) is part of Spring public API and I can't change it.

msridhar added a commit that referenced this issue Mar 29, 2024
Fixes #943

We extend `ContractHandler` to insert an unconditional `throw` into the
CFG when encountering a method that fails unconditionally according to
its `@Contract` annotation.
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