Unused Capture List

Unused reference in a capture list should be removed

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

Non Triggering Examples

[1, 2].map {
    [ weak
      delegate,
      unowned
      self
    ] num in
    delegate.handle(num)
}
[1, 2].map { [weak self] num in
    self?.handle(num)
}
let failure: Failure = { [weak self, unowned delegate = self.delegate!] foo in
    delegate.handle(foo, self)
}
numbers.forEach({
    [weak handler] in
    handler?.handle($0)
})
withEnvironment(apiService: MockService(fetchProjectResponse: project)) {
    [Device.phone4_7inch, Device.phone5_8inch, Device.pad].forEach { device in
        device.handle()
    }
}
{ [foo] _ in foo.bar() }()
sizes.max().flatMap { [(offset: offset, size: $0)] } ?? []
[1, 2].map { [self] num in
    handle(num)
}
[1, 2].map { [unowned self] num in
    handle(num)
}
[1, 2].map { [self, unowned delegate = self.delegate!] num in
    delegate.handle(num)
}
[1, 2].map { [unowned self, unowned delegate = self.delegate!] num in
    delegate.handle(num)
}
[1, 2].map {
    [ weak
      delegate,
      self
    ] num in
    delegate.handle(num)
}
rx.onViewDidAppear.subscribe(onNext: { [unowned self] in
      doSomething()
}).disposed(by: disposeBag)
let closure = { [weak self] in
    guard let self else {
        return
    }
    someInstanceFunction()
}

Triggering Examples

[1, 2].map { [weak self] num in
    print(num)
}
let failure: Failure = { [weak self, unowned delegate = self.delegate!] foo in
    self?.handle(foo)
}
let failure: Failure = { [weak self, unowned delegate = self.delegate!] foo in
    print(foo)
}
numbers.forEach({
    [weak handler] in
    print($0)
})
numbers.forEach({
    [self, weak handler] in
    print($0)
})
withEnvironment(apiService: MockService(fetchProjectResponse: project)) { [foo] in
    [Device.phone4_7inch, Device.phone5_8inch, Device.pad].forEach { device in
        device.handle()
    }
}
{ [foo] in _ }()
let closure = { [weak a] in
    // The new `a` immediately shadows the captured `a` which thus isn't needed.
    guard let a = getOptionalValue() else {
        return
    }
    someInstanceFunction()
}