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

Specific code causes false positive of UnconditionalJumpStatementInLoop #6442

Closed
simaotwx opened this issue Aug 30, 2023 · 0 comments · Fixed by #6443
Closed

Specific code causes false positive of UnconditionalJumpStatementInLoop #6442

simaotwx opened this issue Aug 30, 2023 · 0 comments · Fixed by #6443
Labels

Comments

@simaotwx
Copy link

The following statement causes a false positive UnconditionalJumpStatementInLoop: This loop contains an unconditional jump expression which essentially renders it useless as it will exit the loop during the first iteration.:

inline fun <reified T> BlockingQueue<T>.pollEach(action: (T) -> Unit) {
    while (true) poll()?.let { action(it) } ?: break
}

Adding braces to the while statement appears to make it disappear. IntelliJ has a suggestion to remove these braces which restores the code above:

image

As far as I know both variants are semantically equivalent and only differ in syntax.
The code polls the queue until poll returns null which then breaks out of the while loop:

java.util.Queue<E>
E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
Returns:
the head of this queue, or null if this queue is empty

Expected Behavior

I expected it to not report said finding.

Observed Behavior

It reported the code mentioned above as having UnconditionalJumpStatementInLoop

Steps to Reproduce

Analyze following code:

inline fun <reified T> BlockingQueue<T>.pollEach(action: (T) -> Unit) {
    while (true) poll()?.let { action(it) } ?: break
}

making sure you have following configuration:

potential-bugs:
  active: true
  UnconditionalJumpStatementInLoop:
    active: true

Context

It's a false positive that does not require fixing.

Your Environment

  • Version of detekt used: 1.23.1
  • Version of Gradle used (if applicable): 8.1.1
  • Gradle scan link (add --scan option when running the gradle task): –
  • Operating System and version: Arch Linux (rolling)
  • Link to your project (if it's a public repository): –
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant