Skip to content

Commit

Permalink
Ignore unused_closure_parameter on inline closures
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri authored and natanrolnik committed Feb 8, 2017
1 parent 118e387 commit ecec105
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
* Fix `redundant_void_return` matches if return type starts with Void~.
[Hayashi Tatsuya](https://github.com/sora0077)

* Ignore `unused_closure_parameter` rule on closures that are called inline.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1161](https://github.com/realm/SwiftLint/issues/1161)

## 0.16.1: Commutative Fabric Sheets

##### Breaking
Expand Down
15 changes: 14 additions & 1 deletion Source/SwiftLintFramework/Rules/UnusedClosureParameterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule, Co
"}",
"genericsFunc { (a: Type, b) in\n" +
"a + b\n" +
"}\n"
"}\n",
"var label: UILabel = { (lbl: UILabel) -> UILabel in\n" +
" lbl.backgroundColor = .red\n" +
" return lbl\n" +
"}(UILabel())\n"
],
triggeringExamples: [
"[1, 2].map { ↓number in\n return 3\n}\n",
Expand Down Expand Up @@ -81,6 +85,7 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule, Co
private func violationRanges(in file: File, dictionary: [String: SourceKitRepresentable],
kind: SwiftExpressionKind) -> [(range: NSRange, name: String)] {
guard kind == .call,
!isClosure(dictionary: dictionary),
let offset = dictionary.offset,
let length = dictionary.length,
let nameOffset = dictionary.nameOffset,
Expand Down Expand Up @@ -133,6 +138,14 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule, Co
}
}

private func isClosure(dictionary: [String: SourceKitRepresentable]) -> Bool {
return dictionary.name.flatMap { name -> Bool in
let length = name.bridge().length
let range = NSRange(location: 0, length: length)
return regex("\\A\\s*\\{").firstMatch(in: name, options: [], range: range) != nil
} ?? false
}

private func violationRanges(in file: File,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
return dictionary.substructure.flatMap { subDict -> [NSRange] in
Expand Down

0 comments on commit ecec105

Please sign in to comment.