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

potential-bugs InvalidRange does not work in all cases #2044

Closed
kepocnhh opened this issue Oct 19, 2019 · 1 comment · Fixed by #2074
Closed

potential-bugs InvalidRange does not work in all cases #2044

kepocnhh opened this issue Oct 19, 2019 · 1 comment · Fixed by #2074
Milestone

Comments

@kepocnhh
Copy link

private fun verifyInvalidRange() {
    for (i in 2 until 1) {} // empty and detekt work
    for (i in 2 until 2) {} // empty but detekt does not work
    for (i in 2 until 3) {} // not empty
}

Documentation of Int.until(to: Int): IntRange:

If the [to] value is less than or equal to this value, then the returned range is empty.

  • 2 until 1 - 1 is less than or equal to 2
  • 2 until 2 - 2 is less than or equal to 2

Expected Behavior

The rule will work and the detekt will report an error in case of 2 until 2.

Observed Behavior

Detekt does not report an error in case of 2 until 2.

Steps to Reproduce

I use my task:

task<Detekt>("verifyQuality") {
    source = files(subprojects.withPlugin("kotlin").srcDirs("main")).asFileTree
    config = files(
        "buildSrc/src/main/resources/detekt/config/potential_bugs.yml"
    )
    reports {
        html {
            enabled = true
            destination = File(analysisQualityHtmlPath)
        }
        xml.enabled = false
        txt.enabled = false
    }
}

File potential_bugs.yml contains:

build:
  maxIssues: 0

potential-bugs:
  active: true
  InvalidRange:
    active: true

processors:
  active: false

Context

The problem is only with the until method and only for the case when both parameters are equivalent. For example, the downTo method is correctly determined by detekt. Apparently there is a problem in the code of the io.gitlab.arturbosch.detekt.rules.bugs.InvalidRange class.

private fun checkRangeTo(lower: Int, upper: Int) = lower > upper
private fun checkUntil(lower: Int, upper: Int) = lower > upper

The checks for both methods are the same. Although downTo creates an IntRange with the parameter endInclusive, and until with the parameter endInclusive-1.
In all other cases, I did not find any problems with this rule.

Environment

java 1.8.0_221
kotlin 1.3.50
detekt 1.1.1
Gradle 5.5.1
macOS Mojave Version 10.14.6 (18G87)

@schalkms
Copy link
Member

schalkms commented Nov 4, 2019

Thanks for analyzing this problem.

@arturbosch arturbosch added this to the 1.2.0 milestone Nov 5, 2019
arturbosch pushed a commit that referenced this issue Nov 5, 2019
This commit handles the case where both given values in the until
function are equal. Closes #2044

Docs for Int.until(to: Int): IntRange:
If the [to] value is less than or equal to this value,
then the returned range is empty.
smyachenkov pushed a commit to smyachenkov/detekt that referenced this issue Dec 9, 2019
This commit handles the case where both given values in the until
function are equal. Closes detekt#2044

Docs for Int.until(to: Int): IntRange:
If the [to] value is less than or equal to this value,
then the returned range is empty.
smyachenkov pushed a commit to smyachenkov/detekt that referenced this issue Dec 9, 2019
This commit handles the case where both given values in the until
function are equal. Closes detekt#2044

Docs for Int.until(to: Int): IntRange:
If the [to] value is less than or equal to this value,
then the returned range is empty.
@lock lock bot locked as resolved and limited conversation to collaborators Feb 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants