Skip to content

Commit 1ab1299

Browse files
committed
[gardening] Remove conditional casts from Error to NSError.
Since Swift 3 (IIRC) Error can always be casted to NSError, so the conditional casts are always going to succeed and they produce warnings. However because in macOS SwiftFoundation.NSError is not the ObjC NSError, the compiler misses this, and might fail to mark some catch as exhaustives. To avoid one warning per use of NSError, move all the warnings into a single one in a utility function, used from all the places when Error needs to be transformed into NSError. In a couple of cases, the code is simplified because a branch disappears. This should remove a lot of warnings while compiling the tests.
1 parent 2b3a0af commit 1ab1299

File tree

4 files changed

+42
-37
lines changed

4 files changed

+42
-37
lines changed

TestFoundation/TestFileManager.swift

+20-20
Original file line numberDiff line numberDiff line change
@@ -1321,81 +1321,81 @@ VIDEOS=StopgapVideos
13211321
XCTAssertNil(NSHomeDirectoryForUser(""))
13221322

13231323
XCTAssertThrowsError(try fm.contentsOfDirectory(atPath: "")) {
1324-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1324+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13251325
XCTAssertEqual(code, .fileReadInvalidFileName)
13261326
}
13271327

13281328
XCTAssertNil(fm.enumerator(atPath: ""))
13291329
XCTAssertNil(fm.subpaths(atPath: ""))
13301330
XCTAssertThrowsError(try fm.subpathsOfDirectory(atPath: "")) {
1331-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1331+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13321332
XCTAssertEqual(code, .fileReadInvalidFileName)
13331333
}
13341334

13351335
XCTAssertThrowsError(try fm.createDirectory(atPath: "", withIntermediateDirectories: true)) {
1336-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1336+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13371337
XCTAssertEqual(code, .fileReadInvalidFileName)
13381338
}
13391339
XCTAssertFalse(fm.createFile(atPath: "", contents: Data()))
13401340
XCTAssertThrowsError(try fm.removeItem(atPath: "")) {
1341-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1341+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13421342
XCTAssertEqual(code, .fileReadInvalidFileName)
13431343
}
13441344

13451345
XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "/tmp/t")) {
1346-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1346+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13471347
XCTAssertEqual(code, .fileReadInvalidFileName)
13481348
}
13491349
XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "")) {
1350-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1350+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13511351
XCTAssertEqual(code, .fileReadInvalidFileName)
13521352
}
13531353
XCTAssertThrowsError(try fm.copyItem(atPath: "/tmp/t", toPath: "")) {
1354-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1354+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13551355
XCTAssertEqual(code, .fileReadNoSuchFile)
13561356
}
13571357

13581358
XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "/tmp/t")) {
1359-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1359+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13601360
XCTAssertEqual(code, .fileReadInvalidFileName)
13611361
}
13621362
XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "")) {
1363-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1363+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13641364
XCTAssertEqual(code, .fileReadInvalidFileName)
13651365
}
13661366
XCTAssertThrowsError(try fm.moveItem(atPath: "/tmp/t", toPath: "")) {
1367-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1367+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13681368
XCTAssertEqual(code, .fileReadInvalidFileName)
13691369
}
13701370

13711371
XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "/tmp/t")) {
1372-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1372+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13731373
XCTAssertEqual(code, .fileReadInvalidFileName)
13741374
}
13751375
XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "")) {
1376-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1376+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13771377
XCTAssertEqual(code, .fileReadInvalidFileName)
13781378
}
13791379
XCTAssertThrowsError(try fm.linkItem(atPath: "/tmp/t", toPath: "")) {
1380-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1380+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13811381
XCTAssertEqual(code, .fileReadNoSuchFile)
13821382
}
13831383

13841384
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "")) {
1385-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1385+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13861386
XCTAssertEqual(code, .fileReadInvalidFileName)
13871387
}
13881388
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "/tmp/t")) {
1389-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1389+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13901390
XCTAssertEqual(code, .fileReadInvalidFileName)
13911391
}
13921392
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "/tmp/t", withDestinationPath: "")) {
1393-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1393+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13941394
XCTAssertEqual(code, .fileReadInvalidFileName)
13951395
}
13961396

13971397
XCTAssertThrowsError(try fm.destinationOfSymbolicLink(atPath: "")) {
1398-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1398+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13991399
XCTAssertEqual(code, .fileReadInvalidFileName)
14001400
}
14011401
XCTAssertFalse(fm.fileExists(atPath: ""))
@@ -1406,15 +1406,15 @@ VIDEOS=StopgapVideos
14061406
XCTAssertTrue(fm.isDeletableFile(atPath: ""))
14071407

14081408
XCTAssertThrowsError(try fm.attributesOfItem(atPath: "")) {
1409-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1409+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
14101410
XCTAssertEqual(code, .fileReadInvalidFileName)
14111411
}
14121412
XCTAssertThrowsError(try fm.attributesOfFileSystem(forPath: "")) {
1413-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1413+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
14141414
XCTAssertEqual(code, .fileReadInvalidFileName)
14151415
}
14161416
XCTAssertThrowsError(try fm.setAttributes([:], ofItemAtPath: "")) {
1417-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1417+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
14181418
XCTAssertEqual(code, .fileReadInvalidFileName)
14191419
}
14201420

TestFoundation/TestNSError.swift

+6-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class TestNSError : XCTestCase {
4646
let nsdictionary = ["error": error] as NSDictionary
4747
let dictionary = nsdictionary as? Dictionary<String, Error>
4848
XCTAssertNotNil(dictionary)
49-
XCTAssertEqual(error, dictionary?["error"] as? NSError)
49+
XCTAssertEqual(error, dictionary?["error"]?.asNSError())
5050
}
5151

5252
func test_CustomNSError_domain() {
@@ -94,14 +94,11 @@ class TestNSError : XCTestCase {
9494
func test_errorConvenience() {
9595
let error = CocoaError.error(.fileReadNoSuchFile, url: URL(fileURLWithPath: #file))
9696

97-
if let nsError = error as? NSError {
98-
XCTAssertEqual(nsError._domain, NSCocoaErrorDomain)
99-
XCTAssertEqual(nsError._code, CocoaError.fileReadNoSuchFile.rawValue)
100-
if let filePath = nsError.userInfo[NSURLErrorKey] as? URL {
101-
XCTAssertEqual(filePath, URL(fileURLWithPath: #file))
102-
} else {
103-
XCTFail()
104-
}
97+
let nsError = error.asNSError()
98+
XCTAssertEqual(nsError.domain, NSCocoaErrorDomain)
99+
XCTAssertEqual(nsError.code, CocoaError.fileReadNoSuchFile.rawValue)
100+
if let filePath = nsError.userInfo[NSURLErrorKey] as? URL {
101+
XCTAssertEqual(filePath, URL(fileURLWithPath: #file))
105102
} else {
106103
XCTFail()
107104
}

TestFoundation/TestURL.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class TestURL : XCTestCase {
297297
try FileManager.default.removeItem(atPath: gFileDoesNotExistPath)
298298
} catch {
299299
// The error code is a CocoaError
300-
if (error as? NSError)?.code != CocoaError.fileNoSuchFile.rawValue {
300+
if error.asNSError().code != CocoaError.fileNoSuchFile.rawValue {
301301
return false
302302
}
303303
}
@@ -306,7 +306,7 @@ class TestURL : XCTestCase {
306306
try FileManager.default.createDirectory(atPath: gDirectoryExistsPath, withIntermediateDirectories: false)
307307
} catch {
308308
// The error code is a CocoaError
309-
if (error as? NSError)?.code != CocoaError.fileWriteFileExists.rawValue {
309+
if error.asNSError().code != CocoaError.fileWriteFileExists.rawValue {
310310
return false
311311
}
312312
}
@@ -315,7 +315,7 @@ class TestURL : XCTestCase {
315315
try FileManager.default.removeItem(atPath: gDirectoryDoesNotExistPath)
316316
} catch {
317317
// The error code is a CocoaError
318-
if (error as? NSError)?.code != CocoaError.fileNoSuchFile.rawValue {
318+
if error.asNSError().code != CocoaError.fileNoSuchFile.rawValue {
319319
return false
320320
}
321321
}
@@ -624,11 +624,8 @@ class TestURL : XCTestCase {
624624
assertRelevantValuesAreEqual(in: newValues)
625625
}
626626
} catch {
627-
if let error = error as? NSError {
628-
print("error: \(error.description) - \(error.userInfo)")
629-
} else {
630-
print("error: \(error)")
631-
}
627+
let error = error.asNSError()
628+
print("error: \(error.description) - \(error.userInfo)")
632629
throw error
633630
}
634631
}

TestFoundation/Utilities.swift

+11
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ func _checkHashing<Source: Hashable, Target: Hashable, S: Sequence>(
133133
}
134134
}
135135

136+
extension Error {
137+
func asNSError() -> NSError {
138+
// The cast will never fail, and this will create a warning during
139+
// compilation, but since it is SwiftFoundation.NSError (and not ObjC
140+
// NSError) the macOS build cannot detect that and might fail indicating
141+
// some catch is not exhaustive. This avoid the compilation problem,
142+
// but centralizes all the warnings in only this point.
143+
return (self as? NSError)!
144+
}
145+
}
146+
136147
enum TestError: Error {
137148
case unexpectedNil
138149
}

0 commit comments

Comments
 (0)