Skip to content

SimplifyBooleanExpression: skip nullable boolean comparisons in Kotlin#839

Merged
timtebeek merged 3 commits intomainfrom
tim/fix-nullable-boolean-simplify
Mar 24, 2026
Merged

SimplifyBooleanExpression: skip nullable boolean comparisons in Kotlin#839
timtebeek merged 3 commits intomainfrom
tim/fix-nullable-boolean-simplify

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Mar 23, 2026

Summary

  • Fixes shouldSimplifyEqualsOn override to handle nullable Boolean? comparisons, not just null-safe method calls (?.)

  • Previously only checked for IsNullSafe marker on method invocations (e.g. x?.fun() == true), missing plain nullable boolean comparisons like nullableBoolean == true

  • For non-Java source files (Kotlin), non-method-invocation expressions are now only simplified when their type is primitive boolean

  • Fixes Kotlin code with nullable fields in data classes breaks SimplifyBooleanExpression #303

Test plan

  • Existing Kotlin tests pass (regular, doNotChangeWithNullable, nullableChain, nullableVariable)
  • New test: Boolean?.toLegacyFlag() = if (this == true) "1" else "0" — preserved
  • New test: todo.completed == true with Boolean? field — preserved
  • New test: b == false with Boolean? parameter — preserved
  • New test: b != true with Boolean? parameter — preserved
  • Java SimplifyBooleanExpressionTest passes (no regression)

…s in Kotlin

The `shouldSimplifyEqualsOn` override previously only checked for the
`IsNullSafe` marker on method invocations (e.g. `x?.fun() == true`), but
missed plain nullable boolean comparisons like `nullableBoolean == true`
where `nullableBoolean` is `Boolean?`.

For non-Java source files (Kotlin), non-method-invocation expressions are
now only simplified when their type is primitive boolean, preventing
incorrect simplification of nullable Boolean comparisons.

Fixes #303
@timtebeek
Copy link
Copy Markdown
Member Author

Co-authored-by: Tim te Beek <timtebeek@gmail.com>
if (j instanceof J.MethodInvocation) {
return !j.getMarkers().findFirst(IsNullSafe.class).isPresent();
}
return super.shouldSimplifyEqualsOn(j);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@greg-at-moderne greg-at-moderne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I haven't looked at the CI failure.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Mar 24, 2026
@timtebeek timtebeek merged commit c82f961 into main Mar 24, 2026
1 of 3 checks passed
@timtebeek timtebeek deleted the tim/fix-nullable-boolean-simplify branch March 24, 2026 10:17
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Kotlin code with nullable fields in data classes breaks SimplifyBooleanExpression

2 participants