Suspicious call to add or remove Iterable argument
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:
Quick-fixes:
Convert to
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)Convert the
Pathargument to a collection to clarify intent without changing semantics:For
plus: wrap the argument withtoList()to preserve order.For
minus: wrap the argument withtoSet()for efficient removal.
val paths = listOf(path).plus(somePath.toList()) val paths2 = setOf(path).minus(somePath.toSet())
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
SuspiciousCallOnCollectionToAddOrRemovePath- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection ID: SuspiciousCallOnCollectionToAddOrRemovePath
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |