uber / uber/NullAway

Test assertion that object is equal to a non-null object isn't recognised

Open
#548 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
4.1k
Forks
370
Avg merge
1d 13h
Merged PRs (30d)
68

Description

If we have a test assertion that an object is equal to some other non-null object, NullAway doesn't treat this as if we had directly asserted that the object was non-null.

We're currently using NullAway v0.9.1.

Example class:

import javax.annotation.Nullable;
public class A {
    @Nullable
    public String getThing() {
        return "ok";
    }
}

Example test:

import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
public class TestA {
    @Test
    public void testGetThing() {
        String thing = new A().getThing();
        String expected = "ok";

        // This line implicitly shows that `thing` is not null
        assertThat(thing, is(expected));

        // Workaround is to uncomment this line
        // assertThat(thing, is(notNullValue()));

        assertThat(thing.length(), is(2));
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the shown A and TestA examples using NullAway v0.9.1, then trace how the assertion assertThat(thing, is(expected)) is handled. Done means NullAway recognizes the non-null equality assertion so the following thing.length() is accepted, while the existing notNullValue workaround remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.