Scala

Scala

Made by DeepSource
Detected unreachable code SC-W1006
Bug risk
Major

Unreachable code is usually a result of programming error - usually when statements like 'return' are misplaced. This causes some part of the code to not execute at all, thus affecting the overall control-flow and behaviour of the program.

Empty interpolated string SC-W1011
Bug risk
Critical

Empty interpolated string is usually a result of human error - either that the programmer forgot to specify the required arguments, or that the string wasn't meant to be interpolated. Such strings can critically alter the behaviour and control-flow of the program if the program relies on it.

Duplicate case in pattern-matching SC-W1007
Bug risk
Major

Duplicate cases in pattern matching are unreachable and are usually the result of human error. Because such cases are unreachable, they have the potential to affect the overall control-flow and the behaviour of the program. It is suggested that you remove such duplicate cases.

Using .deep to compare Arrays is deprecated SC-W1051
Bug risk
Critical

Comparing two Arrays using .deep is deprecated and is not supported beyond Scala version 2.12. To avoid compilation errors when moving to version 2.13 and above, consider switching to a more well supported alternative.

Redundant val keyword for case class' constructor parameter SC-R1038
Bug risk
Minor

By default, case class' parameters are accessible from both outside and inside the class. Therefore, marking them with the val keyword is no longer necessary and is redundant.

Oddness check i % 2 == 0 fails for -ve numbers SC-W1000
Bug risk
Major

Using the standard i % 2 == 0 oddness check fails for -ve numbers. The preferred way is: i % 2 != 0 since +ve numbers leave +1 as remainder and -ve numbers leave -1 as remainder.

Incorrect number of args to String.format SC-W1008
Bug risk
Critical

String.format() requires that you supply all the arguments corresponding to the format specifiers. Missing arguments usually leads to an improperly formatted string. Such strings can critically alter the behaviour and control-flow of the program if the program relies on it.

Missing method return type SC-W1009
Bug risk
Major

Not specifying the return type of a method is part of the procedural syntax and is deprecated. In such cases, the compiler assumes that the return type of such method is Unit. Any explicit return statements inside such methods have no effect and are discarded.

Duplicate key in HashMap SC-W1019
Bug risk
Major

A duplicate key exists in the HashMap. This may lead to an unintentional behaviour as the key will always map to the last value provided to it and hence is always recommended that these keys be unique.

Duplicate element in HashSet SC-W1020
Bug risk
Minor

A duplicate element exists in the HashSet. This adds no value as the structure HashSet always maintains elements that are unique and distinct from each other and is usually a result of a human-error. It is suggested that you remove such duplicate elements.

Explicit trap of NullPointerException SC-W1021
Bug risk
Major

Explicit trapping of NullPointerException is considered a bad practice in both Scala and Java. It usually means that your application has come across a null reference in an unexpected manner which you're trying to suppress by explicitly trapping through a catch block rather than finding the root cause. Since this was unexpected, it is probably not safe for your application to continue with the execution.

Another idea is to rely on Option[T] instead of null. Have your methods return Some(_) if the call succeeds and None if the call fails, an approach taken by many of Scala's built-in methods. If you truly have to rely on null, use if-else to explicitly check for null references rather than relying on try-catch.

If condition depends on boolean value that is a compile time constant SC-W1022
Bug risk
Major

The specified if condition depends on a boolean value that is a constant and changes neither at compile-time nor at runtime. This is likely due to a human error and is expected that this constant value is replaced with an appropriate condition that can be evaluated as necessary. Without this fix, it is expected that this if condition can potentially alter the logic of your program, thus affecting the overall behavior of your program.

Pattern matching may be non-exhaustive SC-W1024
Bug risk
Major

The general practice when pattern-matching is to cover as many scenarios as possible. If none of the specified cases match the provided input, scala.MatchError exception is thrown during the runtime. If you wish to match only a small subset of scenarios, it is recommended to add an additional case that handles the rest of the scenarios through the usage of a wildcard - case _ => ....

Missing case None in pattern-matching SC-W1025
Bug risk
Critical

Analyzer has detected that the case Some(_) was specified in the pattern-matching but not None. The general consensus is that any method that wraps a value in Some usually has a return type of Option[T]. In case the method succeeds, Some(_) is returned, else None. Therefore it is recommended that you add the missing case None in your pattern-matching.

Infix operator has same operand on both sides SC-W1026
Bug risk
Critical

This infix operator has the same operand on both sides.

Duplicated then and else blocks SC-W1027
Bug risk
Critical

The same code is duplicated in the then and else blocks of this if expression, meaning that regardless of whether this condition is true or false, the program executes the same set of instructions.

Array comparison using conventional comparison operators SC-W1046
Bug risk
Critical

Using the conventional comparison operators such as == or != do not necessarily compare the contents of arrays. Rather, the said operators check if the entities on both sides refer to the same object or not. To put it in simpler terms, they check reference, not contents. To compare the contents of 2 Arrays, use sameElements.

Usage of reserved keywords SC-W1030
Bug risk
Major

This variable identifier is a reserved keyword.

Potential resource (file handle) leak SC-W1033
Bug risk
Major

Calling methods such as mkString directly over scala.io.Source methods such as fromFile leaks the underneath file handle. To mitigate this, one way is to manually call the close method to free/close these resources yourself. However, the ideal and right approach is to use Scala's automatic resource management. scala.util.Using helps automatically manage either a single resource or multiple resources, thus preventing any underneath file handles from leaking.

Invalid deprecated annotation used SC-W1035
Bug risk
Critical

Languages like Java and Scala allow you to annotate a method as deprecated to signify that the method is no longer supported and will be dropped in the near future and that the users must switch to a suitable alternative. However, marking a Scala method with Java's annotation, i.e. @Deprecated is not ideal. Rather, it is suggested that you use the Scala's annotation, i.e. @deprecated as the former may or may not trigger the deprecated message.