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

False positive: UnconditionalJumpStatementInLoop with elvis operator ?: #2388

Closed
BraisGabin opened this issue Mar 2, 2020 · 5 comments
Closed

Comments

@BraisGabin
Copy link
Member

Expected Behavior

The elvis operator is a conditional so code like this should be accepted:

for (resource in Detektor::class.java.classLoader.getResources("META-INF/MANIFEST.MF")) {
    return runCatching { readVersion(resource) }.getOrNull() ?: continue
}

Observed Behavior

It fails

Context

We found this false positive at #2387

Your Environment

  • Version of detekt used: 1.6.0
@schalkms
Copy link
Member

schalkms commented Mar 3, 2020

Fixed by a9bb93e

@schalkms schalkms closed this as completed Mar 3, 2020
@michalsikora-silvair
Copy link

michalsikora-silvair commented Sep 28, 2021

I'm getting this in 1.18.1.

fun function(items: List<Int?>): String? {
    for (item in items) {
        val intItem = item ?: continue
        return intItem.toString()
    }
    return null
}

Unless it is different due to return in different statement. I can't inline it in the real code as there is more data processing in between.

EDIT:

Yup, seems that return in the same line fixes it. Should I open different issue?

@BraisGabin
Copy link
Member Author

Yes, please, open a new issue.

@michalsikora-silvair
Copy link

Will do. :)

@MiSikora
Copy link
Contributor

Filed in #4134.

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

5 participants