rspec/rules/S4173/swift/rule.adoc

40 lines
780 B
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
If you only want one instance that matches certain criteria out of a collection, it's far more efficient to grab the first matching item than it is to fully filter the collection for your criteria and then only use a single value.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
let one = arr.filter { $0.containsString("yo") }.first // Noncompliant
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,swift]
2021-04-28 16:49:39 +02:00
----
let one = arr.first(where: { $0.containsString("yo") })
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Convert this "filter...first" operation to "first(where: ...)".
=== Highlighting
primary: ``++first++``
secondary: ``++filter++``
endif::env-github,rspecator-view[]