Swiftlint: Rule Request: Use .first instead of [0]

Created on 5 Sep 2018  路  1Comment  路  Source: realm/SwiftLint

Rule Request

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

Indexing a collection using [0] is unsafe and could cause a crash if the collection is empty

  1. Provide several examples of what _would_ and _wouldn't_ trigger violations.

Bad

let value = array[0]
let something = resource.collection[0].group.value

Good

let value = array.first
let something = resource.collection.first.group.value
  1. Should the rule be configurable, if so what parameters should be configurable?

No

  1. Should the rule be opt-in or enabled by default? Why?

I think this rule should be enabled by default for the index-at-zero case. If the rule is expanded beyond that, I think it should be opt-in.

rule-request

Most helpful comment

This should be opt-in if implemented.

>All comments

This should be opt-in if implemented.

Was this page helpful?
0 / 5 - 0 ratings