forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Classes are required to have a deinit method. This is a style to help debugging memory issues, when it is common to want to set a breakpoint at the point of deallocation. Most classes don't have a deinit, so the developer ends up having to quit, add a deinit and rebuild to proceed. If all classes have a deinit, debugging is much smoother. Ref: realm#2620
- Loading branch information
1 parent
9bae50f
commit 9b982b1
Showing
8 changed files
with
177 additions
and
2 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
76 changes: 76 additions & 0 deletions
76
Source/SwiftLintFramework/Rules/Style/DeinitRequiredRule.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,76 @@ | ||
import SourceKittenFramework | ||
|
||
public struct DeinitRequiredRule: OptInRule, ConfigurationProviderRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public static let description = RuleDescription( | ||
identifier: "deinit_required", | ||
name: "Deinit Required", | ||
description: "Classes should have an explicit deinit method.", | ||
kind: .style, | ||
nonTriggeringExamples: [ | ||
""" | ||
class Apple { | ||
deinit { } | ||
} | ||
""", | ||
"enum Banana { }", | ||
"protocol Cherry { }", | ||
"struct Damson { }" | ||
], | ||
triggeringExamples: [ | ||
"↓class Apple { }", | ||
"↓class Banana: NSObject, Equatable { }", | ||
""" | ||
↓class Cherry { | ||
// deinit { } | ||
} | ||
""", | ||
""" | ||
↓class Damson { | ||
func deinitialize() { } | ||
} | ||
""", | ||
""" | ||
class Outer { | ||
func hello() -> String { return "outer" } | ||
deinit { } | ||
↓class Inner { | ||
func hello() -> String { return "inner" } | ||
} | ||
} | ||
""" | ||
] | ||
) | ||
|
||
public init() {} | ||
|
||
public func validate(file: File) -> [StyleViolation] { | ||
let classCollector = NamespaceCollector(dictionary: file.structure.dictionary) | ||
let classes = classCollector.findAllElements(of: [.class]) | ||
|
||
let violations: [StyleViolation] = classes.compactMap { element in | ||
guard let offset = element.dictionary.offset else { | ||
return nil | ||
} | ||
|
||
let methodCollector = NamespaceCollector(dictionary: element.dictionary) | ||
let methods = methodCollector.findAllElements(of: [.functionMethodInstance]) | ||
|
||
let containsDeinit = methods.contains { | ||
$0.name == "deinit" | ||
} | ||
|
||
if containsDeinit { | ||
return nil | ||
} | ||
|
||
return StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset)) | ||
} | ||
|
||
return violations | ||
} | ||
} |
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
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