-
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 #421 from realm/rule/force-unwrapping
Add opt-in force unwrapping rule
- Loading branch information
Showing
5 changed files
with
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// ForceUnwrappingRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Benjamin Otto on 14/01/16. | ||
// Copyright (c) 2015 Realm. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
|
||
public struct ForceUnwrappingRule: OptInRule, ConfigProviderRule { | ||
|
||
public var config = SeverityConfig(.Warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "force_unwrapping", | ||
name: "Force Unwrapping", | ||
description: "Force unwrapping should be avoided.", | ||
nonTriggeringExamples: [ | ||
"if let url = NSURL(string: query)", | ||
"navigationController?.pushViewController(viewController, animated: true)" | ||
], | ||
triggeringExamples: [ | ||
"let url = NSURL(string: query)↓!", | ||
"navigationController↓!.pushViewController(viewController, animated: true)", | ||
"let unwrapped = optional↓!", | ||
"return cell↓!" | ||
] | ||
) | ||
|
||
public func validateFile(file: File) -> [StyleViolation] { | ||
return file.matchPattern("[\\w)]+!", withSyntaxKinds: [.Identifier]).map { | ||
StyleViolation(ruleDescription: self.dynamicType.description, | ||
severity: config.severity, | ||
location: Location(file: file, characterOffset: NSMaxRange($0) - 1)) | ||
} | ||
} | ||
} |
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