plugins/kotlin/code-insight/descriptions/resources-en/inspectionDescriptions/SuspiciousCallOnCollectionToAddOrRemovePath.html
Reports usages of plus/minus where the argument type implements Iterable, and the receiver is a Kotlin Collection or Sequence of the same element type.
If the argument type implements Iterable, it is treated as a collection of elements rather than as a single element. This may unintentionally add or remove multiple elements instead of the object itself.
A common example is java.nio.file.Path, which implements Iterable\<Path\> (iterating over its name elements). Adding a Path to a Collection\<Path\> using + or - may therefore operate on its components rather than on the Path instance as a whole.
The inspection covers both operator syntax (a + b, a - b) and explicit calls (a.plus(b), a.minus(b)).
Examples:
// Operator form
val paths = listOf(path) + somePath
val paths2 = setOf(path) - somePath
// Regular call form
val paths = listOf(path).plus(somePath)
val paths2 = setOf(path).minus(somePath)
// Sequences
val seq = sequenceOf(path) + somePath
// Path + Path (both sides are iterable)
val combined = path1 + path2 // Returns List of segments, not a combined path
Quick-fixes:
plusElement/minusElement (changes the semantics to what was originally intended):val paths = listOf(path).plusElement(somePath)
val paths2 = setOf(path).minusElement(somePath)
val paths3 = sequenceOf(path).plusElement(somePath)
Path argument to a collection to clarify intent without changing semantics:
plus: wrap the argument with toList() to preserve order.minus: wrap the argument with toSet() for efficient removal.val paths = listOf(path).plus(somePath.toList())
val paths2 = setOf(path).minus(somePath.toSet())