Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unused imports are not removed with .* in the end #1256

Closed
YauheniPo opened this issue Oct 21, 2021 · 23 comments · Fixed by #1258
Closed

Unused imports are not removed with .* in the end #1256

YauheniPo opened this issue Oct 21, 2021 · 23 comments · Fixed by #1258
Labels

Comments

@YauheniPo
Copy link

YauheniPo commented Oct 21, 2021

.editorconfig:

# top-most EditorConfig file
root = true

[**.{kt,kts}]

indent_size=4
insert_final_newline=true
max_line_length=off

# Comma-separated list of rules to disable
disabled_rules=no-wildcard-imports,experimental:argument-list-wrapping,filename,parameter-list-wrapping,spacing-between-declarations-with-annotations

ij_kotlin_imports_layout=*,java.**,javax.**,kotlin.**,^ # default IntelliJ IDEA style, same as alphabetical, but with "java", "javax", "kotlin" and alias imports in the end of the imports list
import io.ktor.client.*
import io.ktor.client.statement.*
import java.util.*

import java.util.* - not using

Your Environment

  • Version of ktlint used: "10.2.0"
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task):
  • Version of Gradle used (if applicable): 6.9.1
  • Operating System and version: MacOS
  • Link to your project (if it's a public repository):
@romtsn
Copy link
Collaborator

romtsn commented Oct 21, 2021

Sorry I don't get the issue - if the java.util.* import is unused, why should it not be removed?

@YauheniPo
Copy link
Author

@romtsn sorry
I have updated issue title

Unused imports are not removed with .* in the end as import java.util.* for exanple

@YauheniPo YauheniPo changed the title Don't remove unused imports with .* in the end Unused imports are not removed with .* in the end Oct 21, 2021
@paul-dingemans
Copy link
Collaborator

In your editorconfig you have explicitly disabled the rule no-wildcard-import. When you would have run with the rule enabled, you would have got output like:

Issue1256.kt:1:1: Wildcard import (cannot be auto-corrected)
Issue1256.kt:2:1: Wildcard import (cannot be auto-corrected)
Issue1256.kt:3:1: Wildcard import (cannot be auto-corrected)

Wildcard imports can just not be auto-corrected by ktlint.

@romtsn
Copy link
Collaborator

romtsn commented Oct 23, 2021

it's not about wildcard import but about the no-unused-imports rule I believe. @YauheniPo could you provide the full file? Otherwise, it's hard to reproduce and understand the problem

@YauheniPo
Copy link
Author

it's not about wildcard import but about the no-unused-imports rule I believe. @YauheniPo could you provide the full file? Otherwise, it's hard to reproduce and understand the problem

Yes @romtsn and @paul-dingemans this issue about no-unused-imports rule which should be available by default if we not disabled it.

This config is full my linter config

@romtsn
Copy link
Collaborator

romtsn commented Oct 23, 2021

@YauheniPo I'm not asking you about the config file, but about the source code file - we cannot reproduce the issue just by having an import list, we also need to see the source code and how these imports are used

paul-dingemans pushed a commit to paul-dingemans/ktlint that referenced this issue Oct 24, 2021
@paul-dingemans
Copy link
Collaborator

Actually the code provided could be a full example ;-) It would result in an empty after lint/format.

I have fixed it (#1258), using example below:

                import abc.*
                import def.*
                import def.Something

                fun foo() = def.Something()

which reports both abc.* and def.* as unnecessary imports while keeping def.Something

@YauheniPo
Copy link
Author

awesome @paul-dingemans
Thanks!

@YauheniPo
Copy link
Author

YauheniPo commented Nov 2, 2021

Issue reproduced for the current moment with the next setting

buildscript {
    dependencies {
        classpath("com.google.protobuf:protobuf-gradle-plugin:${Versions.protobufPlugin}")
        classpath("org.jlleitschuh.gradle:ktlint-gradle:${Versions.ktlintPlugin}")
    }
    repositories {
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
}

apply(plugin = "org.jlleitschuh.gradle.ktlint")

plugins {
    kotlin("jvm") version Versions.kotlin
    id("java")
}

tasks.build {
    dependsOn("ktlintFormat")
}

java.sourceCompatibility = JavaVersion.VERSION_1_8

allprojects {
    // Run ktlint check './gradlew ktlintCheck' for returning list of issues
    // Run ktlint format './gradlew ktlintFormat' for fixing issues
    // Add '--continue' option if you don't want to stop task execution in case of some of the subprojects tasks failed
    apply(plugin = "org.jlleitschuh.gradle.ktlint")

    configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
        version.set(Versions.ktlintExtension)
        debug.set(true)
        verbose.set(true)
        android.set(false)
        outputToConsole.set(true)
        outputColorName.set("RED")
        ignoreFailures.set(false)
        enableExperimentalRules.set(true)
        reporters {
            reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.HTML)
            reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.CHECKSTYLE)
        }
    }

    task("printVersion") {
        doLast {
            println(version)
        }
    }

    configurations.all {
        exclude(module = "slf4j-log4j12")
    }
}
const val ktlintPlugin = "10.2.0"
const val ktlintExtension = "0.42.1"

@romtsn @JLLeitschuh please help me with the issue

don't remove unused import
import io.ktor.client.*
import io.ktor.client.statement.*
import me.tango.client.*
import me.tango.client.ClientConf

epopovich@MacBook-Pro-Yauheni ft % ./gradlew ktlintCheck                                                          

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.9.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 3s

@YauheniPo
Copy link
Author

sorry "0.42.1" is a old version without fix

Who can build new version?

@paul-dingemans
Copy link
Collaborator

You seem to be lucky. Today a new version, 0.43.0 was released in which this fix should be included.

@abouda
Copy link

abouda commented Nov 5, 2021

In 0.43.0 it seems to be removing wild card imports even though they are needed. And I have no-wildcard-import disabled.

    import com.example.ktlinttest.Test.*
    import com.example.ktlinttest.model.*

    val testObj = TestObj
    val testData = TestData(0)

    val testClass1: TestClass1
    val testClass2: TestClass2

Both imports are flagged as "Unnecessary import" and removed.

@matiascalvo
Copy link

I'm having same issue as @abouda

@YauheniPo
Copy link
Author

You can create new issue with your problem of the klint
@abouda @matiascalvo

my issue was resolved in 0.43.0 version

@YauheniPo
Copy link
Author

#1277 from @illuzor

@YauheniPo YauheniPo reopened this Nov 9, 2021
@YauheniPo
Copy link
Author

I see the issue and for the next case

import io.ktor.client.*
import io.ktor.client.call.* - unused import
import io.ktor.client.statement.*

config

allprojects {
    // Run ktlint check './gradlew ktlintCheck' for returning list of issues
    // Run ktlint format './gradlew ktlintFormat' for fixing issues
    // Add '--continue' option if you don't want to stop task execution in case of some of the subprojects tasks failed
    apply(plugin = "org.jlleitschuh.gradle.ktlint")

    configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
        version.set(Versions.ktlintExtension)
        debug.set(true)
        verbose.set(true)
        android.set(false)
        outputToConsole.set(true)
        outputColorName.set("RED")
        ignoreFailures.set(false)
        enableExperimentalRules.set(true)
        reporters {
            reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.HTML)
            reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.CHECKSTYLE)
        }
    }

@paul-dingemans please help )

@YauheniPo
Copy link
Author

const val ktlintExtension = "0.43.0"

1 similar comment
@YauheniPo
Copy link
Author

const val ktlintExtension = "0.43.0"

@paul-dingemans
Copy link
Collaborator

Please be more clear. I do not understand why the issue is reopened. Neither do I understand a stand alone comment like this:

const val ktlintExtension = "0.43.0"

@YauheniPo
Copy link
Author

Sorry
I am using new fixed version 0.43.0, but I see invalid behavior for case

import io.ktor.client.*
import io.ktor.client.call.*     - unused import
import io.ktor.client.statement.*

unused import are not removed

above i provided my setting in gradle

@YauheniPo
Copy link
Author

please tell me which setting of klint will prohibit the use * of such imports?

@paul-dingemans
Copy link
Collaborator

I assume that the code sample is not complete as it only contains import statements. I can not analyse why the second import is not removed without the full sample.

Not removing an unused import statement might be unwanted but is not a blocking problem. I suggest you accept that behavior till it is fixed in a future release.

paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Mar 12, 2022
…lead to removal of required imports

There is no reliable way to determined whether a wildcard import is actually used
in the file. The AST does not seem to contain information about the actuall class
that an identifier refers to. It is preferred to not remove unused imports than
that needed imports are removed.

Revert pinterest#1256
Closes pinterest#1277
@paul-dingemans
Copy link
Collaborator

The solution for this issue has been reverted. It can lead to removal of wildcard imports that are actually needed to compile the code (see #1277). The downside that some imports which are not used, is considered less harmfull than removal of imports which are required. As we see no solution for how to implement this with reasonable effort, the issue will be closed without resolving it.

paul-dingemans added a commit that referenced this issue Mar 12, 2022
…emoval of required imports (#1402)

There is no reliable way to determined whether a wildcard import is actually used
in the file. The AST does not seem to contain information about the actuall class
that an identifier refers to. It is preferred to not remove unused imports than
that needed imports are removed.

Revert #1256
Closes #1277
Closes #1393
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants