From d14d79d2c451fe433b195050fde75df8e8b464aa Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Mon, 29 Apr 2019 09:47:00 -0700 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20trigger=20redundant=5Fvoid=5Fre?= =?UTF-8?q?turn=20when=20using=20subscripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 +++- Rules.md | 8 ++++++++ .../Rules/Idiomatic/RedundantVoidReturnRule.swift | 12 ++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a94c59b6..1f0e4ae9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,9 @@ #### Bug Fixes -* None. +* Don't trigger `redundant_void_return` violations when using `subscript` as the + return type is required. + [Marcelo Fabri](https://github.com/marcelofabri) ## 0.32.0: Wash-N-Fold-N-Reduce diff --git a/Rules.md b/Rules.md index 255d3956c..ddc1c0792 100644 --- a/Rules.md +++ b/Rules.md @@ -17070,6 +17070,14 @@ func foo() -> Void! ``` +```swift +struct A { + subscript(key: String) { + print(key) + } +} +``` +
Triggering Examples diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/RedundantVoidReturnRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/RedundantVoidReturnRule.swift index 66a18930b..0b700670a 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/RedundantVoidReturnRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/RedundantVoidReturnRule.swift @@ -23,7 +23,14 @@ public struct RedundantVoidReturnRule: ConfigurationProviderRule, SubstitutionCo "func foo() -> ()?\n", "func foo() -> ()!\n", "func foo() -> Void?\n", - "func foo() -> Void!\n" + "func foo() -> Void!\n", + """ + struct A { + subscript(key: String) { + print(key) + } + } + """ ], triggeringExamples: [ "func foo()↓ -> Void {}\n", @@ -51,6 +58,7 @@ public struct RedundantVoidReturnRule: ConfigurationProviderRule, SubstitutionCo private let pattern = "\\s*->\\s*(?:Void\\b|\\(\\s*\\))(?![?!])" private let excludingKinds = SyntaxKind.allKinds.subtracting([.typeidentifier]) + private let functionKinds = SwiftDeclarationKind.functionKinds.subtracting([.functionSubscript]) public func validate(file: File, kind: SwiftDeclarationKind, dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { @@ -63,7 +71,7 @@ public struct RedundantVoidReturnRule: ConfigurationProviderRule, SubstitutionCo public func violationRanges(in file: File, kind: SwiftDeclarationKind, dictionary: [String: SourceKitRepresentable]) -> [NSRange] { - guard SwiftDeclarationKind.functionKinds.contains(kind), + guard functionKinds.contains(kind), let nameOffset = dictionary.nameOffset, let nameLength = dictionary.nameLength, let length = dictionary.length,