Skip to content

SimplifyBooleanExpression: skip nullable boolean comparisons in Kotlin#7103

Merged
jkschneider merged 1 commit intomainfrom
tim/fix-simplifyconsttif-ternary
Mar 23, 2026
Merged

SimplifyBooleanExpression: skip nullable boolean comparisons in Kotlin#7103
jkschneider merged 1 commit intomainfrom
tim/fix-simplifyconsttif-ternary

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Mar 23, 2026

Summary

  • Fixes shouldSimplifyEqualsOn in SimplifyBooleanExpressionVisitor to not simplify x == true / x != false when the source is Kotlin and x could be a nullable Boolean?

  • In Kotlin, nullableBoolean == true evaluates to false when null (safe idiom), whereas nullableBoolean directly is a type error — so the simplification changes semantics

  • Uses the same pattern as canSimplifyDoubleNegation: allow all simplifications for Java (J.CompilationUnit), restrict to primitive boolean for other languages

  • Adds Kotlin-specific tests covering nullable receiver, nullable variable, == false, and != true cases

  • Fixes Kotlin code with nullable fields in data classes breaks SimplifyBooleanExpression rewrite-static-analysis#303

Test plan

  • Existing Java tests pass (SimplifyBooleanExpressionVisitorTest)
  • Existing Groovy tests pass
  • Existing JavaScript tests pass
  • New Kotlin tests verify nullable Boolean? comparisons are preserved
  • New Kotlin test verifies literal operations (!false, || true, && false) still simplify

…s in Kotlin

`shouldSimplifyEqualsOn` now checks whether the source file is Java before
allowing `x == true` / `x != false` simplifications. For non-Java languages
(Kotlin), simplification is restricted to primitive boolean types to avoid
changing semantics of nullable Boolean comparisons like `Boolean? == true`.

Fixes openrewrite/rewrite-static-analysis#303
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