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 unused import with version 0.38.0-alpha01 #836

Closed
vRallev opened this issue Aug 18, 2020 · 4 comments · Fixed by #839
Closed

False positive unused import with version 0.38.0-alpha01 #836

vRallev opened this issue Aug 18, 2020 · 4 comments · Fixed by #839
Labels

Comments

@vRallev
Copy link

vRallev commented Aug 18, 2020

I'm testing 0.38.0-alpha01 with Kotlin 1.4.0 and see a few false positives for unused imports.

package com.squareup.ktlint.rule

import com.pinterest.ktlint.core.Rule
import com.squareup.psi.deleteFromParentWithWhitespace
import com.squareup.psi.findAnnotation
import dagger.Module
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtObjectDeclaration

internal val DAGGER_MODULE_FQ_NAME = FqName(Module::class.java.canonicalName)

/**
 * KtLint rule that flags redundant @Module annotations on companion objects.
 */
class NoModuleAnnotationOnCompanionObjects : Rule("no-module-annotation-on-companion-objects") {

  override fun visit(
    node: ASTNode,
    autoCorrect: Boolean,
    emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
  ) {
    val psi = node.psi as? KtObjectDeclaration ?: return
    if (!psi.isCompanion()) return

    val moduleAnnotation = psi.findAnnotation(DAGGER_MODULE_FQ_NAME) ?: return

    emit(
        node.startOffset,
        "Companion objects no longer need to be annotated with @Module. The " +
            "annotation is only required for the surrounding class.",
        true
    )

    if (autoCorrect) {
      moduleAnnotation.deleteFromParentWithWhitespace()
    }
  }
}
/Users/ralf/path/buildSrc/ktlint-rules/src/main/java/com/squareup/ktlint/rule/NoModuleAnnotationOnCompanionObjects.kt
(5, 1): Unused import (no-unused-imports)

The extension function is used on the object psi line 26. Please let me know if you need any further information.

@ZacSweers
Copy link

We saw this as well and can also find some examples if need be

@romtsn
Copy link
Collaborator

romtsn commented Aug 18, 2020

@ZacSweers would appreciate at least one, as the reason might differ

@shashachu
Copy link
Contributor

@vRallev just to confirm - this is code that was passing in ktlint 0.37.2?

@vRallev
Copy link
Author

vRallev commented Aug 18, 2020

I double checked and confirmed that it's passing with 0.37.2 (we have issues with the continuation indent, though).

shashachu added a commit to shashachu/ktlint that referenced this issue Aug 18, 2020
shashachu added a commit that referenced this issue Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants