Trailing Comma

Trailing commas in arrays and dictionaries should be avoided/enforced.

  • Identifier: trailing_comma
  • Enabled by default: Yes
  • Supports autocorrection: Yes
  • Kind: style
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning
    mandatory_comma false

Non Triggering Examples

let foo = [1, 2, 3]
let foo = []
let foo = [:]
let foo = [1: 2, 2: 3]
let foo = [Void]()
let example = [ 1,
 2
 // 3,
]
foo([1: "\(error)"])
let foo = [Int]()

Triggering Examples

let foo = [1, 2, 3,]
let foo = [1, 2, 3, ]
let foo = [1, 2, 3   ,]
let foo = [1: 2, 2: 3, ]
struct Bar {
 let foo = [1: 2, 2: 3, ]
}
let foo = [1, 2, 3,] + [4, 5, 6,]
let example = [ 1,
2,
 // 3,
]
let foo = ["אבג", "αβγ", "🇺🇸",]
class C {
 #if true
 func f() {
 let foo = [1, 2, 3,]
 }
 #endif
}
foo([1: "\(error)",])