NoSuchElementException
KT-W1033NoSuchElementException
is a runtime exception that should be thrown when the next()
method is called on an iterator and there are no more elements to iterate over. This exception serves as a signal to the caller that there are no more elements in the collection.
Use the safe cast operator (as?
) to perform a fallible cast instead of using a condition to check the type before casting. This can simplify operations by directly converting the value to the desired type. The safe cast operator also evaluates to null if the cast fails, reducing verbosity, enforcing null safety, and avoiding an extra if
condition. It is considered idiomatic Kotlin to use as?
to cast expressions.
It is important to ensure that ranges specified in for
loops are valid. Ranges that are empty can lead to unexpected behavior.
hasNext()
on an Iterator
should not have any side-effects KT-W1032The hasNext()
method in Kotlin's Iterator
interface is used to check if there are any more elements in the collection that can be iterated over. This method should have no side-effects and should not modify the underlying collection or iterator state.
finally
blocks KT-W1010Do not throw exceptions within finally
blocks. Doing so can cause exceptions to be lost.
equals
but not hashCode
KT-W1028The class overrides the equals()
method, but does not override the hashCode()
method. When a class overrides the equals()
method, it should also override the hashCode()
method to maintain the contract between these two methods. If the hashCode()
method is not overridden, it can lead to inconsistent behavior when the class is used in data structures like HashSet
or HashMap
.
Using the not-null assertion operator (!!
) along with the index operator []
or the .get()
method of a map type can result in a NullPointerException
if the key is not present in the map. This is because Map.get()
returns null
if the key is not found. By using the not-null assertion operator, you are explicitly telling the compiler that the result will never be null
, which can lead to a runtime exception if the assumption is incorrect.
GlobalScope
to launch coroutines KT-E1001The use of GlobalScope.launch
or GlobalScope.async
was detected. The use of GlobalScope
is discouraged according to Kotlin' official documentation.
as
KT-E1002Avoid casting using as
to a nullable type, as it is unsafe and may be misused as a safe cast (as? String
).
equals
should be overridden correctly KT-E1003When overriding equals()
in Kotlin, it is crucial to ensure that the method has the correct signature, much like it is in Java. In the Any
class, which is the root of the Kotlin class hierarchy, the equals()
method has the following signature:
equals
method always returns the same result KT-W1027The equals()
method is used to compare two objects for equality. However, if the implementation of the method always returns a fixed value, such as true
or false
, it fails to accurately determine if the two objects are equal. This can lead to incorrect equality comparisons and unexpected behavior in the code.
The code uses the implicit default locale for string processing. By relying on the default locale, the code becomes susceptible to variations in behavior across different systems, which can result in inconsistent and unpredictable results. It is recommended to explicitly specify the locale to ensure consistent and predictable behavior.
Avoid unconditional jumps within loops. If the loop is intended to repeat but mistakenly contains an unconditional jump, it can result in incorrect program behavior. The loop may terminate prematurely or fail to execute essential iterations, leading to incorrect output or unexpected program states.
Postfix expressions increment or decrement a value after it has been evaluated. When the result of the postfix expression is not utilized, it can lead to confusion and potential bugs.
lateinit
modifier KT-W1047The lateinit modifier in Kotlin is used to indicate that a non-null property will be initialized later, usually before its first use. While lateinit can be convenient in certain situations, it also introduces some potential issues and risks, which is why it is generally considered bad practice or discouraged in Kotlin.
Avoid throwing exceptions in non-fallible operations such as equals()
or toString()
.
Ignoring the return value of functions in Kotlin is generally discouraged because it can lead to unintended consequences and make the code harder to understand and maintain. Ignoring the return value means that valuable data might be discarded, which could be crucial for the program's logic or correctness. Besides, certain functions may return resources like file
Kotlin supports checking for equality in two ways; by comparing references, or by comparing the values stored in the references. Kotlin's ==
operator is used to compare by value and is equivalent to calling the equals()
method as defined in Java. To perform referential equality, one must use the ===
operator instead, which would correspond to the normal ==
operator from Java.
Casting a nullable type to a non-nullable type in Kotlin can be dangerous and lead to runtime exceptions if the underlying value is null
, since while nullable types explicitly allow for null
values, non-nullable types disallow null
.
Casting to a mutable collection (such as MutableList
) should be avoided as it poses inherent safety risks. A common design pattern in classes involves exposing read-only views of mutable collections as immutable collection instances to safeguard them from external mutations. Kotlin adopts this approach to prevent crashes when dealing with Lists, Maps, etc., that must