pinterest/ktlint

False positive from `standard:no-unused-imports` when using `org.gradle.kotlin.dsl.assign`

erdi opened this issue · 6 comments

erdi commented

Expected Behavior

When importing org.gradle.kotlin.dsl.assign and using that extension method in the file, standard:no-unused-imports should not raise a violation.

Observed Behavior

standard:no-unused-imports raises a violation if org.gradle.kotlin.dsl.assign is imported even though the import is used in a file.

Context

Gradle 8.1 introduced simple property assignment in Kotlin DSL scripts as an experimental feature. That feature became stable in Gradle 8.4. It looks like the implementation involves a compiler plugin.

FWIW, a similar issue (in that the import was reported as unused) seems to have been fixed in IntelliJ.

As a side note, I tried disabling that rule using EOL comment on the line importing org.gradle.kotlin.dsl.assign, // ktlint-disable standard:no-unused-imports, but that had no effect, the only way I was able to suppress it was for the whole file, using @file:Suppress("ktlint:standard:no-unused-imports"). Should I open an issue for that?

Your Environment

  • Version of ktlint used: 0.49.1
  • Name and version of integration used: org.jlleitschuh.gradle.ktlint:11.6.1 Gradle plugin
  • Version of Gradle used (if applicable): 8.4
  • Operating System and version: macOS 14.1 (23B74)

When importing org.gradle.kotlin.dsl.assign and using that extension method in the file, standard:no-unused-imports should not raise a violation.

Please include a code sample that reproduces the issue.

As a side note, I tried disabling that rule using EOL comment on the line importing org.gradle.kotlin.dsl.assign, // ktlint-disable standard:no-unused-imports, but that had no effect, the only way I was able to suppress it was for the whole file, using @file:Suppress("ktlint:standard:no-unused-imports"). Should I open an issue for that?

No, you don't need to open an issue for that. The // ktlint-disable directives are deprecated in version 0.50 and removed in 1.0 in favor of the @Suppress annotation. So adding the @file:Suppress(...) was the proper way to achieve this. As the the directive is phased out, and no bugfix releases are published once a new minor or major release is published, this will not be investigated or fixed.

erdi commented

Very odd, I'm unable to reproduce this in a standalone project. What's more, in a standalone project importing org.gradle.kotlin.dsl.assign seems to be completely ignored by ktlint so even if I import it but then not use it then no standard:no-unused-imports violations are being raised... 🤔

erdi commented

I'm going to close this because this is clearly something specific to the setup of my project and I unfortunately cannot isolate it into a standalone sample even after trying various theories for over an hour. Sorry about the noise.

erdi commented

Ok, so now that I need to add more and more suppressions this started bugging me enough to attach the debugger and see why I was unable to reproduce. It turns out it was because in my reproducer sample I used <...>.assign as the package name of the class containing the false positive, so ktlint thought that assign symbol was being referenced. 🤦 After I moved it out of that package the problem is reproducible.

Reproducer sample is available at: https://github.com/erdi/ktlint-assign-bug. There are instructions for how to reproduce in the readme file.

I believe the fix will be to add# assign to NoUnusedImportsRule#OPERATOR_SET. Feels like this issue is similar to #40 and #54.

Reproducer sample is available at: https://github.com/erdi/ktlint-assign-bug. There are instructions for how to reproduce in the readme file.

Can you check whether this a public repository? The link does not work. Neither do I see another public repository in your profile that could match.

erdi commented

Apologies, I unintentionally made the repository private, my settings had to change and rather than my repositories being public on creation by default they are now private.

I've now changed that repository to be public.

Screenshot 2023-11-20 at 19 46 40