2023-10-10 16:26:46 +02:00

77 lines
1.6 KiB
Plaintext

== Why is this an issue?
Unnecessary imports refer to importing types that are not used or referenced anywhere in the code.
include::../description.adoc[]
=== 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
[source,kotlin,diff-id=1,diff-type=noncompliant]
----
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
[source,kotlin,diff-id=1,diff-type=compliant]
----
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
* https://kotlinlang.org/docs/packages.html[Kotlin packages and imports]
=== Related rules
* S1144 - Unused "private" methods should be removed
* S1481 - Unused local variables should be removed
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Unnecessary imports should be removed.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]