From 3bbaaa56e6a3979c87f8e957d8e0f4260517e550 Mon Sep 17 00:00:00 2001 From: Hank Chen Date: Fri, 18 Sep 2020 17:13:40 +0800 Subject: [PATCH 1/3] [Fix] `UnusedCaptureListRule`: implicit self in @escaping closures --- .../Rules/Lint/UnusedCaptureListRule.swift | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift b/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift index 1acbbd1a1d..263d88f3ce 100644 --- a/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift +++ b/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift @@ -37,7 +37,17 @@ 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) + } + """) ], triggeringExamples: [ Example(""" @@ -62,6 +72,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() @@ -74,6 +90,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 @@ -116,6 +134,7 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat var locationOffset = 0 return captureList.components(separatedBy: ",") .reduce(into: [(String, Int)]()) { referencesAndLocations, item in + guard item != selfKeyword else { return } let item = item.bridge() let range = item.rangeOfCharacter(from: CharacterSet.whitespacesAndNewlines.inverted) guard range.location != NSNotFound else { return } From 577a2847da6521ce25494a0faa5f1a9f798b79c1 Mon Sep 17 00:00:00 2001 From: Hank Chen Date: Fri, 18 Sep 2020 17:32:49 +0800 Subject: [PATCH 2/3] [Fix] `UnusedCaptureListRule`: handle newlines and whitespace --- .../Rules/Lint/UnusedCaptureListRule.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift b/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift index 263d88f3ce..a21e951c47 100644 --- a/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift +++ b/Source/SwiftLintFramework/Rules/Lint/UnusedCaptureListRule.swift @@ -47,6 +47,15 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat [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: [ @@ -134,7 +143,8 @@ public struct UnusedCaptureListRule: ASTRule, ConfigurationProviderRule, Automat var locationOffset = 0 return captureList.components(separatedBy: ",") .reduce(into: [(String, Int)]()) { referencesAndLocations, item in - guard item != selfKeyword else { return } + 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 } From cdc70d44cd88356d29a866606f81378497fbedf2 Mon Sep 17 00:00:00 2001 From: hank121314 Date: Fri, 18 Sep 2020 18:16:53 +0800 Subject: [PATCH 3/3] chore(CHANGELOG): update CHANGELOG.MD --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 741c096365..e0b3d69806 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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