Why is this an issue?

Unnecessary imports refer to importing types that are not used or referenced anywhere in the code.

Although they don’t affect the runtime behavior of the application after compilation, removing them will:

Exceptions

Imports for types mentioned in KDoc are ignored.

How to fix it

While it’s not difficult to remove these unneeded lines manually, modern code editors support the removal of every unnecessary import with a single click from every file of the project.

Code examples

Noncompliant code example

package myapp.helpers

import java.io.IOException
import java.nio.file.*
import java.nio.file.*     // Noncompliant - package is imported twice
import java.nio.*          // Noncompliant - nothing is used from that package

object FileHelper {
    fun readFirstLine(filePath: String)
        = Files.readAllLines(Paths.get(filePath)).first()
}

Compliant solution

package myapp.helpers

import java.io.IOException
import java.nio.file.*

object FileHelper {
    fun readFirstLine(filePath: String)
        = Files.readAllLines(Paths.get(filePath)).first()
}

Resources

Documentation

Related rules