Unused Enumerated

When the index or the item is not used, .enumerated() can be removed.

  • Identifier: unused_enumerated
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: idiomatic
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

for (idx, foo) in bar.enumerated() { }
for (_, foo) in bar.enumerated().something() { }
for (_, foo) in bar.something() { }
for foo in bar.enumerated() { }
for foo in bar { }
for (idx, _) in bar.enumerated().something() { }
for (idx, _) in bar.something() { }
for idx in bar.indices { }
for (section, (event, _)) in data.enumerated() {}
list.enumerated().map { idx, elem in "\(idx): \(elem)" }
list.enumerated().map { $0 + $1 }
list.enumerated().something().map { _, elem in elem }
list.enumerated().map { ($0.offset, $0.element) }
list.enumerated().map { ($0.0, $0.1) }
list.enumerated().map {
    $1.enumerated().forEach { print($0, $1) }
    return $0
}

Triggering Examples

for (_, foo) in bar.enumerated() { }
for (_, foo) in abc.bar.enumerated() { }
for (_, foo) in abc.something().enumerated() { }
for (idx, _) in bar.enumerated() { }
list.enumerated().map { idx, _ in idx }
list.enumerated().map { _, elem in elem }
list.enumerated().forEach { print($0) }
list.enumerated().map { $1 }
list.enumerated().map {
    $1.enumerated().forEach { print($1) }
    return $0
}
list.enumerated().map {
    $1.enumerated().forEach { print($0, $1) }
    return 1
}
list.enumerated().forEach {
    let (i, _) = $0
}