Skip to content

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

@ZzAve

Description

@ZzAve

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.27.4
  • rewrite-kotlin v1.18-0-SNAPSHOT

How are you running OpenRewrite?

I've added a test to my fork of this repository that shows the issue
00d7a3e

What is the smallest, simplest way to reproduce the problem?

data class Todo(val completed: Boolean?)
fun main() {
  val todo = Todo(null) 
  val isCompleted: Boolean = todo.completed == true
}

What did you expect to see?

No changes

data class Todo(val completed: Boolean?)
fun main() {
  val todo = Todo(null) 
  val isCompleted: Boolean = todo.completed == true
}

What did you see instead?

data class Todo(val completed: Boolean?)
fun main() {
  val todo = Todo(null)
   val isCompleted: Boolean = todo.completed
}

What is the full stack trace of any errors you encountered?


[Expected recipe to complete in 0 cycles, but took at least one more cycle. Between the last two executed cycles there were changes to "openRewriteFile.kt"] 
expected: 
  "data class Todo(val completed: Boolean?)
  fun main() {
      val todo = Todo(null)
      val isCompleted: Boolean = todo.completed == true
  }"
 but was: 
  "data class Todo(val completed: Boolean?)
  fun main() {
      val todo = Todo(null)
      val isCompleted: Boolean = todo.completed
  }"
<Click to see difference>

org.opentest4j.AssertionFailedError: [Expected recipe to complete in 0 cycles, but took at least one more cycle. Between the last two executed cycles there were changes to "openRewriteFile.kt"] 
expected: 
  "data class Todo(val completed: Boolean?)
  fun main() {
      val todo = Todo(null)
      val isCompleted: Boolean = todo.completed == true
  }"
 but was: 
  "data class Todo(val completed: Boolean?)
  fun main() {
      val todo = Todo(null)
      val isCompleted: Boolean = todo.completed
  }"
	at org.openrewrite.test.LargeSourceSetCheckingExpectedCycles.afterCycle(LargeSourceSetCheckingExpectedCycles.java:96)
	at org.openrewrite.RecipeScheduler.runRecipeCycles(RecipeScheduler.java:97)
	at org.openrewrite.RecipeScheduler.scheduleRun(RecipeScheduler.java:41)
	at org.openrewrite.Recipe.run(Recipe.java:340)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:375)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:133)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:128)
	at org.openrewrite.staticanalysis.kotlin.SimplifyBooleanExpressionTest.nullableVariable2(SimplifyBooleanExpressionTest.java:116)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Are you interested in contributing a fix to OpenRewrite?

I'm happy to fiddle around trying to see if I can fix the issue myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions