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

assertThat(...).as(...).isNotNull() is not recognized #877

Closed
vladykin opened this issue Dec 11, 2023 · 2 comments · Fixed by #885
Closed

assertThat(...).as(...).isNotNull() is not recognized #877

vladykin opened this issue Dec 11, 2023 · 2 comments · Fixed by #885

Comments

@vladykin
Copy link

vladykin commented Dec 11, 2023

NullAway recognizes the assertThat(...).isNotNull() (#301), but fails to recognize if the description is added. Please refer to the example below. NullAway version is 0.10.18.

package com.example;

import static org.assertj.core.api.Assertions.assertThat;

import jakarta.annotation.Nullable;
import org.junit.jupiter.api.Test;

public class NullAwayTest {

  @Test
  void assertNotNull() {
    String s = getString();
    assertThat(s).isNotNull();
    assertThat(s.length()).isEqualTo(0); // NullAway doesn't complain, this is correct
  }

  @Test
  void assertNotNullAs() {
    String s = getString();
    assertThat(s).as("text").isNotNull();
    assertThat(s.length()).isEqualTo(0); // NullAway complains, this is wrong
  }

  @Test
  void assertNotNullDescribedAs() {
    String s = getString();
    assertThat(s).describedAs("text").isNotNull();
    assertThat(s.length()).isEqualTo(0); // NullAway complains, this is wrong
  }

  static @Nullable String getString() {
    return null;
  }
}
@msridhar
Copy link
Collaborator

Thanks for the report. The relevant code in NullAway is here:

// Look for statements of the form: assertThat(A).isNotNull() or
// assertThat(A).isInstanceOf(Foo.class)
// A will not be NULL after this statement.
if (methodNameUtil.isMethodIsNotNull(callee) || methodNameUtil.isMethodIsInstanceOf(callee)) {
Node receiver = node.getTarget().getReceiver();
if (receiver instanceof MethodInvocationNode) {
MethodInvocationNode receiver_method = (MethodInvocationNode) receiver;
Symbol.MethodSymbol receiver_symbol = ASTHelpers.getSymbol(receiver_method.getTree());
if (methodNameUtil.isMethodAssertThat(receiver_symbol)) {
Node arg = receiver_method.getArgument(0);
AccessPath ap = AccessPath.getAccessPathForNode(arg, state, apContext);
if (ap != null) {
bothUpdates.set(ap, NONNULL);
}
}
}
}

The logic is explicitly looking for an AST corresponding to assertThat(A).isNotNull(). Instead, we'd have to generalize this logic to recurse when appropriate to look for a more deeply-nested assertThat call.

@vladykin is it the case that any chain of method calls starting with assertThat(s) and ending in isNotNull() should make s @NonNull? Or is it only for specific intermediate method calls like as() or describedAs()?

We would welcome a PR for this one.

@vladykin
Copy link
Author

is it the case that any chain of method calls starting with assertThat(s) and ending in isNotNull() should make s @NonNull? Or is it only for specific intermediate method calls like as() or describedAs()?

AssertJ API is very rich, with a lot of things possible in the call chain between assertThat(s) and isNotNull(). I can't confidently say that any such call chain should make s @NonNull.

as() and describedAs() is the thing I noticed when enabling NullAway in our codebase. Supporting just this case would address our needs.

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