Skip to content
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

In Kotlin safe call operator followed by elvis operator should be filtered #1814

Draft
jacoco:master
base: master
Choose a base branch
from

Conversation

Godin
Copy link
Member

@Godin Dec 12, 2024

Requires


For the following Example.kt

data class B(val c: String)
fun example(b: B?): String =
    b?.c ?: ""

Kotlin compiler generates

  public static final java.lang.String example(B);
    descriptor: (LB;)Ljava/lang/String;
    flags: (0x0019) ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: dup
         2: ifnull        12
         5: invokevirtual #14                 // Method B.getC:()Ljava/lang/String;
         8: dup
         9: ifnonnull     15
        12: pop
        13: ldc           #16                 // String
        15: areturn

To be able to achieve full coverage branches of ifnull and ifnonnull instructions should be replaced as following

non-null case should be considered covered when

  • branch 1 of ifnonnull instruction was executed

There is no instruction whose execution status allows to determine that only non-null case was executed, so the current replaceBranches is not usable and that's why #1813 is required.

null case should be considered covered when

  • branch 1 of ifnull instruction was executed
  • or branch 0 of ifnonnull instruction was executed

Fixes #1351

force-pushed the kotlin_safe_call_followed_by_elvis branch from c70e9b2 to 2599863 Compare December 15, 2024 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Missing branch reported for safe call/elvis operator statement, but only only later versions of Kotlin
1 participant