Redundant Nil Coalescing

ID

swift.redundant_nil_coalescing

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, redundant-code

Description

Reports a ?? (nil-coalescing) binary expression whose right operand is the literal nil. The whole expression is equivalent to the left operand alone.

let a = x ?? nil         // FLAW
let c = a ?? b ?? nil    // FLAW

let a = x ?? 0           // OK
let c = a ?? b ?? 0      // OK

Rationale

Coalescing with nil produces the same result as the LHS in every case, so the operator is pure noise.

Remediation

Drop the ?? nil:

let a = x
let c = a ?? b