From bfaa201e83be23ebacc2f2bbbbd43b82368eecec Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Mon, 27 Nov 2023 18:10:05 +0100 Subject: [PATCH] Ignore imports for `assign` in `no-unused-imports` rule (#2343) (#2382) Removing this import results in compilation error when shorthand "=" is used instead of function `assign(..)`. Closes #2343 --- .../standard/rules/NoUnusedImportsRule.kt | 4 +-- .../standard/rules/NoUnusedImportsRuleTest.kt | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRule.kt index b8a21a5b14..af73f825f8 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRule.kt @@ -298,8 +298,8 @@ public class NoUnusedImportsRule : StandardRule("no-unused-imports") { "get", "set", // invoke "invoke", - // augmented assignments - "plusAssign", "minusAssign", "timesAssign", "divAssign", "modAssign", + // (augmented) assignment + "assign", "plusAssign", "minusAssign", "timesAssign", "divAssign", "modAssign", // (in)equality "equals", // comparison diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRuleTest.kt index f75bbfffd0..79a2f6ff47 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/NoUnusedImportsRuleTest.kt @@ -856,4 +856,29 @@ class NoUnusedImportsRuleTest { """.trimIndent() noUnusedImportsRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2343 - Do not mark assign as unused import`() { + val code = + """ + package com.github.erdi.ktlint.bug + + import org.gradle.api.Plugin + import org.gradle.api.Project + import org.gradle.kotlin.dsl.assign + import javax.inject.Inject + + class ExamplePlugin @Inject constructor() : Plugin { + override fun apply(project: Project) { + project.let { + it.javaexec { + mainClass = "com.github.erdi.ktlint.bug.ExampleMainClass" + } + } + } + } + """.trimIndent() + noUnusedImportsRuleAssertThat(code) + .hasNoLintViolations() + } }