Unneeded Escaping
The @escaping attribute should only be used when the closure actually escapes.
- Identifier:
unneeded_escaping - Enabled by default: No
- Supports autocorrection: Yes
- Kind: lint
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
Non Triggering Examples
func outer(completion: @escaping () -> Void) { inner(completion: completion) }
func apply(_ work: @escaping () -> Void) -> () -> Void { return work }
func f(g: @escaping () -> Void) -> () -> Void { g }
func store(completion: @escaping () -> Void) { self.c = completion }
func async(completion: @escaping () -> Void) {
DispatchQueue.main.async { completion() }
}
func capture(completion: @escaping () -> Void) {
let closure = { completion() }
closure()
}
Triggering Examples
func forEach(action: ↓@escaping (Int) -> Void) {
for i in 0..<10 {
action(i)
}
}
func process(completion: ↓@escaping () -> Void) {
completion()
}
func apply(_ transform: ↓@escaping (Int) -> Int) -> Int {
return transform(5)
}
func optional(completion: (↓@escaping () -> Void)?) {
completion?()
}
func multiple(first: ↓@escaping () -> Void, second: ↓@escaping () -> Void) {
first()
second()
}
subscript(transform: ↓@escaping (Int) -> String) -> String {
transform(42)
}
View on GitHub
Install in Dash