rspec/rules/S4173/swift/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

40 lines
780 B
Plaintext

== Why is this an issue?
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
[source,swift]
----
let one = arr.filter { $0.containsString("yo") }.first // Noncompliant
----
=== Compliant solution
[source,swift]
----
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[]