Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] UnusedCaptureListRule: implicit self in @escaping closures #3352

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

#### Bug Fixes

* None.
* Rule `unused_capture_list` should not be triggered by self keyword.
[hank121314](https://github.com/hank121314)
[#2367](https://github.com/realm/SwiftLint/issues/3267)

## 0.40.2: Demo Unit

Expand Down
31 changes: 30 additions & 1 deletion Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,26 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat
}
"""),
Example("{ [foo] _ in foo.bar() }()"),
Example("sizes.max().flatMap { [(offset: offset, size: $0)] } ?? []")
Example("sizes.max().flatMap { [(offset: offset, size: $0)] } ?? []"),
Example("""
[1, 2].map { [self] num in
handle(num)
}
"""),
Example("""
[1, 2].map { [self, unowned delegate = self.delegate!] num in
delegate.handle(num)
}
"""),
Example("""
[1, 2].map {
[ weak
delegate,
self
] num in
delegate.handle(num)
}
""")
],
triggeringExamples: [
Example("""
Expand All @@ -62,6 +81,12 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat
})
"""),
Example("""
numbers.forEach({
[self, weak handler] in
print($0)
})
"""),
Example("""
withEnvironment(apiService: MockService(fetchProjectResponse: project)) { [↓foo] in
[Device.phone4_7inch, Device.phone5_8inch, Device.pad].forEach { device in
device.handle()
Expand All @@ -74,6 +99,8 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat

private let captureListRegex = regex("^\\{\\s*\\[([^\\]]+)\\]")

private let selfKeyword = "self"

public func validate(file: SwiftLintFile, kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
let contents = file.stringView
Expand Down Expand Up @@ -116,6 +143,8 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat
var locationOffset = 0
return captureList.components(separatedBy: ",")
.reduce(into: [(String, Int)]()) { referencesAndLocations, item in
let word = item.trimmingCharacters(in: .whitespacesAndNewlines)
guard word != selfKeyword else { return }
let item = item.bridge()
let range = item.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines.inverted)
guard range.location != NSNotFound else { return }
Expand Down