Skip to content

Commit b4b2dad

Browse files
authored
Fix concurrency warnings for more objective-c/foundation types (#1165)
1 parent 322f9be commit b4b2dad

File tree

7 files changed

+17
-9
lines changed

7 files changed

+17
-9
lines changed

Sources/Nimble/Adapters/NMBExpectation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private func from(objcMatcher: NMBMatcher) -> Matcher<NSObject> {
1414

1515
// Equivalent to Expectation, but for Nimble's Objective-C interface
1616
public final class NMBExpectation: NSObject, Sendable {
17-
internal let _actualBlock: @Sendable () -> NSObject?
17+
internal let _actualBlock: @Sendable () -> sending NSObject?
1818
internal let _negative: Bool
1919
internal let _file: FileString
2020
internal let _line: UInt

Sources/Nimble/Matchers/AllPass.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ extension NMBMatcher {
100100
)
101101
}
102102

103-
let expr = Expression(expression: ({ nsObjects }), location: location)
103+
let immutableCollection = nsObjects
104+
105+
let expr = Expression(expression: ({ immutableCollection }), location: location)
104106
let pred: Matcher<[NSObject]> = createMatcher(Matcher { expr in
105107
return matcher.satisfies(({ try expr.evaluate() }), location: expr.location).toSwift()
106108
})

Sources/Nimble/Matchers/BeCloseTo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private func beCloseTo(
6767
}
6868

6969
#if canImport(Darwin)
70-
public class NMBObjCBeCloseToMatcher: NMBMatcher {
70+
public final class NMBObjCBeCloseToMatcher: NMBMatcher, @unchecked Sendable {
7171
private let _expected: NSNumber
7272

7373
fileprivate init(expected: NSNumber, within: CDouble) {

Sources/Nimble/Matchers/BeEmpty.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ extension NMBMatcher {
8888
let expr = Expression(expression: { value }, location: location)
8989
return try beEmpty().satisfies(expr).toObjectiveC()
9090
} else if let value = actualValue as? NSString {
91-
let expr = Expression(expression: { value }, location: location)
91+
let stringValue = String(value)
92+
let expr = Expression(expression: { stringValue }, location: location)
9293
return try beEmpty().satisfies(expr).toObjectiveC()
9394
} else if let actualValue = actualValue {
9495
let badTypeErrorMsg = "be empty (only works for NSArrays, NSSets, NSIndexSets, NSDictionaries, NSHashTables, and NSStrings)"

Sources/Nimble/Matchers/Contain.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ public func contain(_ substrings: NSString...) -> Matcher<NSString> {
7777
}
7878

7979
public func contain(_ substrings: [NSString]) -> Matcher<NSString> {
80+
let stringSubstrings = substrings.map { String($0) }
8081
return Matcher.simple("contain <\(arrayAsString(substrings))>") { actualExpression in
8182
guard let actual = try actualExpression.evaluate() else { return .fail }
83+
let actualString = String(actual)
8284

83-
let matches = substrings.allSatisfy { actual.range(of: $0.description).length != 0 }
85+
let matches = stringSubstrings.allSatisfy { string in
86+
actualString.contains(string)
87+
}
8488
return MatcherStatus(bool: matches)
8589
}
8690
}
@@ -115,7 +119,8 @@ extension NMBMatcher {
115119
let expectedOptionals: [Any?] = expected.map({ $0 as Any? })
116120
return try contain(expectedOptionals).satisfies(expr).toObjectiveC()
117121
} else if let value = actualValue as? NSString {
118-
let expr = Expression(expression: ({ value as String }), location: location)
122+
let stringValue = String(value)
123+
let expr = Expression(expression: ({ stringValue }), location: location)
119124
// swiftlint:disable:next force_cast
120125
return try contain(expected as! [String]).satisfies(expr).toObjectiveC()
121126
}

Sources/Nimble/Matchers/Matcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public class NMBMatcher: NSObject, @unchecked Sendable {
225225
self.init(matcher: predicate)
226226
}
227227

228-
func satisfies(_ expression: @escaping @Sendable () throws -> NSObject?, location: SourceLocation) -> NMBMatcherResult {
228+
func satisfies(_ expression: @escaping @Sendable () throws -> sending NSObject?, location: SourceLocation) -> NMBMatcherResult {
229229
let expr = Expression(expression: expression, location: location)
230230
do {
231231
return try self.matcher(expr)

Tests/NimbleTests/Matchers/BeAKindOfTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import Nimble
55
import NimbleSharedTestHelpers
66
#endif
77

8-
private class TestNull: NSNull {}
8+
private final class TestNull: NSNull, @unchecked Sendable {}
99
private protocol TestProtocol {}
10-
private class TestClassConformingToProtocol: TestProtocol {}
10+
private final class TestClassConformingToProtocol: TestProtocol {}
1111
private struct TestStructConformingToProtocol: TestProtocol {}
1212

1313
final class BeAKindOfSwiftTest: XCTestCase {

0 commit comments

Comments
 (0)