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

Analysis Fails On Conditional Initialization #35

Closed
naturalwarren opened this issue Oct 7, 2017 · 6 comments
Closed

Analysis Fails On Conditional Initialization #35

naturalwarren opened this issue Oct 7, 2017 · 6 comments

Comments

@naturalwarren
Copy link

naturalwarren commented Oct 7, 2017

void method(@Nullable Dep dep1, @Nullable Dep dep2) {
  Thing thing = null;
  if (dep1 == null || dep2 == null) {
    thing = new Thing();
  }
  if (dep1 == null) {
  // Passing thing as a parameter to a @NonNull API causes NullAway to report a nullability error 
  }
  if (dep2 == null) {
  // Passing thing as a parameter to a @NonNull API causes NullAway to report a nullability error 
  }
}

In the example above dep1 and dep2 are both nullable. If they're both present I want them to share the same thing. If neither are present I don't want to instantiate thing.

@msridhar
Copy link
Collaborator

msridhar commented Oct 7, 2017

Hrm, NullAway doesn't currently track this kind of conditional non-nullness. Adding it would be some work and it might significantly impact performance. Besides suppression, the one workaround I can think of is to put all the code using thing under the if condition where it is initialized. Is that a possibility?

@naturalwarren
Copy link
Author

I can do that. I try to prevent wrapping if statements as much as possible for readability but for this case the nesting is only two levels so the impact is minimal.

Since conditional nullness is some work and might impact performance could we try to detect this scenario upfront and mention in the error message NullAway can't verify the expression is safe? I'm not sure how easy this would be.

An interesting datapoint is that Intellij didn't flag passing thing as a potential NPE. This may just be because it's optimistic for expressions it doesn't/can't evaluate.

@msridhar
Copy link
Collaborator

msridhar commented Oct 8, 2017

Not sure what IntelliJ does exactly; it may well track more precise info than we do. I don't immediately see a way to figure out when this case is happening in the error message without doing the analysis itself 😄 One thing we could do eventually is to run a more precise path-sensitive analysis only if our current analysis can't prove safety. Since the common case is no error, this shouldn't affect performance too much. Still not clear it's worth the implementation effort, though. We can keep this issue open and see if this kind of thing comes up frequently. FWIW, on our internal code base I don't think this code pattern is a major cause of warning suppressions (/cc @lazaroclapp)

@SolomonSun2010
Copy link

expect to see new progress ...

@msridhar
Copy link
Collaborator

msridhar commented May 13, 2024

@SolomonSun2010 there has been more recent discussion of this issue in #98, so you can see what is happening there

@msridhar msridhar closed this as not planned Won't fix, can't repro, duplicate, stale May 13, 2024
@msridhar
Copy link
Collaborator

Duplicate of #98

@msridhar msridhar marked this as a duplicate of #98 May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants