Equality compared between a sequence and a set
ID |
scala.correctness.comparing_diff_collection_kinds |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Misuse |
Language |
Scala |
Description
A sequence and a set never compare equal, whatever elements they hold, because collection equality first requires the two operands to belong to the same category. This comparison is therefore constant. Convert one side first, for example with toSet, so both operands are the same kind of collection.
Rationale
A sequence and a set never compare equal, whatever elements they hold, because collection equality first requires the two operands to belong to the same category. This comparison is therefore constant. Convert one side first, for example with toSet, so both operands are the same kind of collection.
The following code illustrates the pattern detected by this rule:
class ComparingDiffCollectionKinds {
def seqVsSet(a: Seq[Int], b: Set[Int]): Boolean = {
// FLAGGED: Equality compared between a sequence and a set
a == b
}
}