Skip to content

Commit 5fe414c

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. Remove the conditional casts and turn them into unconditional to avoid the warnings. 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 2e6971e commit 5fe414c

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

TestFoundation/TestFileManager.swift

+20-20
Original file line numberDiff line numberDiff line change
@@ -1273,81 +1273,81 @@ VIDEOS=StopgapVideos
12731273
XCTAssertNil(NSHomeDirectoryForUser(""))
12741274

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

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

12871287
XCTAssertThrowsError(try fm.createDirectory(atPath: "", withIntermediateDirectories: true)) {
1288-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1288+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
12891289
XCTAssertEqual(code, .fileReadInvalidFileName)
12901290
}
12911291
XCTAssertFalse(fm.createFile(atPath: "", contents: Data()))
12921292
XCTAssertThrowsError(try fm.removeItem(atPath: "")) {
1293-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1293+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
12941294
XCTAssertEqual(code, .fileReadInvalidFileName)
12951295
}
12961296

12971297
XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "/tmp/t")) {
1298-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1298+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
12991299
XCTAssertEqual(code, .fileReadInvalidFileName)
13001300
}
13011301
XCTAssertThrowsError(try fm.copyItem(atPath: "", toPath: "")) {
1302-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1302+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13031303
XCTAssertEqual(code, .fileReadInvalidFileName)
13041304
}
13051305
XCTAssertThrowsError(try fm.copyItem(atPath: "/tmp/t", toPath: "")) {
1306-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1306+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13071307
XCTAssertEqual(code, .fileReadNoSuchFile)
13081308
}
13091309

13101310
XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "/tmp/t")) {
1311-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1311+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13121312
XCTAssertEqual(code, .fileReadInvalidFileName)
13131313
}
13141314
XCTAssertThrowsError(try fm.moveItem(atPath: "", toPath: "")) {
1315-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1315+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13161316
XCTAssertEqual(code, .fileReadInvalidFileName)
13171317
}
13181318
XCTAssertThrowsError(try fm.moveItem(atPath: "/tmp/t", toPath: "")) {
1319-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1319+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13201320
XCTAssertEqual(code, .fileReadInvalidFileName)
13211321
}
13221322

13231323
XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "/tmp/t")) {
1324-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1324+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13251325
XCTAssertEqual(code, .fileReadInvalidFileName)
13261326
}
13271327
XCTAssertThrowsError(try fm.linkItem(atPath: "", toPath: "")) {
1328-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1328+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13291329
XCTAssertEqual(code, .fileReadInvalidFileName)
13301330
}
13311331
XCTAssertThrowsError(try fm.linkItem(atPath: "/tmp/t", toPath: "")) {
1332-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1332+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13331333
XCTAssertEqual(code, .fileReadNoSuchFile)
13341334
}
13351335

13361336
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "")) {
1337-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1337+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13381338
XCTAssertEqual(code, .fileReadInvalidFileName)
13391339
}
13401340
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "", withDestinationPath: "/tmp/t")) {
1341-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1341+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13421342
XCTAssertEqual(code, .fileReadInvalidFileName)
13431343
}
13441344
XCTAssertThrowsError(try fm.createSymbolicLink(atPath: "/tmp/t", withDestinationPath: "")) {
1345-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1345+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13461346
XCTAssertEqual(code, .fileReadInvalidFileName)
13471347
}
13481348

13491349
XCTAssertThrowsError(try fm.destinationOfSymbolicLink(atPath: "")) {
1350-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1350+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13511351
XCTAssertEqual(code, .fileReadInvalidFileName)
13521352
}
13531353
XCTAssertFalse(fm.fileExists(atPath: ""))
@@ -1358,15 +1358,15 @@ VIDEOS=StopgapVideos
13581358
XCTAssertTrue(fm.isDeletableFile(atPath: ""))
13591359

13601360
XCTAssertThrowsError(try fm.attributesOfItem(atPath: "")) {
1361-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1361+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13621362
XCTAssertEqual(code, .fileReadInvalidFileName)
13631363
}
13641364
XCTAssertThrowsError(try fm.attributesOfFileSystem(forPath: "")) {
1365-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1365+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13661366
XCTAssertEqual(code, .fileReadInvalidFileName)
13671367
}
13681368
XCTAssertThrowsError(try fm.setAttributes([:], ofItemAtPath: "")) {
1369-
let code = CocoaError.Code(rawValue: ($0 as? NSError)!.code)
1369+
let code = CocoaError.Code(rawValue: ($0 as NSError).code)
13701370
XCTAssertEqual(code, .fileReadInvalidFileName)
13711371
}
13721372

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"] as NSError?)
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 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))
105102
} else {
106103
XCTFail()
107104
}

TestFoundation/TestURL.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class TestURL : XCTestCase {
243243
try FileManager.default.removeItem(atPath: gFileDoesNotExistPath)
244244
} catch {
245245
// The error code is a CocoaError
246-
if (error as? NSError)?.code != CocoaError.fileNoSuchFile.rawValue {
246+
if (error as NSError).code != CocoaError.fileNoSuchFile.rawValue {
247247
return false
248248
}
249249
}
@@ -252,7 +252,7 @@ class TestURL : XCTestCase {
252252
try FileManager.default.createDirectory(atPath: gDirectoryExistsPath, withIntermediateDirectories: false)
253253
} catch {
254254
// The error code is a CocoaError
255-
if (error as? NSError)?.code != CocoaError.fileWriteFileExists.rawValue {
255+
if (error as NSError).code != CocoaError.fileWriteFileExists.rawValue {
256256
return false
257257
}
258258
}
@@ -261,7 +261,7 @@ class TestURL : XCTestCase {
261261
try FileManager.default.removeItem(atPath: gDirectoryDoesNotExistPath)
262262
} catch {
263263
// The error code is a CocoaError
264-
if (error as? NSError)?.code != CocoaError.fileNoSuchFile.rawValue {
264+
if (error as NSError).code != CocoaError.fileNoSuchFile.rawValue {
265265
return false
266266
}
267267
}
@@ -570,11 +570,8 @@ class TestURL : XCTestCase {
570570
assertRelevantValuesAreEqual(in: newValues)
571571
}
572572
} catch {
573-
if let error = error as? NSError {
574-
print("error: \(error.description) - \(error.userInfo)")
575-
} else {
576-
print("error: \(error)")
577-
}
573+
let error = error as NSError
574+
print("error: \(error.description) - \(error.userInfo)")
578575
throw error
579576
}
580577
}

0 commit comments

Comments
 (0)