Fix void_return rule to support async and async throws functions (#4772)

This commit is contained in:
Mathias Schreck 2023-02-17 21:39:32 +01:00 committed by GitHub
parent 6a9e6776a9
commit 352ffdfc57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -112,6 +112,10 @@
[Marcelo Fabri](https://github.com/marcelofabri) [Marcelo Fabri](https://github.com/marcelofabri)
[#4753](https://github.com/realm/SwiftLint/issues/4753) [#4753](https://github.com/realm/SwiftLint/issues/4753)
* Fix `void_return` rule to support async and async throws functions.
[Mathias Schreck](https://github.com/lo1tuma)
[#4772](https://github.com/realm/SwiftLint/issues/4772)
## 0.50.3: Bundle of Towels ## 0.50.3: Bundle of Towels
#### Breaking #### Breaking

View File

@ -18,7 +18,12 @@ struct VoidReturnRule: ConfigurationProviderRule, SubstitutionCorrectableRule {
Example("let foo: (ConfigurationTests) -> () throws -> Void\n"), Example("let foo: (ConfigurationTests) -> () throws -> Void\n"),
Example("let foo: (ConfigurationTests) -> () throws -> Void\n"), Example("let foo: (ConfigurationTests) -> () throws -> Void\n"),
Example("let foo: (ConfigurationTests) ->() throws -> Void\n"), Example("let foo: (ConfigurationTests) ->() throws -> Void\n"),
Example("let foo: (ConfigurationTests) -> () -> Void\n") Example("let foo: (ConfigurationTests) -> () -> Void\n"),
Example("let foo: () -> () async -> Void\n"),
Example("let foo: () -> () async throws -> Void\n"),
Example("let foo: () -> () async -> Void\n"),
Example("func foo() -> () async throws -> Void {}\n"),
Example("func foo() async throws -> () async -> Void { return {} }\n")
], ],
triggeringExamples: [ triggeringExamples: [
Example("let abc: () -> ↓() = {}\n"), Example("let abc: () -> ↓() = {}\n"),
@ -27,7 +32,9 @@ struct VoidReturnRule: ConfigurationProviderRule, SubstitutionCorrectableRule {
Example("func foo(completion: () -> ↓())\n"), Example("func foo(completion: () -> ↓())\n"),
Example("func foo(completion: () -> ↓( ))\n"), Example("func foo(completion: () -> ↓( ))\n"),
Example("func foo(completion: () -> ↓(Void))\n"), Example("func foo(completion: () -> ↓(Void))\n"),
Example("let foo: (ConfigurationTests) -> () throws -> ↓()\n") Example("let foo: (ConfigurationTests) -> () throws -> ↓()\n"),
Example("func foo() async -> ↓()\n"),
Example("func foo() async throws -> ↓()\n")
], ],
corrections: [ corrections: [
Example("let abc: () -> ↓() = {}\n"): Example("let abc: () -> Void = {}\n"), Example("let abc: () -> ↓() = {}\n"): Example("let abc: () -> Void = {}\n"),
@ -37,7 +44,8 @@ struct VoidReturnRule: ConfigurationProviderRule, SubstitutionCorrectableRule {
Example("func foo(completion: () -> ↓( ))\n"): Example("func foo(completion: () -> Void)\n"), Example("func foo(completion: () -> ↓( ))\n"): Example("func foo(completion: () -> Void)\n"),
Example("func foo(completion: () -> ↓(Void))\n"): Example("func foo(completion: () -> Void)\n"), Example("func foo(completion: () -> ↓(Void))\n"): Example("func foo(completion: () -> Void)\n"),
Example("let foo: (ConfigurationTests) -> () throws -> ↓()\n"): Example("let foo: (ConfigurationTests) -> () throws -> ↓()\n"):
Example("let foo: (ConfigurationTests) -> () throws -> Void\n") Example("let foo: (ConfigurationTests) -> () throws -> Void\n"),
Example("func foo() async throws -> ↓()\n"): Example("func foo() async throws -> Void\n")
] ]
) )
@ -53,7 +61,7 @@ struct VoidReturnRule: ConfigurationProviderRule, SubstitutionCorrectableRule {
let kinds = SyntaxKind.commentAndStringKinds let kinds = SyntaxKind.commentAndStringKinds
let parensPattern = "\\(\\s*(?:Void)?\\s*\\)" let parensPattern = "\\(\\s*(?:Void)?\\s*\\)"
let pattern = "->\\s*\(parensPattern)\\s*(?!->)" let pattern = "->\\s*\(parensPattern)\\s*(?!->)"
let excludingPattern = "(\(pattern))\\s*(throws\\s+)?->" let excludingPattern = "(\(pattern))\\s*(async\\s+)?(throws\\s+)?->"
return file.match(pattern: pattern, excludingSyntaxKinds: kinds, excludingPattern: excludingPattern, return file.match(pattern: pattern, excludingSyntaxKinds: kinds, excludingPattern: excludingPattern,
exclusionMapping: { $0.range(at: 1) }).compactMap { exclusionMapping: { $0.range(at: 1) }).compactMap {