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

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

Open
frayneposset opened this issue Aug 8, 2022 · 4 comments
Labels
language: Kotlin type: bug 🐛 Something isn't working
Projects

Comments

@frayneposset
Copy link

frayneposset commented Aug 8, 2022

Steps to reproduce

  • JaCoCo version: 0.8.8
  • Operating system: Mac OS
  • Tool integration: Gradle Kotlin build script, Kotlin 1.6.21 or 1.7.10
  • Complete executable reproducer: jacococoveragebug
  • Steps:

Please note this behaviour depends on the version of Kotlin being used. If 1.6.10 is used, Jacoco reports 100% coverage. If 1.6.21 or 1.7.10 is used Jacoco incorrectly reports a missing branch on this line return street?.house ?: throw IllegalArgumentException("unknown key $key")

data class House(val line1 : String)
data class Street(val house: House)

class Bug {

    fun mapCheck(key : String): House {
        val map = mapOf("A" to Street(House("10 London Road")))
        val street = map[key]
        return street?.house ?: throw IllegalArgumentException("unknown key $key")
    }
}

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

internal class BugTest {
    val bug = Bug()

    @Test
    fun `get key works`() {
        bug.mapCheck("A").line1 shouldBe "10 London Road"
    }

    @Test
    fun `get key throws exception`() {
        shouldThrow<IllegalArgumentException>{
            bug.mapCheck("B")}.message shouldBe "unknown key B"
    }

Expected behaviour

Should get 100% coverage on Bug class

Actual behaviour

75% coverage due to a missed branch

@frayneposset frayneposset added the type: bug 🐛 Something isn't working label Aug 8, 2022
@Godin Godin added this to Awaiting triage in Filtering via automation Aug 8, 2022
@RGU5Android
Copy link

Facing same issue with Kotlin version 1.8.0 and Jacoco version 0.8.8

@tonny1983
Copy link

Still meeting the same issue with Kotlin 1.9.10

@bergerst
Copy link

bergerst commented Nov 14, 2023

Possible duplicate of #921.

Null-checks were unoptimized because of KT-49136, this issue is tracked via KT-52472

@petarmarijanovicfive
Copy link

Still same issue in Kotlin 1.9.22 and Jacoco 0.8.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language: Kotlin type: bug 🐛 Something isn't working
Projects
Filtering
  
Awaiting triage
Development

No branches or pull requests

6 participants