2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-10-10 16:26:46 +02:00
Unnecessary imports refer to importing types that are not used or referenced anywhere in the code.
2021-08-17 16:06:47 +02:00
include::../description.adoc[]
2023-10-10 16:26:46 +02:00
=== Exceptions
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
Imports for types mentioned in KDoc are ignored.
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
== How to fix it
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
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.
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
=== Code examples
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
==== Noncompliant code example
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
[source,kotlin,diff-id=1,diff-type=noncompliant]
2023-05-25 14:18:12 +02:00
----
2023-10-10 16:26:46 +02:00
package myapp.helpers
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
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
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
object FileHelper {
fun readFirstLine(filePath: String)
= Files.readAllLines(Paths.get(filePath)).first()
}
2023-05-25 14:18:12 +02:00
----
2023-10-10 16:26:46 +02:00
==== Compliant solution
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
[source,kotlin,diff-id=1,diff-type=compliant]
----
package myapp.helpers
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
import java.io.IOException
import java.nio.file.*
2023-05-25 14:18:12 +02:00
2023-10-10 16:26:46 +02:00
object FileHelper {
fun readFirstLine(filePath: String)
= Files.readAllLines(Paths.get(filePath)).first()
2023-05-25 14:18:12 +02:00
}
----
2021-08-17 16:06:47 +02:00
2023-10-10 16:26:46 +02:00
== Resources
=== Documentation
2021-08-17 16:06:47 +02:00
2023-10-10 16:26:46 +02:00
* https://kotlinlang.org/docs/packages.html[Kotlin packages and imports]
=== Related rules
2021-08-17 16:06:47 +02:00
2023-10-10 16:26:46 +02:00
* S1144 - Unused "private" methods should be removed
* S1481 - Unused local variables should be removed
2021-08-17 16:06:47 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-10-10 16:26:46 +02:00
=== Message
* Unnecessary imports should be removed.
2021-09-20 15:38:42 +02:00
2021-08-17 16:06:47 +02:00
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-08-17 16:06:47 +02:00
endif::env-github,rspecator-view[]