Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.kotlin.marker.IsNullSafe;

import java.util.Set;
Expand Down Expand Up @@ -52,10 +55,21 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

// Comparing Kotlin nullable type `?` with true/false can not be simplified,
// e.g. `X?.fun() == true` is not equivalent to `X?.fun()`
// and `nullableBoolean == true` is not equivalent to `nullableBoolean`
@Override
protected boolean shouldSimplifyEqualsOn(@Nullable J j) {
return !(j instanceof J.MethodInvocation) ||
!j.getMarkers().findFirst(IsNullSafe.class).isPresent();
if (j instanceof J.MethodInvocation) {
return !j.getMarkers().findFirst(IsNullSafe.class).isPresent();
}
// For non-Java source files (e.g. Kotlin), only simplify when the
// expression type is primitive boolean to avoid changing semantics
// of nullable Boolean comparisons like `Boolean? == true`.
// Once the parent class includes this logic, this block can be
// replaced with just `super.shouldSimplifyEqualsOn(j)`.
if (!(getCursor().firstEnclosing(SourceFile.class) instanceof J.CompilationUnit)) {
return j instanceof Expression && ((Expression) j).getType() == JavaType.Primitive.Boolean;
}
Comment thread
timtebeek marked this conversation as resolved.
Outdated
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.

}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.staticanalysis.SimplifyBooleanExpression;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -111,4 +112,60 @@ fun main() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/303")
@Test
void nullableBooleanReceiverEqualsTrue() {
rewriteRun(
kotlin(
"""
fun Boolean?.toLegacyFlag() = if (this == true) "1" else "0"
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/303")
@Test
void nullableBooleanFieldEqualsTrue() {
rewriteRun(
kotlin(
"""
data class Todo(val completed: Boolean?)
fun main() {
val todo = Todo(null)
val isCompleted: Boolean = todo.completed == true
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/303")
@Test
void nullableBooleanParameterEqualsFalse() {
rewriteRun(
kotlin(
"""
fun check(b: Boolean?) {
if (b == false) println("false or null")
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/303")
@Test
void nullableBooleanParameterNotEqualTrue() {
rewriteRun(
kotlin(
"""
fun check(b: Boolean?) {
if (b != true) println("not true")
}
"""
)
);
}
}
Loading