Is Disjoint

ID

swift.is_disjoint

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Swift

Tags

efficiency, idiomatic

Description

Reports a .intersection(other).isEmpty chain. The standard library exposes isDisjoint(with:) for the same predicate without materialising the intersection set.

a.intersection(b).isEmpty           // FLAW
a.isDisjoint(with: b)               // OK

Rationale

.intersection(b) allocates a new set and walks both inputs to completion before .isEmpty is even evaluated. .isDisjoint(with:) walks the smaller set once and stops at the first shared element. On large sets the difference is measurable; even on tiny sets, isDisjoint(with:) is the idiomatic spelling.

Remediation

Replace the chain with isDisjoint(with:):

if a.isDisjoint(with: b) { ... }

If you need the intersection itself elsewhere, keep the call but store the result instead of just checking emptiness.