-
Notifications
You must be signed in to change notification settings - Fork 299
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
Question: Best use of NullAway in specific scenarios #1032
Comments
Thanks for the questions!
You're correct we don't have support for this right now. We actually do support
Here I'd like to see an example to understand better. We do have logic to support |
Hi, @msridhar, thanks for the response! For 1, For 2, we do have a good number of maps that we create based on, say, enum keys, and later, we |
Honestly I'm a bit surprised :-) If
Yeah, unfortunately we don't have built-in handling for this. My best suggestion is adding a castToNonNull method and using that. If you pass the |
For 1, I said "it solved our case", but I haven't tried that in practice yet. I'll reach out to the engineer that had this issue and try it together with him 😆 I'll get back to you. For 2, yeah, that's the solution I also see! Ok, I'll be suggesting that! |
@msridhar It indeed doesn't work! If you give me a few pointers, I'd like to try implementing something like |
A quick brain dump of thoughts here.
Hope that's enough to get you started 🙂 |
@msridhar I have a simple draft ready, with a few open questions! Should we continue the conversation in the PR? |
Yup, I've commented there! |
This PR adds support for `@EnsuresNonNullIf`, following the discussion on issue #1032. This annotation allows the definition of methods that conditionally ensure that fields aren't null. An example of the annotation: ``` class Foo { @nullable Object field; @EnsuresNonNullIf("field") boolean hasField() { return field != null; } void action() { if(!hasField()) { return; } // field is non-null from this point on. } } ``` --------- Co-authored-by: Manu Sridharan <[email protected]>
I am closing this issue since it's solved by PR #1044! |
Dear, NullAway sourcerers! First of all, thanks for such a great tool.
I have two questions related to NullAway issues I've been seeing in our codebase.
First, if a class has methods like
hasX()
which checks that fieldX
(which is nullable) isn't null, NullAway doesn't understand it. And so, it complains about follow-up usage ofX
. The@Contract
annotation doesn't work in this case, as no parameters are passed tohasX
. I also don't think the annotation has support for fields in its specification, to be honest.Second, it's truly great that NullAway understands that the
get()
operation of aMap
can returnnull
. However, in some pieces of code, the algorithm ensures that theget()
will always returns a value. Extracting theget()
to a private method which does the checks or wrapping the call up withObjects.requireNonNull
works but doesn't look nice, aesthetically speaking.I wonder if there's a best way to solve these two issues without having to suppress the warning. If you see some improvement that can happen in NullAway to support these cases, I'm happy to follow your guidance and try and do it myself.
Thanks for the help!
The text was updated successfully, but these errors were encountered: