Kotlin

Kotlin

Made by DeepSource
Unreachable code can be safely removed KT-W1065
Anti-pattern
Major
Autofix

Unreachable code should be removed, as it serves no purpose. Such code is typically a result of incorrect or unintended control flow in code. It is thus important to correctly structure your code and ensure that all code-paths are reachable and can be executed.

The property can be declared as const KT-P1001
Performance
Major
Autofix

In Kotlin, using const properties is preferred over regular properties for improved performance, efficiency, code clarity, and compile-time safety. const properties are evaluated at compile-time, leading to faster execution and eliminating the need for runtime initialization. const properties also enhance code readability and consistency by

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.

Unreachable catch blocks must be removed KT-W1064
Anti-pattern
Major
Autofix

Unreachable catch blocks should be removed, because they serve no useful purpose and can lead to confusing and potentially incorrect behavior in exception handling.

Avoid using forEach with ranges KT-P1000
Performance
Major

Using the forEach method on ranges has a heavy performance cost. Prefer using simple for loops.

Non-compliant class/object name detected KT-C1000
Style
Major

This class/object has a name that does not match agreed-upon naming conventions. Not following naming conventions can lead to confusion among developers and inconsistency in the codebase, which can result in bugs and maintenance issues. It is important to maintain a consistent and easily understandable codebase by following the recommended naming conventions.

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.

Creation of temporary objects when converting primitive types to String KT-P1002
Performance
Minor

When converting primitive types to String, you should avoid creating an object of the respective primitive type first and then using .toString() for converting it to a string. Creating a temporary object is an unnecessary overhead and can incur a performance penalty.

Catch block should not require to check or cast the type of exception via is or as KT-W1000
Anti-pattern
Major

Avoid using is to check the type of exceptions, or as to cast exception types within catch blocks.

Ensure all APIs are implemented KT-W1001
Anti-pattern
Major

Avoid throwing NotImplementedError and using TODO(...) in code.

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.

Functions with only the return statement should be rewritten with expression body syntax KT-W1051
Anti-pattern
Minor

Functions which only contain a return statement can be collapsed to an expression body. This shortens the code and makes it more readable.

Found if statements that can be collapsed into one KT-W1048
Anti-pattern
Minor

Collapsing nested if statements into one improves code readability by making it more concise and easier to understand. It simplifies the logic, reduces cyclomatic complexity, and can lead to better performance. Debugging becomes easier, and code maintainability is enhanced. It is important to strike a balance between readability and line length while

Unnecessary abstract modifier in interface detected KT-W1054
Anti-pattern
Minor
Autofix

In Kotlin, the abstract keyword is redundant when declaring interfaces and their members. Interfaces are implicitly abstract in Kotlin, so there is no need to use the abstract keyword, and the same is true for any members of an interface. Omitting the abstract keyword does not affect the behavior or functionality of an interface, and allows for more concise code.

Declaration of serialVersionUID not found KT-W1060
Anti-pattern
Major

When a class implements Serializable, it is recommended to also declare a serialVersionUID field within that class. The serialVersionUID serves as a version identifier for the serialized class. It helps to ensure compatibility between the serialized objects and their corresponding class definitions, particularly when performing deserialization.

Found function that only returns a constant KT-W1052
Anti-pattern
Minor

In Kotlin, it is generally preferred to use a constant variable instead of a function returning a single constant for improved code clarity, performance, and maintainability. Constant variables are more readable, avoid unnecessary runtime overhead, and enable centralized value management. Additionally, they allow potential compiler optimizations

Detected the use of forbidden comments KT-D1000
Documentation
Minor

Developers often add TODO or FIXME comments to code that is not complete or needs review. It is a good idea to fix or review such commented code, and remove the comments before considering the code to be production-ready.

Redundant use of ?. detected KT-W1063
Anti-pattern
Minor
Autofix

In Kotlin, ?. is called the "safe call operator" or "null-safe call operator". It is used to perform a safe access or safe method call on a nullable object. It allows you to execute a method or access a property on an object only if the receiver is not null. If the object is null, the expression will evaluate to null without throwing a NullPointerException.

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.