-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2739 from realm/marcelo/unowned_variable_capture
Add `unowned_variable_capture` opt-in rule
- Loading branch information
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Source/SwiftLintFramework/Rules/Lint/UnownedVariableCaptureRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct UnownedVariableCaptureRule: ASTRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "unowned_variable_capture", | ||
name: "Unowned Variable Capture", | ||
description: "Prefer capturing references as weak to avoid potential crashes.", | ||
kind: .lint, | ||
minSwiftVersion: .five, | ||
nonTriggeringExamples: [ | ||
"foo { [weak self] in _ }", | ||
"foo { [weak self] param in _ }", | ||
"foo { [weak bar] in _ }", | ||
"foo { [weak bar] param in _ }", | ||
"foo { bar in _ }", | ||
"foo { $0 }" | ||
], | ||
triggeringExamples: [ | ||
"foo { [↓unowned self] in _ }", | ||
"foo { [↓unowned bar] in _ }", | ||
"foo { [bar, ↓unowned self] in _ }" | ||
] | ||
) | ||
|
||
public func validate(file: File, kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard kind == .closure, let bodyOffset = dictionary.bodyOffset, let bodyLength = dictionary.bodyLength, | ||
case let contents = file.contents.bridge(), | ||
let closureRange = contents.byteRangeToNSRange(start: bodyOffset, length: bodyLength), | ||
let inTokenRange = file.match(pattern: "\\bin\\b", with: [.keyword], range: closureRange).first, | ||
let inTokenByteRange = contents.NSRangeToByteRange(start: inTokenRange.location, | ||
length: inTokenRange.length) else { | ||
return [] | ||
} | ||
|
||
let length = inTokenByteRange.location - bodyOffset | ||
let variables = localVariableDeclarations(inByteRange: NSRange(location: bodyOffset, length: length), | ||
structure: file.structure) | ||
let unownedVariableOffsets = variables.compactMap { dictionary in | ||
return dictionary.swiftAttributes.first { attributeDict in | ||
guard attributeDict.attribute.flatMap(SwiftDeclarationAttributeKind.init) == .weak, | ||
let offset = attributeDict.offset, let length = attributeDict.length else { | ||
return false | ||
} | ||
|
||
return contents.substringWithByteRange(start: offset, length: length) == "unowned" | ||
}?.offset | ||
} | ||
|
||
return unownedVariableOffsets.map { offset in | ||
return StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset)) | ||
} | ||
} | ||
|
||
private func localVariableDeclarations(inByteRange byteRange: NSRange, | ||
structure: Structure) -> [[String: SourceKitRepresentable]] { | ||
var results = [[String: SourceKitRepresentable]]() | ||
|
||
func parse(dictionary: [String: SourceKitRepresentable]) { | ||
if let kindString = (dictionary.kind), | ||
SwiftDeclarationKind(rawValue: kindString) == .varLocal, | ||
let offset = dictionary.offset, | ||
let length = dictionary.length { | ||
let variableByteRange = NSRange(location: offset, length: length) | ||
|
||
if byteRange.intersects(variableByteRange) { | ||
results.append(dictionary) | ||
} | ||
} | ||
dictionary.substructure.forEach(parse) | ||
} | ||
parse(dictionary: structure.dictionary) | ||
return results | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters