Skip to content

[gardening] Clean up do catch blocks #1627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,7 @@ open class FileManager : NSObject {
/* These methods are provided here for compatibility. The corresponding methods on NSData which return NSErrors should be regarded as the primary method of creating a file from an NSData or retrieving the contents of a file as an NSData.
*/
open func contents(atPath path: String) -> Data? {
do {
return try Data(contentsOf: URL(fileURLWithPath: path))
} catch {
return nil
}
return try? Data(contentsOf: URL(fileURLWithPath: path))
}

open func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey : Any]? = nil) -> Bool {
Expand All @@ -971,7 +967,7 @@ open class FileManager : NSObject {
try self.setAttributes(attr, ofItemAtPath: path)
}
return true
} catch _ {
} catch {
return false
}
}
Expand Down
16 changes: 6 additions & 10 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,22 +579,18 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
if fsync(fd) < 0 {
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
} catch let err {
} catch {
if let auxFilePath = auxFilePath {
do {
try FileManager.default.removeItem(atPath: auxFilePath)
} catch _ {}
try? FileManager.default.removeItem(atPath: auxFilePath)
}
throw err
throw error
}
}
}
if let auxFilePath = auxFilePath {
try fm._fileSystemRepresentation(withPath: auxFilePath, { auxFilePathFsRep in
if rename(auxFilePathFsRep, pathFsRep) != 0 {
do {
try FileManager.default.removeItem(atPath: auxFilePath)
} catch _ {}
try? FileManager.default.removeItem(atPath: auxFilePath)
throw _NSErrorWithErrno(errno, reading: false, path: path)
}
if let mode = mode {
Expand Down Expand Up @@ -705,8 +701,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
self.enumerateBytes() { (buf, range, stop) -> Void in
do {
try block(buf, range, stop)
} catch let e {
err = e
} catch {
err = error
}
}
if let err = err {
Expand Down
13 changes: 5 additions & 8 deletions Foundation/NSKeyedArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,15 @@ open class NSKeyedArchiver : NSCoder {

do {
(fd, auxFilePath) = try _NSCreateTemporaryFile(path)
} catch _ {
} catch {
return false
}

defer {
do {
if finishedEncoding {
try _NSCleanupTemporaryFile(auxFilePath, path)
} else {
try FileManager.default.removeItem(atPath: auxFilePath)
}
} catch _ {
if finishedEncoding {
try? _NSCleanupTemporaryFile(auxFilePath, path)
} else {
try? FileManager.default.removeItem(atPath: auxFilePath)
}
}

Expand Down
6 changes: 1 addition & 5 deletions Foundation/NSKeyedUnarchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ open class NSKeyedUnarchiver : NSCoder {
}

open class func unarchiveObject(with data: Data) -> Any? {
do {
return try unarchiveTopLevelObjectWithData(data)
} catch {
}
return nil
return try? unarchiveTopLevelObjectWithData(data)
}

open class func unarchiveObject(withFile path: String) -> Any? {
Expand Down
14 changes: 6 additions & 8 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,13 @@ internal func _createRegexForPattern(_ pattern: String, _ options: NSRegularExpr
if let regex = local.__NSRegularExpressionCache.object(forKey: key._nsObject) {
return regex
}
do {
let regex = try NSRegularExpression(pattern: pattern, options: options)
local.__NSRegularExpressionCache.setObject(regex, forKey: key._nsObject)
return regex
} catch {


guard let regex = try? NSRegularExpression(pattern: pattern, options: options) else {
return nil
}

return nil

local.__NSRegularExpressionCache.setObject(regex, forKey: key._nsObject)
return regex
}

internal func _bytesInEncoding(_ str: NSString, _ encoding: String.Encoding, _ fatalOnError: Bool, _ externalRep: Bool, _ lossy: Bool) -> UnsafePointer<Int8>? {
Expand Down
20 changes: 6 additions & 14 deletions TestFoundation/TestBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class BundlePlayground {
}

self.bundlePath = bundleURL.path
} catch _ {
} catch {
return false
}

Expand Down Expand Up @@ -176,7 +176,7 @@ class BundlePlayground {
}

self.bundlePath = resourcesDirectory.path
} catch _ {
} catch {
return false
}

Expand Down Expand Up @@ -210,7 +210,7 @@ class BundlePlayground {
}

self.bundlePath = resourcesDirectory.path
} catch _ {
} catch {
return false
}
}
Expand All @@ -222,12 +222,8 @@ class BundlePlayground {
func destroy() {
guard let path = self.playgroundPath else { return }
self.playgroundPath = nil

do {
try FileManager.default.removeItem(atPath: path)
} catch _ {
// ¯\_(ツ)_/¯ We did what we could.
}

try? FileManager.default.removeItem(atPath: path)
}

deinit {
Expand Down Expand Up @@ -363,11 +359,7 @@ class TestBundle : XCTestCase {
}

private func _cleanupPlayground(_ location: String) {
do {
try FileManager.default.removeItem(atPath: location)
} catch _ {
// Oh well
}
try? FileManager.default.removeItem(atPath: location)
}

func test_URLsForResourcesWithExtension() {
Expand Down
36 changes: 18 additions & 18 deletions TestFoundation/TestCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class TestCodable : XCTestCase {
for components in personNameComponentsValues {
do {
try expectRoundTripEqualityThroughJSON(for: components)
} catch let error {
} catch {
XCTFail("\(error) for \(components)")
}
}
Expand All @@ -103,7 +103,7 @@ class TestCodable : XCTestCase {
// We have to wrap the UUID since we cannot have a top-level string.
do {
try expectRoundTripEqualityThroughJSON(for: UUIDCodingWrapper(uuid))
} catch let error {
} catch {
XCTFail("\(error) for \(uuid)")
}
}
Expand All @@ -122,7 +122,7 @@ class TestCodable : XCTestCase {
for url in urlValues {
do {
try expectRoundTripEqualityThroughJSON(for: url)
} catch let error {
} catch {
XCTFail("\(error) for \(url)")
}
}
Expand All @@ -139,7 +139,7 @@ class TestCodable : XCTestCase {
for range in nsrangeValues {
do {
try expectRoundTripEqualityThroughJSON(for: range)
} catch let error {
} catch {
XCTFail("\(error) for \(range)")
}
}
Expand All @@ -161,7 +161,7 @@ class TestCodable : XCTestCase {
for locale in localeValues {
do {
try expectRoundTripEqualityThroughJSON(for: locale)
} catch let error {
} catch {
XCTFail("\(error) for \(locale)")
}
}
Expand All @@ -178,7 +178,7 @@ class TestCodable : XCTestCase {
for indexSet in indexSetValues {
do {
try expectRoundTripEqualityThroughJSON(for: indexSet)
} catch let error {
} catch {
XCTFail("\(error) for \(indexSet)")
}
}
Expand All @@ -196,7 +196,7 @@ class TestCodable : XCTestCase {
for indexPath in indexPathValues {
do {
try expectRoundTripEqualityThroughJSON(for: indexPath)
} catch let error {
} catch {
XCTFail("\(error) for \(indexPath)")
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ class TestCodable : XCTestCase {
for transform in affineTransformValues {
do {
try expectRoundTripEqualityThroughJSON(for: transform)
} catch let error {
} catch {
XCTFail("\(error) for \(transform)")
}
}
Expand All @@ -244,7 +244,7 @@ class TestCodable : XCTestCase {
for decimal in decimalValues {
do {
try expectRoundTripEqualityThroughJSON(for: decimal)
} catch let error {
} catch {
XCTFail("\(error) for \(decimal)")
}
}
Expand All @@ -264,7 +264,7 @@ class TestCodable : XCTestCase {
for point in cgpointValues {
do {
try expectRoundTripEqualityThroughJSON(for: point)
} catch let error {
} catch {
XCTFail("\(error) for \(point)")
}
}
Expand All @@ -284,7 +284,7 @@ class TestCodable : XCTestCase {
for size in cgsizeValues {
do {
try expectRoundTripEqualityThroughJSON(for: size)
} catch let error {
} catch {
XCTFail("\(error) for \(size)")
}
}
Expand All @@ -305,7 +305,7 @@ class TestCodable : XCTestCase {
for rect in cgrectValues {
do {
try expectRoundTripEqualityThroughJSON(for: rect)
} catch let error {
} catch {
XCTFail("\(error) for \(rect)")
}
}
Expand Down Expand Up @@ -335,7 +335,7 @@ class TestCodable : XCTestCase {
for characterSet in characterSetValues {
do {
try expectRoundTripEqualityThroughJSON(for: characterSet)
} catch let error {
} catch {
XCTFail("\(error) for \(characterSet)")
}
}
Expand Down Expand Up @@ -366,7 +366,7 @@ class TestCodable : XCTestCase {
for timeZone in timeZoneValues {
do {
try expectRoundTripEqualityThroughJSON(for: timeZone)
} catch let error {
} catch {
XCTFail("\(error) for \(timeZone)")
}
}
Expand Down Expand Up @@ -404,7 +404,7 @@ class TestCodable : XCTestCase {
for calendar in calendarValues {
do {
try expectRoundTripEqualityThroughJSON(for: calendar)
} catch let error {
} catch {
XCTFail("\(error) for \(calendar)")
}
}
Expand Down Expand Up @@ -441,7 +441,7 @@ class TestCodable : XCTestCase {
let components = calendar.dateComponents(dateComponents, from: Date(timeIntervalSince1970: 1501283776))
do {
try expectRoundTripEqualityThroughJSON(for: components)
} catch let error {
} catch {
XCTFail("\(error)")
}
}
Expand All @@ -452,7 +452,7 @@ class TestCodable : XCTestCase {
try expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitAcceleration.metersPerSecondSquared))
try expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitMass.kilograms))
try expectRoundTripEqualityThroughJSON(for: Measurement(value: 42, unit: UnitLength.miles))
} catch let error {
} catch {
XCTFail("\(error)")
}
}
Expand Down Expand Up @@ -548,7 +548,7 @@ class TestCodable : XCTestCase {
for (components) in urlComponentsValues {
do {
try expectRoundTripEqualityThroughJSON(for: components)
} catch let error {
} catch {
XCTFail("\(error)")
}
}
Expand Down
Loading