Skip to content

Commit 7846ffe

Browse files
committed
let 'swiftVerRegex' is not concurrency-safe because non-'Sendable' type 'NSRegularExpression' may have shared mutable state
1 parent 089e0a6 commit 7846ffe

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

Sources/App/Core/SwiftVersion.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ struct SwiftVersion: Content, Equatable, Hashable {
3030

3131
extension SwiftVersion: LosslessStringConvertible {
3232
init?(_ string: String) {
33-
let groups = swiftVerRegex.matchGroups(string)
33+
let groups = Self.swiftVerRegex.matchGroups(string)
3434
guard
35-
groups.count == swiftVerRegex.numberOfCaptureGroups,
35+
groups.count == Self.swiftVerRegex.numberOfCaptureGroups,
3636
let major = Int(groups[0])
3737
else { return nil }
3838
self = .init(major, Int(groups[1]) ?? 0, Int(groups[2]) ?? 0)
@@ -71,6 +71,24 @@ extension SwiftVersion: LosslessStringConvertible {
7171

7272
}
7373
}
74+
75+
static var swiftVerRegex: NSRegularExpression {
76+
NSRegularExpression(
77+
#"""
78+
^
79+
v? # SPI extension: allow leading 'v'
80+
(?<major>0|[1-9]\d*)
81+
(?:\.
82+
(?<minor>0|[1-9]\d*)
83+
)?
84+
(?:\.
85+
(?<patch>0|[1-9]\d*)
86+
)?
87+
$
88+
"""#, options: [.allowCommentsAndWhitespace]
89+
)
90+
}
91+
7492
}
7593

7694

@@ -100,17 +118,3 @@ extension SwiftVersion {
100118
extension SwiftVersion {
101119
static var latest: Self { allActive.sorted().last! }
102120
}
103-
104-
105-
let swiftVerRegex = NSRegularExpression(#"""
106-
^
107-
v? # SPI extension: allow leading 'v'
108-
(?<major>0|[1-9]\d*)
109-
(?:\.
110-
(?<minor>0|[1-9]\d*)
111-
)?
112-
(?:\.
113-
(?<patch>0|[1-9]\d*)
114-
)?
115-
$
116-
"""#, options: [.allowCommentsAndWhitespace])

Tests/AppTests/SwiftVersionTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import XCTest
2020
class SwiftVersionTests: XCTestCase {
2121

2222
func test_swiftVerRegex() throws {
23-
XCTAssert(swiftVerRegex.matches("1"))
24-
XCTAssert(swiftVerRegex.matches("1.2"))
25-
XCTAssert(swiftVerRegex.matches("1.2.3"))
26-
XCTAssert(swiftVerRegex.matches("v1"))
27-
XCTAssertFalse(swiftVerRegex.matches("1."))
28-
XCTAssertFalse(swiftVerRegex.matches("1.2."))
29-
XCTAssertFalse(swiftVerRegex.matches("1.2.3-pre"))
23+
XCTAssert(SwiftVersion.swiftVerRegex.matches("1"))
24+
XCTAssert(SwiftVersion.swiftVerRegex.matches("1.2"))
25+
XCTAssert(SwiftVersion.swiftVerRegex.matches("1.2.3"))
26+
XCTAssert(SwiftVersion.swiftVerRegex.matches("v1"))
27+
XCTAssertFalse(SwiftVersion.swiftVerRegex.matches("1."))
28+
XCTAssertFalse(SwiftVersion.swiftVerRegex.matches("1.2."))
29+
XCTAssertFalse(SwiftVersion.swiftVerRegex.matches("1.2.3-pre"))
3030
}
3131

3232
func test_init() throws {

0 commit comments

Comments
 (0)