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.

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.

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.

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

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.

Found duplicate condition in binary expression KT-W1043
Anti-pattern
Minor

Duplicate conditions in binary expressions are problematic because they introduce redundancy, make code harder to maintain, and can negatively impact performance.

null comparison using equals method KT-W1049
Anti-pattern
Major
Autofix

Using equals to compare an object with null is bad practice. If an object has to be checked for null, consider using the == operator directly. This makes the code more concise. Furthermore, comparison using == also prevents mistakes caused by forgetting to handle null in the equals() implementation, or if the object you are calling equals() on is itself null.

Detected member with protected visibility in final class KT-W1055
Anti-pattern
Minor

Kotlin classes are final by default. Thus, classes which are not marked as open should not contain protected members. If stricter visibility is desired, one should consider making the members private. On the other hand, if the member is a const property and visibility outside the class is not an issue, consider making it public.

Exceptions should not be rethrown after being wrapped in a new instance of the same type KT-W1008
Anti-pattern
Major

Exceptions should not be wrapped inside the same exception type and then rethrown.

Instead, wrap the exception in a more meaningful exception type.

Avoid explicitly triggering garbage collection KT-W1029
Anti-pattern
Major

The code explicitly triggers the garbage collector by calling System.gc() or similar methods. In Kotlin, you don't need to explicitly trigger the garbage collector.

Proper logging should be preferred over printing stack trace of exceptions KT-W1002
Anti-pattern
Major

Avoid using printStackTrace to log exceptions.

Caught exceptions must not be rethrown KT-W1003
Anti-pattern
Major

Avoid throwing a caught exception without modifying it.

Method should not return from finally blocks KT-W1004
Anti-pattern
Major

Avoid returning from within a finally block, as this can prevent normal exception handling control flow from executing.

Ensure exceptions that are caught are actually handled KT-W1005
Anti-pattern
Major

Avoid "swallowing" exceptions by catching them and leaving the exception variable unused.

The main method should not throw exceptions KT-W1007
Anti-pattern
Major

Avoid throwing exceptions within the main method.