Kotlin

Kotlin

Made by DeepSource
Iterator implementation does not throw NoSuchElementException KT-W1033
Bug risk
Major
Autofix

NoSuchElementException 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.

Checked cast can be replaced with a safe cast KT-E1004
Bug risk
Major

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.

The range specified for iteration is empty KT-W1031
Bug risk
Major

It is important to ensure that ranges specified in for loops are valid. Ranges that are empty can lead to unexpected behavior.

Calling hasNext() on an Iterator should not have any side-effects KT-W1032
Bug risk
Major

The 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.

Avoid throwing exceptions within finally blocks KT-W1010
Bug risk
Major

Do not throw exceptions within finally blocks. Doing so can cause exceptions to be lost.

The class overrides equals but not hashCode KT-W1028
Bug risk
Major

The 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.

Map values fetched with not-null assertion operator KT-W1034
Bug risk
Major

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.

Avoid using GlobalScope to launch coroutines KT-E1001
Bug risk
Major

The use of GlobalScope.launch or GlobalScope.async was detected. The use of GlobalScope is discouraged according to Kotlin' official documentation.

Avoid casting to nullable types using as KT-E1002
Bug risk
Major

Avoid 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-E1003
Bug risk
Major

When 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-W1027
Bug risk
Major

The 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.

Implicit default locale used for string processing KT-W1030
Bug risk
Minor

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.

Loop with unconditional jump statements KT-W1035
Bug risk
Major

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.

The result of increment/decrement operation is unused KT-W1036
Bug risk
Major

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.

Found usages of lateinit modifier KT-W1047
Bug risk
Major

The 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.

Method should not throw exceptions KT-W1056
Bug risk
Major

Avoid throwing exceptions in non-fallible operations such as equals() or toString().

Return values must not be ignored KT-E1008
Bug risk
Minor

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

Avoid referential equality test for strings KT-E1005
Bug risk
Minor

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.

Avoid casting nullable types to non-null types KT-E1006
Bug risk
Major

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.

Avoid casting immutable collection types to mutable collection types KT-E1007
Bug risk
Major

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