Skip to content

Commit

Permalink
minor tweaks to UnusedClosureParameterRule.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim authored Dec 16, 2016
1 parent e854701 commit d2b9c72
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions Source/SwiftLintFramework/Rules/UnusedClosureParameterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,27 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule {

return parameters.flatMap { param -> StyleViolation? in
guard let paramOffset = (param["key.offset"] as? Int64).flatMap({ Int($0) }),
let name = param["key.name"] as? String else {
return nil
}

// swiftlint:disable:next force_try
let regex = try! NSRegularExpression(pattern: name, options: [.ignoreMetacharacters])
guard let range = contents.byteRangeToNSRange(start: rangeStart,
length: rangeLength) else {
let name = param["key.name"] as? String,
let regex = try? NSRegularExpression(pattern: name,
options: [.ignoreMetacharacters]),
let range = contents.byteRangeToNSRange(start: rangeStart, length: rangeLength)
else {
return nil
}

let matches = regex.matches(in: file.contents, options: [], range: range).ranges()
for range in matches {
guard let byteRange = contents.NSRangeToByteRange(start: range.location,
length: range.length) else {
continue
}

// if it's the parameter declaration itself, we should skip
if byteRange.location == paramOffset {
continue
}

let tokens = file.syntaxMap.tokensIn(byteRange)
// a parameter usage should be only one token
if tokens.count != 1 {
length: range.length),
// if it's the parameter declaration itself, we should skip
byteRange.location != paramOffset,
case let tokens = file.syntaxMap.tokensIn(byteRange),
// a parameter usage should be only one token
tokens.count == 1 else {
continue
}

// found an usage, there's no violation!
// found a usage, there's no violation!
if let token = tokens.first, SyntaxKind(rawValue: token.type) == .identifier,
token.offset == byteRange.location, token.length == byteRange.length {
return nil
Expand Down

0 comments on commit d2b9c72

Please sign in to comment.