Skip to content

Commit 30efb6f

Browse files
committed
[gardening] Use optional binding
1 parent fdf6bb4 commit 30efb6f

12 files changed

+53
-65
lines changed

Foundation/Data.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ public final class _DataStorage {
343343
let tryCalloc = (origLength == 0 || (newLength / origLength) >= 4)
344344
if allocateCleared && tryCalloc {
345345
newBytes = _DataStorage.allocate(newCapacity, true)
346-
if newBytes != nil {
347-
_DataStorage.move(newBytes!, _bytes!, origLength)
346+
if let newBytes = newBytes {
347+
_DataStorage.move(newBytes, _bytes!, origLength)
348348
_freeBytes()
349349
}
350350
}
@@ -353,8 +353,8 @@ public final class _DataStorage {
353353
allocateCleared = false
354354
if _deallocator != nil {
355355
newBytes = _DataStorage.allocate(newCapacity, true)
356-
if newBytes != nil {
357-
_DataStorage.move(newBytes!, _bytes!, origLength)
356+
if let newBytes = newBytes {
357+
_DataStorage.move(newBytes, _bytes!, origLength)
358358
_freeBytes()
359359
_deallocator = nil
360360
}
@@ -369,8 +369,8 @@ public final class _DataStorage {
369369
allocateCleared = clear && _DataStorage.shouldAllocateCleared(newCapacity)
370370
if allocateCleared && tryCalloc {
371371
newBytes = _DataStorage.allocate(newCapacity, true)
372-
if newBytes != nil {
373-
_DataStorage.move(newBytes!, _bytes!, origLength)
372+
if let newBytes = newBytes {
373+
_DataStorage.move(newBytes, _bytes!, origLength)
374374
_freeBytes()
375375
}
376376
}
@@ -619,8 +619,8 @@ public final class _DataStorage {
619619
memmove(mutableBytes! + start + replacementLength, mutableBytes! + start + length, currentLength - start - length)
620620
}
621621
if replacementLength != 0 {
622-
if replacementBytes != nil {
623-
memmove(mutableBytes! + start, replacementBytes!, replacementLength)
622+
if let replacementBytes = replacementBytes {
623+
memmove(mutableBytes! + start, replacementBytes, replacementLength)
624624
} else {
625625
memset(mutableBytes! + start, 0, replacementLength)
626626
}

Foundation/FileManager.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,13 @@ open class FileManager : NSObject {
296296
result[.systemNumber] = NSNumber(value: UInt64(s.st_dev))
297297
result[.systemFileNumber] = NSNumber(value: UInt64(s.st_ino))
298298

299-
let pwd = getpwuid(s.st_uid)
300-
if pwd != nil && pwd!.pointee.pw_name != nil {
301-
let name = String(cString: pwd!.pointee.pw_name)
299+
if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
300+
let name = String(cString: pwd.pointee.pw_name)
302301
result[.ownerAccountName] = name
303302
}
304303

305-
let grd = getgrgid(s.st_gid)
306-
if grd != nil && grd!.pointee.gr_name != nil {
307-
let name = String(cString: grd!.pointee.gr_name)
304+
if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
305+
let name = String(cString: grd.pointee.gr_name)
308306
result[.groupOwnerAccountName] = name
309307
}
310308

Foundation/NSCache.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ open class NSCache<KeyType : AnyObject, ObjectType : AnyObject> : NSObject {
119119
return
120120
}
121121

122-
while currentElement.nextByCost != nil && currentElement.nextByCost!.cost < entry.cost {
123-
currentElement = currentElement.nextByCost!
122+
while let nextByCost = currentElement.nextByCost, nextByCost.cost < entry.cost {
123+
currentElement = nextByCost
124124
}
125125

126126
// Insert entry between currentElement and nextElement

Foundation/NSPathUtilities.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ public extension NSString {
475475
break loop
476476
}
477477

478-
if char != nil {
479-
let lhs = caseSensitive ? char : String(char!).lowercased().first!
478+
if let char = char {
479+
let lhs = caseSensitive ? char : String(char).lowercased().first!
480480
let rhs = caseSensitive ? c : String(c).lowercased().first!
481481
if lhs != rhs {
482482
break loop

Foundation/NSString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ extension NSString {
699699
return
700700
}
701701
/* Find the starting point first */
702-
if startPtr != nil {
702+
if let startPtr = startPtr {
703703
var start: Int = 0
704704
if range.location == 0 {
705705
start = 0
@@ -723,7 +723,7 @@ extension NSString {
723723
buf.rewind()
724724
}
725725
}
726-
startPtr!.pointee = start
726+
startPtr.pointee = start
727727
}
728728
}
729729

Foundation/NSStringAPI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,8 +1471,8 @@ extension StringProtocol where Index == String.Index {
14711471
tokenRanges: $0) as NSArray
14721472
}
14731473

1474-
if nsTokenRanges != nil {
1475-
tokenRanges?.pointee = (nsTokenRanges! as [AnyObject]).map {
1474+
if let nsTokenRanges = nsTokenRanges {
1475+
tokenRanges?.pointee = (nsTokenRanges as [AnyObject]).map {
14761476
self._range($0.rangeValue)
14771477
}
14781478
}

Foundation/Process.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ open class Process: NSObject {
308308

309309
// If a termination handler has been set, invoke it on a background thread
310310

311-
if process.terminationHandler != nil {
311+
if let terminationHandler = process.terminationHandler {
312312
let thread = Thread {
313-
process.terminationHandler!(process)
313+
terminationHandler(process)
314314
}
315315
thread.start()
316316
}

Foundation/String.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ extension String : _ObjectTypeBridgeable {
2929
result = source._storage
3030
} else if type(of: source) == _NSCFString.self {
3131
let cf = unsafeBitCast(source, to: CFString.self)
32-
let str = CFStringGetCStringPtr(cf, CFStringEncoding(kCFStringEncodingUTF8))
33-
if str != nil {
34-
result = String(cString: str!)
32+
if let str = CFStringGetCStringPtr(cf, CFStringEncoding(kCFStringEncodingUTF8)) {
33+
result = String(cString: str)
3534
} else {
3635
let length = CFStringGetLength(cf)
3736
let buffer = UnsafeMutablePointer<UniChar>.allocate(capacity: length)

Foundation/XMLParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ internal func _NSXMLParserStartElementNs(_ ctx: _CFXMLInterface, localname: Unsa
291291
// idx+3 = value, i+4 = endvalue
292292
// By using XML_PARSE_NOENT the attribute value string will already have entities resolved
293293
var attributeValue = ""
294-
if attributes[idx + 3] != nil && attributes[idx + 4] != nil {
295-
let numBytesWithoutTerminator = attributes[idx + 4]! - attributes[idx + 3]!
294+
if let value = attributes[idx + 3], let endvalue = attributes[idx + 4] {
295+
let numBytesWithoutTerminator = endvalue - value
296296
if numBytesWithoutTerminator > 0 {
297-
let buffer = UnsafeBufferPointer(start: attributes[idx + 3]!,
297+
let buffer = UnsafeBufferPointer(start: value,
298298
count: numBytesWithoutTerminator)
299299
attributeValue = String._fromCodeUnitSequence(UTF8.self,
300300
input: buffer)!

TestFoundation/TestJSONSerialization.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,14 @@ extension TestJSONSerialization {
757757
XCTFail("Unable to convert string to data")
758758
return
759759
}
760-
let filePath = createTestFile("TestJSON.txt",_contents: data)
761-
if filePath != nil {
762-
let fileStream: InputStream = InputStream(fileAtPath: filePath!)!
760+
if let filePath = createTestFile("TestJSON.txt",_contents: data) {
761+
let fileStream: InputStream = InputStream(fileAtPath: filePath)!
763762
fileStream.open()
764763
let resultRead = try JSONSerialization.jsonObject(with: fileStream, options: [])
765764
let result = resultRead as? [String: Any]
766765
XCTAssertEqual(result?.count, 0)
767766
fileStream.close()
768-
removeTestFile(filePath!)
767+
removeTestFile(filePath)
769768
}
770769
} catch {
771770
XCTFail("Error thrown: \(error)")
@@ -780,14 +779,13 @@ extension TestJSONSerialization {
780779
XCTFail("Unable to convert string to data")
781780
return
782781
}
783-
let filePath = createTestFile("TestJSON.txt",_contents: data)
784-
if filePath != nil {
785-
let url = URL(fileURLWithPath: filePath!)
782+
if let filePath = createTestFile("TestJSON.txt",_contents: data) {
783+
let url = URL(fileURLWithPath: filePath)
786784
let inputStream: InputStream = InputStream(url: url)!
787785
inputStream.open()
788786
let result = try JSONSerialization.jsonObject(with: inputStream, options: []) as? [Any]
789787
inputStream.close()
790-
removeTestFile(filePath!)
788+
removeTestFile(filePath)
791789
XCTAssertEqual(result?[0] as? Bool, true)
792790
XCTAssertEqual(result?[1] as? Bool, false)
793791
XCTAssertEqual(result?[2] as? String, "hello")
@@ -1387,14 +1385,13 @@ extension TestJSONSerialization {
13871385
func test_jsonObjectToOutputStreamFile() {
13881386
let dict = ["a":["b":1]]
13891387
do {
1390-
let filePath = createTestFile("TestFileOut.txt",_contents: Data(capacity: 128))
1391-
if filePath != nil {
1392-
let outputStream = OutputStream(toFileAtPath: filePath!, append: true)
1388+
if let filePath = createTestFile("TestFileOut.txt",_contents: Data(capacity: 128)) {
1389+
let outputStream = OutputStream(toFileAtPath: filePath, append: true)
13931390
outputStream?.open()
13941391
let result = try JSONSerialization.writeJSONObject(dict, toStream: outputStream!, options: [])
13951392
outputStream?.close()
13961393
if(result > -1) {
1397-
let fileStream: InputStream = InputStream(fileAtPath: filePath!)!
1394+
let fileStream: InputStream = InputStream(fileAtPath: filePath)!
13981395
var buffer = [UInt8](repeating: 0, count: 20)
13991396
fileStream.open()
14001397
if fileStream.hasBytesAvailable {
@@ -1404,7 +1401,7 @@ extension TestJSONSerialization {
14041401
XCTAssertEqual(NSString(bytes: buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue), "{\"a\":{\"b\":1}}")
14051402
}
14061403
}
1407-
removeTestFile(filePath!)
1404+
removeTestFile(filePath)
14081405
} else {
14091406
XCTFail("Unable to create temp file")
14101407
}

TestFoundation/TestStream.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ class TestStream : XCTestCase {
5555
let message: NSString = "Hello, playground"
5656
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
5757
//Initialiser with url
58-
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
59-
if testFile != nil {
60-
let url = URL(fileURLWithPath: testFile!)
58+
if let testFile = createTestFile("testFile_in.txt", _contents: messageData) {
59+
let url = URL(fileURLWithPath: testFile)
6160
let urlStream: InputStream = InputStream(url: url)!
6261
XCTAssertEqual(Stream.Status.notOpen, urlStream.streamStatus)
6362
urlStream.open()
@@ -73,7 +72,7 @@ class TestStream : XCTestCase {
7372
XCTAssertEqual(message, output!)
7473
}
7574
}
76-
removeTestFile(testFile!)
75+
removeTestFile(testFile)
7776
} else {
7877
XCTFail("Unable to create temp file")
7978
}
@@ -83,9 +82,8 @@ class TestStream : XCTestCase {
8382
let message: NSString = "Hello, playground"
8483
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
8584
//Initialiser with file
86-
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
87-
if testFile != nil {
88-
let fileStream: InputStream = InputStream(fileAtPath: testFile!)!
85+
if let testFile = createTestFile("testFile_in.txt", _contents: messageData) {
86+
let fileStream: InputStream = InputStream(fileAtPath: testFile)!
8987
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
9088
fileStream.open()
9189
XCTAssertEqual(Stream.Status.open, fileStream.streamStatus)
@@ -100,7 +98,7 @@ class TestStream : XCTestCase {
10098
XCTAssertEqual(message, output!)
10199
}
102100
}
103-
removeTestFile(testFile!)
101+
removeTestFile(testFile)
104102
} else {
105103
XCTFail("Unable to create temp file")
106104
}
@@ -125,9 +123,8 @@ class TestStream : XCTestCase {
125123
}
126124

127125
func test_outputStreamCreationToFile() {
128-
let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
129-
if filePath != nil {
130-
let outputStream = OutputStream(toFileAtPath: filePath!, append: true)
126+
if let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)) {
127+
let outputStream = OutputStream(toFileAtPath: filePath, append: true)
131128
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
132129
var myString = "Hello world!"
133130
let encodedData = [UInt8](myString.utf8)
@@ -137,7 +134,7 @@ class TestStream : XCTestCase {
137134
outputStream?.close()
138135
XCTAssertEqual(myString.count, result)
139136
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
140-
removeTestFile(filePath!)
137+
removeTestFile(filePath)
141138
} else {
142139
XCTFail("Unable to create temp file");
143140
}
@@ -159,9 +156,8 @@ class TestStream : XCTestCase {
159156
}
160157

161158
func test_outputStreamCreationWithUrl() {
162-
let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
163-
if filePath != nil {
164-
let outputStream = OutputStream(url: URL(fileURLWithPath: filePath!), append: true)
159+
if let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)) {
160+
let outputStream = OutputStream(url: URL(fileURLWithPath: filePath), append: true)
165161
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
166162
var myString = "Hello world!"
167163
let encodedData = [UInt8](myString.utf8)
@@ -171,7 +167,7 @@ class TestStream : XCTestCase {
171167
outputStream?.close()
172168
XCTAssertEqual(myString.count, result)
173169
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
174-
removeTestFile(filePath!)
170+
removeTestFile(filePath)
175171
} else {
176172
XCTFail("Unable to create temp file");
177173
}

TestFoundation/XDGTestHelper.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ class XDGCheck {
2323
.path: "/",
2424
.domain: "example.com",
2525
]
26-
let simpleCookie = HTTPCookie(properties: properties)
27-
guard simpleCookie != nil else {
26+
guard let simpleCookie = HTTPCookie(properties: properties) else {
2827
exit(HelperCheckStatus.cookieStorageNil.rawValue)
2928
}
30-
let rawValue = getenv("XDG_DATA_HOME")
31-
guard rawValue != nil else {
29+
guard let rawValue = getenv("XDG_DATA_HOME") else {
3230
exit(HelperCheckStatus.fail.rawValue)
3331
}
34-
let xdg_data_home = String(utf8String: rawValue!)
35-
storage.setCookie(simpleCookie!)
32+
let xdg_data_home = String(utf8String: rawValue)
33+
storage.setCookie(simpleCookie)
3634
let fm = FileManager.default
3735
let destPath = xdg_data_home! + "/xdgTestHelper/.cookies.shared"
3836
var isDir: ObjCBool = false

0 commit comments

Comments
 (0)