Skip to content

Commit 373133f

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 8043670 commit 373133f

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
@@ -1274,81 +1274,81 @@ VIDEOS=StopgapVideos
12741274
XCTAssertNil(NSHomeDirectoryForUser(""))
12751275

12761276
XCTAssertThrowsError(try fm.contentsOfDirectory(atPath: "")) {
1277-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1277+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
12781278
XCTAssertEqual(code, .fileReadInvalidFileName)
12791279
}
12801280

12811281
XCTAssertNil(fm.enumerator(atPath: ""))
12821282
XCTAssertNil(fm.subpaths(atPath: ""))
12831283
XCTAssertThrowsError(try fm.subpathsOfDirectory(atPath: "")) {
1284-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1284+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
12851285
XCTAssertEqual(code, .fileReadInvalidFileName)
12861286
}
12871287

12881288
XCTAssertThrowsError(try fm.createDirectory(atPath: "", withIntermediateDirectories: true)) {
1289-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1289+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
12901290
XCTAssertEqual(code, .fileReadInvalidFileName)
12911291
}
12921292
XCTAssertFalse(fm.createFile(atPath: "", contents: Data()))
12931293
XCTAssertThrowsError(try fm.removeItem(atPath: "")) {
1294-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1294+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
12951295
XCTAssertEqual(code, .fileReadInvalidFileName)
12961296
}
12971297

12981298
XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "/tmp/t")) {
1299-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1299+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13001300
XCTAssertEqual(code, .fileReadInvalidFileName)
13011301
}
13021302
XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "")) {
1303-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1303+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13041304
XCTAssertEqual(code, .fileReadInvalidFileName)
13051305
}
13061306
XCTAssertThrowsError(try fm.copyItem(atPath: "/tmp/t", toPath: "")) {
1307-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1307+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13081308
XCTAssertEqual(code, .fileReadNoSuchFile)
13091309
}
13101310

13111311
XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "/tmp/t")) {
1312-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1312+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13131313
XCTAssertEqual(code, .fileReadInvalidFileName)
13141314
}
13151315
XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "")) {
1316-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1316+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13171317
XCTAssertEqual(code, .fileReadInvalidFileName)
13181318
}
13191319
XCTAssertThrowsError(try fm.moveItem(atPath: "/tmp/t", toPath: "")) {
1320-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1320+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13211321
XCTAssertEqual(code, .fileReadInvalidFileName)
13221322
}
13231323

13241324
XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "/tmp/t")) {
1325-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1325+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13261326
XCTAssertEqual(code, .fileReadInvalidFileName)
13271327
}
13281328
XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "")) {
1329-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1329+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13301330
XCTAssertEqual(code, .fileReadInvalidFileName)
13311331
}
13321332
XCTAssertThrowsError(try fm.linkItem(atPath: "/tmp/t", toPath: "")) {
1333-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1333+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13341334
XCTAssertEqual(code, .fileReadNoSuchFile)
13351335
}
13361336

13371337
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "")) {
1338-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1338+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13391339
XCTAssertEqual(code, .fileReadInvalidFileName)
13401340
}
13411341
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "/tmp/t")) {
1342-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1342+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13431343
XCTAssertEqual(code, .fileReadInvalidFileName)
13441344
}
13451345
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "/tmp/t", withDestinationPath: "")) {
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

13501350
XCTAssertThrowsError(try fm.destinationOfSymbolicLink(atPath: "")) {
1351-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1351+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13521352
XCTAssertEqual(code, .fileReadInvalidFileName)
13531353
}
13541354
XCTAssertFalse(fm.fileExists(atPath: ""))
@@ -1359,15 +1359,15 @@ VIDEOS=StopgapVideos
13591359
XCTAssertTrue(fm.isDeletableFile(atPath: ""))
13601360

13611361
XCTAssertThrowsError(try fm.attributesOfItem(atPath: "")) {
1362-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1362+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13631363
XCTAssertEqual(code, .fileReadInvalidFileName)
13641364
}
13651365
XCTAssertThrowsError(try fm.attributesOfFileSystem(forPath: "")) {
1366-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1366+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13671367
XCTAssertEqual(code, .fileReadInvalidFileName)
13681368
}
13691369
XCTAssertThrowsError(try fm.setAttributes([:], ofItemAtPath: "")) {
1370-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1370+
let code = CocoaError.Code(rawValue: $0.asNSError().code)
13711371
XCTAssertEqual(code, .fileReadInvalidFileName)
13721372
}
13731373

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
@@ -112,6 +112,17 @@ func _checkHashing<Source: Hashable, Target: Hashable, S: Sequence>(
112112
}
113113
}
114114

115+
extension Error {
116+
func asNSError() -> NSError {
117+
// The cast will never fail, and this will create a warning during
118+
// compilation, but since it is SwiftFoundation.NSError (and not ObjC
119+
// NSError) the macOS build cannot detect that and might fail indicating
120+
// some catch is not exhaustive. This avoid the compilation problem,
121+
// but centralizes all the warnings in only this point.
122+
return (self as? NSError)!
123+
}
124+
}
125+
115126
enum TestError: Error {
116127
case unexpectedNil
117128
}

0 commit comments

Comments
 (0)