Syntactic Sugar for Collection Types

ID

swift.syntactic_sugar

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, style

Description

Reports the long-form spelling of standard generic types where the bracketed shorthand exists:

Long form

Shorthand

Array<T>

[T]

Dictionary<K, V>

[K: V]

Optional<T>

T?

func items() -> Array<Int> { [] }          // FLAW
func lookup() -> Optional<String> { nil }  // FLAW

func items() -> [Int] { [] }               // OK
func lookup() -> String? { nil }           // OK

Rationale

The shorthand spellings are the idiomatic Swift forms and are used everywhere in the standard library. The long form makes the type stand out as if it were something exotic.

Remediation

Replace the long form with the bracketed shorthand:

let xs: [Int] = []
let m: [String: Int] = [:]
let x: String? = nil