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

Kotlin inline function shows branch missing error #1168

Closed
salmanjawedfolio opened this issue Mar 24, 2021 · 6 comments
Closed

Kotlin inline function shows branch missing error #1168

salmanjawedfolio opened this issue Mar 24, 2021 · 6 comments

Comments

@salmanjawedfolio
Copy link

salmanjawedfolio commented Mar 24, 2021

Hello Jacoco team

I wanted to submit an issue

JaCoCo version : Created with JaCoCo 0.8.5.201910111838
OS : Mac OS catalina 10.15.7
Tool Integration: Gradle on Android Studio

if (objResponse.result is HNPGetListTemplatesResult) {
        val templateList = (objResponse.result as HNPGetListTemplatesResult).templates (Missed here)
}

The error is shown as 1 of 2 branches missed.
Same for the following code:

fun getInstance(hldbTemplate: HLDBTemplate, templatesEventAction: HLGetTemplatesEventAction) =
                instance ?: synchronized(this) {
                    instance ?: HLTemplatesRepository(hldbTemplate, templatesEventAction).also { instance = it }} (Missed here)

This should not be reporting branches missed according to my understanding and implementation. Can you please check and get back to me. Thank you

@salmanjawedfolio salmanjawedfolio added the type: bug 🐛 Something isn't working label Mar 24, 2021
@salmanjawedfolio
Copy link
Author

Reference screen shot
Screen Shot 2021-03-24 at 2 31 45 PM

@Godin Godin removed the type: bug 🐛 Something isn't working label Mar 24, 2021
@salmanjawedfolio
Copy link
Author

@Godin hello. can you please provide details?

@Godin
Copy link
Member

Godin commented Mar 24, 2021

@salmanjawedfolio you use ?: so called "elvis operator" ( https://kotlinlang.org/docs/null-safety.html#elvis-operator )

If the expression to the left of ?: is not null, the elvis operator returns it, otherwise it returns the expression to the right

So it introduces branch - your code

fun getInstance(hldbTemplate: HLDBTemplate, templatesEventAction: HLGetTemplatesEventAction) =
                instance ?: synchronized(this) {
                    instance ?: HLTemplatesRepository(hldbTemplate, templatesEventAction).also { instance = it }} (Missed here)

is equivalent to the following

        fun getInstance() =
                if (instance == null) {
                    synchronized(this) {
                        if (instance == null) {
                            HLTemplatesRepository(hldbTemplate, templatesEventAction).also { instance = it }
                        } else {
                            instance
                        }
                    }
                } else {
                    instance
                }

where you can clearly see your uncovered branch

example

@salmanjawedfolio
Copy link
Author

@Godin what about this case
if (objResponse.result is HNPGetListTemplatesResult) {
val templateList = (objResponse.result as HNPGetListTemplatesResult).templates (Missed here)
}

@Godin
Copy link
Member

Godin commented Mar 24, 2021

what about this case

if (objResponse.result is HNPGetListTemplatesResult) {
val templateList = (objResponse.result as HNPGetListTemplatesResult).templates (Missed here)
}

probably you are using Kotlin version 1.4 or never - #1134 ,
filter for unsafe-cast operators was improved for Kotlin 1.4 and later in not yet released JaCoCo version 0.8.7 - #1143

@salmanjawedfolio
Copy link
Author

@Godin thanks a lot for the explanation. So based on my understanding the above issue will be fixed with later versions of JaCoCo right?

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

No branches or pull requests

2 participants