Skip to content

Commit fed31bc

Browse files
authored
Merge pull request swiftlang#1358 from ikesyo/optional-binding
2 parents a3898c1 + a4fb766 commit fed31bc

12 files changed

+108
-116
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
}

0 commit comments

Comments
 (0)