Fix isSameAs(null) compile error from null reference comparisons#1038
Merged
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
assertThat(null == a).isEqualTo(true)was being rewritten to the uncompilableassertThat(null).isSameAs(a).The transformation comes from the external picnic
AssertJObjectRulesRecipes(error-prone-support Refaster rules), wired into theAssertjcomposite. Its Refaster template binds the left operand ofa == bas the actual value, so when the left side is thenullliteral you getassertThat(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 inassertj.ymland converges null reference comparisons to the dedicated, more expressive assertion: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: matchesisTrue()/isFalse()andisEqualTo(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 thenullliteral — genuinex == ystill becomesisSameAs(y)via the picnic rule.Tests
New
SimplifyAssertJNullRelatedAssertionTestcovering both operand orders,!=/isFalse,isEqualTo(boolean), parenthesized/negated, fully-qualifiedassertThat, and the no-change cases.New
@Issue(868)integration test through the fullAssertjcomposite inAssertJBestPracticesTest, plus updatedSonarDedicatedAssertionsexpectations (x == nullnow yieldsisNull()rather thanisSameAs(null)).Regenerated
recipes.csv.Fixes AssertJ recipes should not change
assertThat(null== a).isEqualTo(true)toassertThat(null).isSameAs(a)#868