Skip to content

Commit ecbede7

Browse files
defer assignment to position-prefixed constants to inside the switch statements
1 parent b5f7003 commit ecbede7

File tree

2 files changed

+74
-88
lines changed

2 files changed

+74
-88
lines changed

Sources/SymbolKit/SymbolGraph/SemanticVersion/SemanticVersionError.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,12 @@ extension SymbolGraph {
9797
case let .invalidCharacterInIdentifier(identifier, position):
9898
return "semantic version \(position) identifier '\(identifier)' cannot contain characters other than ASCII alphanumerics and hyphen-minus ([0-9A-Za-z-])"
9999
case let .invalidNumericIdentifier(identifier, position, errorKind):
100-
let fault: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
101-
switch errorKind {
102-
case .leadingZeros:
103-
return "contain leading '0'"
104-
case .nonNumericCharacter:
105-
return "contain non-numeric characters"
106-
case .oversizedValue:
107-
return "be larger than 'UInt.max'"
108-
}
109-
}()
100+
let fault: String
101+
switch errorKind {
102+
case .leadingZeros: fault = "contain leading '0'"
103+
case .nonNumericCharacter: fault = "contain non-numeric characters"
104+
case .oversizedValue: fault = "be larger than 'UInt.max'"
105+
}
110106
return "semantic version \(position) identifier '\(identifier)' cannot \(fault)"
111107
case let .invalidVersionCoreIdentifierCount(identifiers):
112108
return """

Tests/SymbolKitTests/SymbolGraph/SemanticVersion/ErrorTests.swift

Lines changed: 68 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,14 @@ final class ErrorTests: XCTestCase {
201201
atPosition position: SymbolGraph.SemanticVersionError.IdentifierPosition,
202202
whenEvaluating expression: @autoclosure () throws -> SymbolGraph.SemanticVersion
203203
) {
204-
let positionString: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
205-
switch position {
206-
case .major: return "major"
207-
case .minor: return "minor"
208-
case .patch: return "patch"
209-
case .prerelease: return "prerelease"
210-
case .buildMetadata: return "buildMetadata"
211-
}
212-
}()
204+
let positionString: String
205+
switch position {
206+
case .major: positionString = "major"
207+
case .minor: positionString = "minor"
208+
case .patch: positionString = "patch"
209+
case .prerelease: positionString = "prerelease"
210+
case .buildMetadata: positionString = "buildMetadata"
211+
}
213212
XCTAssertThrowsError(
214213
try expression(),
215214
"'SymbolGraph.SemanticVersionError.emptyIdentifier(position: .\(positionString))' should've been thrown, but no error is thrown"
@@ -218,15 +217,14 @@ final class ErrorTests: XCTestCase {
218217
XCTFail(#"'SymbolGraph.SemanticVersionError.emptyIdentifier(position: .\#(positionString))' should've been thrown, but a different error is thrown instead; error description: "\#(error)""#)
219218
return
220219
}
221-
let positionDescription: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
222-
switch position {
223-
case .major: return "major version number"
224-
case .minor: return "minor version number"
225-
case .patch: return "patch version number"
226-
case .prerelease: return "pre-release"
227-
case .buildMetadata: return "build metadata"
228-
}
229-
}()
220+
let positionDescription: String
221+
switch position {
222+
case .major: positionDescription = "major version number"
223+
case .minor: positionDescription = "minor version number"
224+
case .patch: positionDescription = "patch version number"
225+
case .prerelease: positionDescription = "pre-release"
226+
case .buildMetadata: positionDescription = "build metadata"
227+
}
230228
XCTAssertEqual(
231229
error.description,
232230
"semantic version \(positionDescription) identifier cannot be empty"
@@ -239,12 +237,11 @@ final class ErrorTests: XCTestCase {
239237
inIdentifier identifier: Substring,
240238
whenEvaluating expression: @autoclosure () throws -> SymbolGraph.SemanticVersion
241239
) {
242-
let positionString: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
243-
switch position {
244-
case .prerelease: return "prerelease"
245-
case .buildMetadata: return "buildMetadata"
246-
}
247-
}()
240+
let positionString: String
241+
switch position {
242+
case .prerelease: positionString = "prerelease"
243+
case .buildMetadata: positionString = "buildMetadata"
244+
}
248245
XCTAssertThrowsError(
249246
try expression(),
250247
"'SymbolGraph.SemanticVersionError.invalidCharacterInIdentifier(\(identifier), position: .\(positionString))' should've been thrown, but no error is thrown"
@@ -253,12 +250,11 @@ final class ErrorTests: XCTestCase {
253250
XCTFail((#"'SymbolGraph.SemanticVersionError.invalidCharacterInIdentifier(\#(identifier), position: .\#(positionString))' should've been thrown, but a different error is thrown instead; error description: "\#(error)""#))
254251
return
255252
}
256-
let positionDescription: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
257-
switch position {
258-
case .prerelease: return "pre-release"
259-
case .buildMetadata: return "build metadata"
260-
}
261-
}()
253+
let positionDescription: String
254+
switch position {
255+
case .prerelease: positionDescription = "pre-release"
256+
case .buildMetadata: positionDescription = "build metadata"
257+
}
262258
XCTAssertEqual(
263259
error.description,
264260
"semantic version \(positionDescription) identifier '\(identifier)' cannot contain characters other than ASCII alphanumerics and hyphen-minus ([0-9A-Za-z-])"
@@ -271,14 +267,13 @@ final class ErrorTests: XCTestCase {
271267
inIdentifier identifier: Substring,
272268
whenEvaluating expression: @autoclosure () throws -> SymbolGraph.SemanticVersion
273269
) {
274-
let positionString: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
275-
switch position {
276-
case .major: return "major"
277-
case .minor: return "minor"
278-
case .patch: return "patch"
279-
case .prerelease: XCTFail("pre-release identifier with non-numeric characters should be regarded as alpha-numeric identifier"); return "prerelease"
280-
}
281-
}()
270+
let positionString: String
271+
switch position {
272+
case .major: positionString = "major"
273+
case .minor: positionString = "minor"
274+
case .patch: positionString = "patch"
275+
case .prerelease: XCTFail("pre-release identifier with non-numeric characters should be regarded as alpha-numeric identifier"); positionString = "prerelease"
276+
}
282277
XCTAssertThrowsError(
283278
try expression(),
284279
"'SymbolGraph.SemanticVersionError.invalidCharacterInIdentifier(\(identifier), position: .\(positionString))' should've been thrown, but no error is thrown"
@@ -287,14 +282,13 @@ final class ErrorTests: XCTestCase {
287282
XCTFail((#"'SymbolGraph.SemanticVersionError.invalidNumericIdentifier(\#(identifier), position: \#(positionString), errorKind: .nonNumericCharacter)' should've been thrown, but a different error is thrown instead; error description: "\#(error)""#))
288283
return
289284
}
290-
let positionDescription: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
291-
switch position {
292-
case .major: return "major version number"
293-
case .minor: return "minor version number"
294-
case .patch: return "patch version number"
295-
case .prerelease: XCTFail("pre-release identifier with non-numeric characters should be regarded as alpha-numeric identifier"); return "pre-release numeric"
296-
}
297-
}()
285+
let positionDescription: String
286+
switch position {
287+
case .major: positionDescription = "major version number"
288+
case .minor: positionDescription = "minor version number"
289+
case .patch: positionDescription = "patch version number"
290+
case .prerelease: XCTFail("pre-release identifier with non-numeric characters should be regarded as alpha-numeric identifier"); positionDescription = "pre-release numeric"
291+
}
298292
XCTAssertEqual(
299293
error.description,
300294
"semantic version \(positionDescription) identifier '\(identifier)' cannot contain non-numeric characters"
@@ -307,14 +301,13 @@ final class ErrorTests: XCTestCase {
307301
inIdentifier identifier: Substring,
308302
whenEvaluating expression: @autoclosure () throws -> SymbolGraph.SemanticVersion
309303
) {
310-
let positionString: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
311-
switch position {
312-
case .major: return "major"
313-
case .minor: return "minor"
314-
case .patch: return "patch"
315-
case .prerelease: return "prerelease"
316-
}
317-
}()
304+
let positionString: String
305+
switch position {
306+
case .major: positionString = "major"
307+
case .minor: positionString = "minor"
308+
case .patch: positionString = "patch"
309+
case .prerelease: positionString = "prerelease"
310+
}
318311
XCTAssertThrowsError(
319312
try expression(),
320313
"'SymbolGraph.SemanticVersionError.invalidCharacterInIdentifier(\(identifier), position: .\(positionString))' should've been thrown, but no error is thrown"
@@ -323,14 +316,13 @@ final class ErrorTests: XCTestCase {
323316
XCTFail((#"'SymbolGraph.SemanticVersionError.invalidNumericIdentifier(\#(identifier), position: \#(positionString), errorKind: .leadingZeros)' should've been thrown, but a different error is thrown instead; error description: "\#(error)""#))
324317
return
325318
}
326-
let positionDescription: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
327-
switch position {
328-
case .major: return "major version number"
329-
case .minor: return "minor version number"
330-
case .patch: return "patch version number"
331-
case .prerelease: return "pre-release numeric"
332-
}
333-
}()
319+
let positionDescription: String
320+
switch position {
321+
case .major: positionDescription = "major version number"
322+
case .minor: positionDescription = "minor version number"
323+
case .patch: positionDescription = "patch version number"
324+
case .prerelease: positionDescription = "pre-release numeric"
325+
}
334326
XCTAssertEqual(
335327
error.description,
336328
"semantic version \(positionDescription) identifier '\(identifier)' cannot contain leading '0'"
@@ -343,14 +335,13 @@ final class ErrorTests: XCTestCase {
343335
inIdentifier identifier: Substring,
344336
whenEvaluating expression: @autoclosure () throws -> SymbolGraph.SemanticVersion
345337
) {
346-
let positionString: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
347-
switch position {
348-
case .major: return "major"
349-
case .minor: return "minor"
350-
case .patch: return "patch"
351-
case .prerelease: return "prerelease"
352-
}
353-
}()
338+
let positionString: String
339+
switch position {
340+
case .major: positionString = "major"
341+
case .minor: positionString = "minor"
342+
case .patch: positionString = "patch"
343+
case .prerelease: positionString = "prerelease"
344+
}
354345
XCTAssertThrowsError(
355346
try expression(),
356347
"'SymbolGraph.SemanticVersionError.invalidCharacterInIdentifier(\(identifier), position: .\(positionString))' should've been thrown, but no error is thrown"
@@ -359,14 +350,13 @@ final class ErrorTests: XCTestCase {
359350
XCTFail((#"'SymbolGraph.SemanticVersionError.invalidNumericIdentifier(\#(identifier), position: \#(positionString), errorKind: .oversizedValue)' should've been thrown, but a different error is thrown instead; error description: "\#(error)""#))
360351
return
361352
}
362-
let positionDescription: String = { // FIXME: This type annotation can be removed in Swift ≥ 5.7
363-
switch position {
364-
case .major: return "major version number"
365-
case .minor: return "minor version number"
366-
case .patch: return "patch version number"
367-
case .prerelease: return "pre-release numeric"
368-
}
369-
}()
353+
let positionDescription: String
354+
switch position {
355+
case .major: positionDescription = "major version number"
356+
case .minor: positionDescription = "minor version number"
357+
case .patch: positionDescription = "patch version number"
358+
case .prerelease: positionDescription = "pre-release numeric"
359+
}
370360
XCTAssertEqual(
371361
error.description,
372362
"semantic version \(positionDescription) identifier '\(identifier)' cannot be larger than 'UInt.max'"

0 commit comments

Comments
 (0)