Skip to content

In Kotlin elvis operator that follows safe call operator should be filtered#1814

Merged
Godin merged 4 commits into
jacoco:masterfrom
Godin:kotlin_safe_call_followed_by_elvis
May 13, 2025
Merged

In Kotlin elvis operator that follows safe call operator should be filtered#1814
Godin merged 4 commits into
jacoco:masterfrom
Godin:kotlin_safe_call_followed_by_elvis

Conversation

@Godin

@Godin Godin commented Dec 12, 2024

Copy link
Copy Markdown
Member

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

@Godin Godin self-assigned this Dec 12, 2024
@Godin Godin added language: Kotlin Tickets about Kotlin language support type: enhancement labels Dec 12, 2024
@Godin
Godin force-pushed the kotlin_safe_call_followed_by_elvis branch 2 times, most recently from 2599863 to f3887ba Compare December 16, 2024 23:01
@Godin Godin added this to the 0.8.13 milestone Dec 16, 2024
@Godin
Godin force-pushed the kotlin_safe_call_followed_by_elvis branch from f3887ba to aa9fdb4 Compare January 6, 2025 10:22
@Godin Godin modified the milestones: 0.8.13, 0.8.14 Apr 1, 2025
@Godin
Godin force-pushed the kotlin_safe_call_followed_by_elvis branch 3 times, most recently from 530b10d to c89833d Compare April 15, 2025 21:15
@Godin
Godin force-pushed the kotlin_safe_call_followed_by_elvis branch from c89833d to 470e707 Compare April 15, 2025 22:48
@Godin Godin changed the title In Kotlin safe call operator followed by elvis operator should be filtered In Kotlin elvis operator that follows safe call operator should be filtered Apr 29, 2025
@Godin
Godin force-pushed the kotlin_safe_call_followed_by_elvis branch from 1320e3c to 2c44071 Compare April 29, 2025 23:04
@Godin
Godin marked this pull request as ready for review April 29, 2025 23:22
@Godin
Godin requested a review from leveretka April 29, 2025 23:22

@leveretka leveretka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

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

2 participants