Skip to content

Commit 674b34d

Browse files
committed
Merge pull request #225 from dylansturg/never_doesnt_return
Don't require a returns comment for functions that never return.
1 parent 7867cda commit 674b34d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Sources/SwiftFormatRules/ValidateDocumentationComments.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
9696
) {
9797
if returnClause == nil && returnDesc != nil {
9898
diagnose(.removeReturnComment(funcName: name), on: node)
99-
} else if returnClause != nil && returnDesc == nil {
99+
} else if let returnClause = returnClause, returnDesc == nil {
100+
if let returnTypeIdentifier = returnClause.returnType.as(SimpleTypeIdentifierSyntax.self),
101+
returnTypeIdentifier.name.text == "Never"
102+
{
103+
return
104+
}
100105
diagnose(.documentReturnValue(funcName: name), on: returnClause)
101106
}
102107
}

Tests/SwiftFormatRulesTests/ValidateDocumentationCommentsTests.swift

+19
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,29 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
117117
/// - p2: Parameter 2.
118118
/// - p3: Parameter 3.
119119
func foo(p1: Int, p2: Int, p3: Int) -> Int {}
120+
121+
/// One sentence summary.
122+
///
123+
/// - Parameters:
124+
/// - p1: Parameter 1.
125+
/// - p2: Parameter 2.
126+
/// - p3: Parameter 3.
127+
func neverReturns(p1: Int, p2: Int, p3: Int) -> Never {}
128+
129+
/// One sentence summary.
130+
///
131+
/// - Parameters:
132+
/// - p1: Parameter 1.
133+
/// - p2: Parameter 2.
134+
/// - p3: Parameter 3.
135+
/// - Returns: Never returns.
136+
func documentedNeverReturns(p1: Int, p2: Int, p3: Int) -> Never {}
120137
"""
121138
performLint(ValidateDocumentationComments.self, input: input)
122139
XCTAssertDiagnosed(.removeReturnComment(funcName: "noReturn"), line: 8, column: 1)
123140
XCTAssertDiagnosed(.documentReturnValue(funcName: "foo"), line: 16, column: 37)
141+
XCTAssertNotDiagnosed(.documentReturnValue(funcName: "neverReturns"))
142+
XCTAssertNotDiagnosed(.removeReturnComment(funcName: "documentedNeverReturns"))
124143
}
125144

126145
func testValidDocumentation() {

0 commit comments

Comments
 (0)