Skip to content

Fix isSameAs(null) compile error from null reference comparisons#1038

Merged
timtebeek merged 2 commits into
mainfrom
fix/868-null-reference-equality-assertion
Jun 20, 2026
Merged

Fix isSameAs(null) compile error from null reference comparisons#1038
timtebeek merged 2 commits into
mainfrom
fix/868-null-reference-equality-assertion

Conversation

@timtebeek

@timtebeek timtebeek commented Jun 20, 2026

Copy link
Copy Markdown
Member

What

assertThat(null == a).isEqualTo(true) was being rewritten to the uncompilable assertThat(null).isSameAs(a).

The transformation comes from the external picnic AssertJObjectRulesRecipes (error-prone-support Refaster rules), wired into the Assertj composite. Its Refaster template binds the left operand of a == b as the actual value, so when the left side is the null literal you get assertThat(null), which doesn't compile. (Refaster itself avoids this via a custom heuristic that isn't ported to OpenRewrite.)

How

Adds SimplifyAssertJNullRelatedAssertion, which runs before the picnic rules in assertj.yml and converges null reference comparisons to the dedicated, more expressive assertion:

before after
assertThat(null == a).isEqualTo(true) assertThat(a).isNull()
assertThat(a == null).isTrue() assertThat(a).isNull()
assertThat(a != null).isTrue() assertThat(a).isNotNull()
assertThat(a == null).isFalse() assertThat(a).isNotNull()

It mirrors the existing SimplifyAssertJInstanceOfAssertion: matches isTrue()/isFalse() and isEqualTo(true|false) (so it fires before the picnic boolean+object rules in the same cycle), unwraps parentheses and ! negations, and only intervenes when one operand is the null literal — genuine x == y still becomes isSameAs(y) via the picnic rule.

Tests

… error

assertThat(null == a).isEqualTo(true) was converted by the picnic
AssertJObjectRulesRecipes into the uncompilable assertThat(null).isSameAs(a),
since the Refaster rule binds the left operand as the actual value.

This adds a recipe that runs before the picnic rules and converges null
reference comparisons to the dedicated assertThat(x).isNull()/isNotNull(),
mirroring SimplifyAssertJInstanceOfAssertion. Only comparisons against the
null literal are handled; genuine x == y still becomes isSameAs(y).

Fixes #868
@timtebeek timtebeek merged commit 9a8b7cb into main Jun 20, 2026
1 check passed
@timtebeek timtebeek deleted the fix/868-null-reference-equality-assertion branch June 20, 2026 13:29
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

AssertJ recipes should not change assertThat(null== a).isEqualTo(true) to assertThat(null).isSameAs(a)

1 participant