-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kotlin code with nullable fields in data classes breaks SimplifyBooleanExpression
#303
Comments
Thanks for the clear report & good seeing you're exploring OpenRewrite some more. Feel free to open a draft PR if you're exploring a fix as well. Always great to have runnable examples of what should work. |
I have quite a hard time to be honest getting the nullability information from the LST. I was hoping I could find some of it in the markers, but alas. Might this be related to openrewrite/rewrite#3221 . IIUC, with nullability information, I think the // Comparing Kotlin nullable type `?` with true/false can not be simplified,
// e.g. `X?.fun() == true` is not equivalent to `X?.fun()`
@Override
protected boolean shouldSimplifyEqualsOn(@Nullable J j) {
if (j == null) {
return true;
}
System.out.println(TreeVisitingPrinter.printTree(getCursor()));
if (j instanceof J.MethodInvocation) {
J.MethodInvocation m = (J.MethodInvocation) j;
return !m.getMarkers().findFirst(IsNullSafe.class).isPresent();
}
if (j instanceof J.FieldAccess) {
J.FieldAccess f = (J.FieldAccess) j;
return !f.getMarkers().findFirst(IsNullable.class).isPresent();
}
return true;
} If you have others ideas on how this could be solved; I'm open to suggestions! |
Had a look into this one; I think we might need to dive into rewrite-kotlin; here's a test I've prepped for FieldAccessTest there. @Test
void dataClassFieldAccessRetainsNullableMarker() {
// language=kotlin
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new KotlinIsoVisitor<>() {
@Override
public J.FieldAccess visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
if ("completed".equals(fieldAccess.getSimpleName())) {
// TODO Type is not nullable, but field is nullable
}
return super.visitFieldAccess(fieldAccess, ctx);
}
})),
kotlin(
"""
data class Todo(val completed: Boolean?)
fun main() {
val todo = Todo(null)
val isCompleted = todo.completed
}
"""
)
);
} I can't immediately see an indication that the accessed field should be of the nullable Boolean type. Perhaps we should log this as an issue there , unless I missed anything. Any quick insights @knutwannheden ? Using the above I've traced it to this point where we're not assigning the nullable Boolean type. |
What version of OpenRewrite are you using?
I am using
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?
What did you expect to see?
No changes
What did you see instead?
What is the full stack trace of any errors you encountered?
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.
The text was updated successfully, but these errors were encountered: