From 09c29b169d052da07bf0f778cc6f7cd306cb40fc Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Sun, 21 May 2017 23:17:53 +0200 Subject: [PATCH] Fix false positive in `syntactic_sugar` rule when using nested types Fixes #1508 --- CHANGELOG.md | 5 +++++ Source/SwiftLintFramework/Rules/SyntacticSugarRule.swift | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5a5c17e8..782a22af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,11 @@ [Marcelo Fabri](https://github.com/marcelofabri) [#1006](https://github.com/realm/SwiftLint/issues/1006) +* Fix false positive in `syntactic_sugar` rule when using nested types named + `Optional`, `ImplicitlyUnwrappedOptional`, `Array` or `Dictionary`. + [Marcelo Fabri](https://github.com/marcelofabri) + [#1508](https://github.com/realm/SwiftLint/issues/1508) + ## 0.18.1: Misaligned Drum ##### Breaking diff --git a/Source/SwiftLintFramework/Rules/SyntacticSugarRule.swift b/Source/SwiftLintFramework/Rules/SyntacticSugarRule.swift index 2e84630d0..17fb05f7b 100644 --- a/Source/SwiftLintFramework/Rules/SyntacticSugarRule.swift +++ b/Source/SwiftLintFramework/Rules/SyntacticSugarRule.swift @@ -30,7 +30,8 @@ public struct SyntacticSugarRule: Rule, ConfigurationProviderRule { "var currentIndex: Array.Index?", "func x(a: [Int], b: Int) -> Array.Index", "unsafeBitCast(nonOptionalT, to: Optional.self)", - "type is Optional.Type" + "type is Optional.Type", + "let x: Foo.Optional" ], triggeringExamples: [ "let x: ↓Array", @@ -40,13 +41,15 @@ public struct SyntacticSugarRule: Rule, ConfigurationProviderRule { "func x(a: ↓Array, b: Int) -> [Int: Any]", "func x(a: [Int], b: Int) -> ↓Dictionary", "func x(a: ↓Array, b: Int) -> ↓Dictionary", - "let x = ↓Array.array(of: object)" + "let x = ↓Array.array(of: object)", + "let x: ↓Swift.Optional" ] ) public func validate(file: File) -> [StyleViolation] { let types = ["Optional", "ImplicitlyUnwrappedOptional", "Array", "Dictionary"] - let pattern = "\\b(" + types.joined(separator: "|") + ")\\s*<.*?>" + let negativeLookBehind = "(?:(?" let kinds = SyntaxKind.commentAndStringKinds() let contents = file.contents.bridge()