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 from UnusedImports on org.gradle.kotlin.dsl.provideDelegate #1608

Closed
alphaho opened this issue Apr 23, 2019 · 6 comments · Fixed by #1632
Closed

False positive from UnusedImports on org.gradle.kotlin.dsl.provideDelegate #1608

alphaho opened this issue Apr 23, 2019 · 6 comments · Fixed by #1632

Comments

@alphaho
Copy link

alphaho commented Apr 23, 2019

Hi,

I've found that when I'm writing the gradle buildSrc using kotlin dsl, and use the kotlin delegation, I would need to import org.gradle.kotlin.dsl.provideDelegate. But Detekt would give me an UnusedImports error in this case.

For example, with the following code, detekt task would fail:

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.WriteProperties
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.provideDelegate  // got UnusedImports from this line
import org.gradle.kotlin.dsl.registering

class DumpVersionProperties : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            val dumpVersionProperties by tasks.registering(WriteProperties::class) {
                setProperties(mapOf("version" to "1.2.3"))
                outputFile = rootDir.resolve("version.properties")
            }

        }
    }
}

Expected Behavior

It should not report UnusedImports if I do use it for delegation.

Observed Behavior

Detekt task fails and report there's an UnusedImports error.

Your Environment

  • Version of detekt used: 1.0.0-RC14
  • Version of Gradle used (if applicable): 5.4
  • Operating System and version: Windows 10
@3flex
Copy link
Member

3flex commented Apr 24, 2019

This is interesting, because provideDelegate is not used in the file, but still has to be imported otherwise build fails. I feel that detekt's behaviour is correct and it's only flagging this because of an issue either in Gradle Kotlin DSL or Kotlin itself, because I don't know expect this implementation detail to be exposed here.

See KT-22101 and gradle/kotlin-dsl-samples#564

Not sure what best thing to do is, since trying to whitelist this case in detekt doesn't seem the right approach.

@alphaho
Copy link
Author

alphaho commented Apr 24, 2019

Hmm, that makes sense to me.

But looking at the delegated properties doc, this imported function seems to be an overloaded operator. So when we use the delegated property in scope, it should be considered as being in used theoretically.
But not sure whether we can tell that difference or not from Detekt's perspective.

@3flex
Copy link
Member

3flex commented May 1, 2019

Sorry, haven't had time to look at this more closely.

Can you see what ktlint does here? It has a similar rule.

@3flex
Copy link
Member

3flex commented May 1, 2019

Couldn't help myself - there's a whitelist of all the operators in the rule in detekt, just need to add provideDelegate to it. I'll submit a PR shortly unless someone beats me to it!

@alphaho
Copy link
Author

alphaho commented May 5, 2019

Sounds good to me

@alphaho
Copy link
Author

alphaho commented May 5, 2019

have tried running ktlint, it would also report provideDelegate as Unused import

@arturbosch arturbosch added this to the RC15 milestone May 16, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 24, 2019
@3flex 3flex removed their assignment Aug 29, 2019
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