Skip to content

Commit fa7fbdb

Browse files
[gardening] Remove redundant nil-initialization of optional variable
From the Swift documentation: "If you define an optional variable without providing a default value, the variable is automatically set to nil for you."
1 parent 41c09f0 commit fa7fbdb

File tree

89 files changed

+137
-137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+137
-137
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,7 @@ Swift 1.0
36633663
accepts inouts or `nil`:
36643664

36653665
```swift
3666-
var error: NSError? = nil
3666+
var error: NSError?
36673667
let words = NSString.stringWithContentsOfFile("/usr/share/dict/words",
36683668
encoding: .UTF8StringEncoding,
36693669
error: &error)

benchmark/utils/DriverUtils.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct TestConfig {
9696

9797
/// After we run the tests, should the harness sleep to allow for utilities
9898
/// like leaks that require a PID to run on the test harness.
99-
var afterRunSleep: Int? = nil
99+
var afterRunSleep: Int?
100100

101101
/// The list of tests to run.
102102
var tests = [Test]()

docs/SequencesAndCollections.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ implement a generic `for`\ …\ `in` loop.
142142
latest = _baseIterator.next() ?? latest
143143
return latest
144144
}
145-
public private(set) var latest: I.Element? = nil
145+
public private(set) var latest: I.Element?
146146
private var _baseIterator: I
147147
}
148148

docs/proposals/CPointerInteropLanguageModel.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ So if you have a function declared::
116116

117117
You can call it as any of::
118118

119-
var x: NSBas? = nil
119+
var x: NSBas?
120120
var p: AutoreleasingUnsafeMutablePointer<NSBas?> = nil
121121
bas(nil)
122122
bas(p)

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1937,8 +1937,8 @@ self.test("\(testNamePrefix)._preprocessingPass/semantics") {
19371937
for test in forEachTests {
19381938
let s = makeWrappedSequence(test.sequence.map(OpaqueValue.init))
19391939
var wasInvoked = false
1940-
var caughtError: Error? = nil
1941-
var result: OpaqueValue<Int>? = nil
1940+
var caughtError: Error?
1941+
var result: OpaqueValue<Int>?
19421942
do {
19431943
result = try s._preprocessingPass {
19441944
(sequence) -> OpaqueValue<Int> in

stdlib/private/StdlibUnicodeUnittest/Collation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public struct StringComparisonTest {
247247
public let collationElements: [UInt64]
248248
public let loc: SourceLoc
249249

250-
public var order: Int? = nil
250+
public var order: Int?
251251

252252
public init(
253253
_ string: String,

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

+7-7
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ func _childProcess() {
681681
}
682682

683683
struct _ParentProcess {
684-
internal var _pid: pid_t? = nil
684+
internal var _pid: pid_t?
685685
internal var _childStdin: _FDOutputStream = _FDOutputStream(fd: -1)
686686
internal var _childStdout: _FDInputStream = _FDInputStream(fd: -1)
687687
internal var _childStderr: _FDInputStream = _FDInputStream(fd: -1)
@@ -849,7 +849,7 @@ struct _ParentProcess {
849849

850850
// Check if the child has sent us "end" markers for the current test.
851851
if stdoutEnd && stderrEnd {
852-
var status: ProcessTerminationStatus? = nil
852+
var status: ProcessTerminationStatus?
853853
if !testSuite._testByName(testName).canReuseChildProcessAfterTest {
854854
status = _waitForChild()
855855
switch status! {
@@ -937,7 +937,7 @@ struct _ParentProcess {
937937
print("[ RUN ] \(fullTestName)\(activeXFailsText)")
938938

939939
var expectCrash = false
940-
var childTerminationStatus: ProcessTerminationStatus? = nil
940+
var childTerminationStatus: ProcessTerminationStatus?
941941
var crashStdout: [String] = []
942942
var crashStderr: [String] = []
943943
if _runTestsInProcess {
@@ -1145,7 +1145,7 @@ public func runAllTests() {
11451145
_childProcess()
11461146
} else {
11471147
var runTestsInProcess: Bool = false
1148-
var filter: String? = nil
1148+
var filter: String?
11491149
var args = [String]()
11501150
var i = 0
11511151
i += 1 // Skip the name of the executable.
@@ -1331,10 +1331,10 @@ public final class TestSuite {
13311331
internal final class _Data {
13321332
var _xfail: [TestRunPredicate] = []
13331333
var _skip: [TestRunPredicate] = []
1334-
var _stdinText: String? = nil
1334+
var _stdinText: String?
13351335
var _stdinEndsWithEOF: Bool = false
13361336
var _crashOutputMatches: [String] = []
1337-
var _testLoc: SourceLoc? = nil
1337+
var _testLoc: SourceLoc?
13381338
}
13391339

13401340
init(testSuite: TestSuite, name: String, loc: SourceLoc) {
@@ -1507,7 +1507,7 @@ func _getOSVersion() -> OSVersion {
15071507
}
15081508

15091509
var _runningOSVersion: OSVersion = _getOSVersion()
1510-
var _overrideOSVersion: OSVersion? = nil
1510+
var _overrideOSVersion: OSVersion?
15111511

15121512
/// Override the OS version for testing.
15131513
public func _setOverrideOSVersion(_ v: OSVersion) {

stdlib/private/StdlibUnittestFoundationExtras/StdlibUnittestFoundationExtras.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import ObjectiveC
1414
import Foundation
1515

16-
internal var _temporaryLocaleCurrentLocale: NSLocale? = nil
16+
internal var _temporaryLocaleCurrentLocale: NSLocale?
1717

1818
extension NSLocale {
1919
@objc

stdlib/private/SwiftPrivatePthreadExtras/PthreadBarriers.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public var _stdlib_PTHREAD_BARRIER_SERIAL_THREAD: CInt {
4343
}
4444

4545
public struct _stdlib_pthread_barrier_t {
46-
var mutex: UnsafeMutablePointer<pthread_mutex_t>? = nil
47-
var cond: UnsafeMutablePointer<pthread_cond_t>? = nil
46+
var mutex: UnsafeMutablePointer<pthread_mutex_t>?
47+
var cond: UnsafeMutablePointer<pthread_cond_t>?
4848

4949
/// The number of threads to synchronize.
5050
var count: CUnsignedInt = 0

stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public func _stdlib_pthread_join<Result>(
9595
_ thread: pthread_t,
9696
_ resultType: Result.Type
9797
) -> (CInt, Result?) {
98-
var threadResultRawPtr: UnsafeMutableRawPointer? = nil
98+
var threadResultRawPtr: UnsafeMutableRawPointer?
9999
let result = pthread_join(thread, &threadResultRawPtr)
100100
if result == 0 {
101101
let threadResultPtr = threadResultRawPtr!.assumingMemoryBound(

stdlib/public/SDK/CryptoTokenKit/CryptoTokenKit.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extension TKSmartCard {
3535

3636
@available(OSX 10.12, *)
3737
public func withSession<T>(_ body: @escaping () throws -> T) throws -> T {
38-
var result: T? = nil
38+
var result: T?
3939
try self.__inSession(executeBlock: {
4040
(errorPointer: NSErrorPointer) -> Bool in
4141
do {

stdlib/public/SDK/Dispatch/Data.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
7272
public func withUnsafeBytes<Result, ContentType>(
7373
body: (UnsafePointer<ContentType>) throws -> Result) rethrows -> Result
7474
{
75-
var ptr: UnsafeRawPointer? = nil
75+
var ptr: UnsafeRawPointer?
7676
var size = 0
7777
let data = __dispatch_data_create_map(__wrapped, &ptr, &size)
7878
let contentPtr = ptr!.bindMemory(
@@ -183,7 +183,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
183183
var offset = 0
184184
let subdata = __dispatch_data_copy_region(__wrapped, index, &offset)
185185

186-
var ptr: UnsafeRawPointer? = nil
186+
var ptr: UnsafeRawPointer?
187187
var size = 0
188188
let map = __dispatch_data_create_map(subdata, &ptr, &size)
189189
defer { _fixLifetime(map) }
@@ -280,7 +280,7 @@ extension DispatchData {
280280
}
281281

282282
public static func _unconditionallyBridgeFromObjectiveC(_ source: __DispatchData?) -> DispatchData {
283-
var result: DispatchData? = nil
283+
var result: DispatchData?
284284
_forceBridgeFromObjectiveC(source!, result: &result)
285285
return result!
286286
}

stdlib/public/SDK/Dispatch/Queue.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public extension DispatchQueue {
3434
public static let initiallyInactive = Attributes(rawValue: 1<<2)
3535

3636
fileprivate func _attr() -> __OS_dispatch_queue_attr? {
37-
var attr: __OS_dispatch_queue_attr? = nil
37+
var attr: __OS_dispatch_queue_attr?
3838

3939
if self.contains(.concurrent) {
4040
attr = _swift_dispatch_queue_concurrent()

stdlib/public/SDK/Foundation/Calendar.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
410410
/// - parameter date: The specified date.
411411
/// - returns: `true` if the starting time and duration of a component could be calculated, otherwise `false`.
412412
public func dateInterval(of component: Component, start: inout Date, interval: inout TimeInterval, for date: Date) -> Bool {
413-
var nsDate : NSDate? = nil
413+
var nsDate : NSDate?
414414
var ti : TimeInterval = 0
415415
if _handle.map({ $0.range(of: Calendar._toCalendarUnit([component]), start: &nsDate, interval: &ti, for: date) }) {
416416
start = nsDate as! Date
@@ -688,7 +688,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
688688
/// - returns: `true` if a date range could be found, and `false` if the date is not in a weekend.
689689
@available(iOS 8.0, *)
690690
public func dateIntervalOfWeekend(containing date: Date, start: inout Date, interval: inout TimeInterval) -> Bool {
691-
var nsDate : NSDate? = nil
691+
var nsDate : NSDate?
692692
var ti : TimeInterval = 0
693693
if _handle.map({ $0.range(ofWeekendStart: &nsDate, interval: &ti, containing: date) }) {
694694
start = nsDate as! Date
@@ -705,7 +705,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
705705
/// - returns: A `DateInterval`, or nil if the date is not in a weekend.
706706
@available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
707707
public func dateIntervalOfWeekend(containing date: Date) -> DateInterval? {
708-
var nsDate : NSDate? = nil
708+
var nsDate : NSDate?
709709
var ti : TimeInterval = 0
710710
if _handle.map({ $0.range(ofWeekendStart: &nsDate, interval: &ti, containing: date) }) {
711711
return DateInterval(start: nsDate as! Date, duration: ti)
@@ -729,7 +729,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
729729
@available(iOS 8.0, *)
730730
public func nextWeekend(startingAfter date: Date, start: inout Date, interval: inout TimeInterval, direction: SearchDirection = .forward) -> Bool {
731731
// The implementation actually overrides previousKeepSmaller and nextKeepSmaller with matchNext, always - but strict still trumps all.
732-
var nsDate : NSDate? = nil
732+
var nsDate : NSDate?
733733
var ti : TimeInterval = 0
734734
if _handle.map({ $0.nextWeekendStart(&nsDate, interval: &ti, options: direction == .backward ? [.searchBackwards] : [], after: date) }) {
735735
start = nsDate as! Date
@@ -751,7 +751,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
751751
@available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
752752
public func nextWeekend(startingAfter date: Date, direction: SearchDirection = .forward) -> DateInterval? {
753753
// The implementation actually overrides previousKeepSmaller and nextKeepSmaller with matchNext, always - but strict still trumps all.
754-
var nsDate : NSDate? = nil
754+
var nsDate : NSDate?
755755
var ti : TimeInterval = 0
756756
if _handle.map({ $0.nextWeekendStart(&nsDate, interval: &ti, options: direction == .backward ? [.searchBackwards] : [], after: date) }) {
757757
/// WARNING: searching backwards is totally broken! 26643365
@@ -1122,7 +1122,7 @@ extension Calendar : _ObjectiveCBridgeable {
11221122
}
11231123

11241124
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSCalendar?) -> Calendar {
1125-
var result: Calendar? = nil
1125+
var result: Calendar?
11261126
_forceBridgeFromObjectiveC(source!, result: &result)
11271127
return result!
11281128
}

stdlib/public/SDK/Foundation/Data.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ extension Data : _ObjectiveCBridgeable {
760760
}
761761

762762
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSData?) -> Data {
763-
var result: Data? = nil
763+
var result: Data?
764764
_forceBridgeFromObjectiveC(source!, result: &result)
765765
return result!
766766
}

stdlib/public/SDK/Foundation/Date.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ extension Date : _ObjectiveCBridgeable {
256256
}
257257

258258
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDate?) -> Date {
259-
var result: Date? = nil
259+
var result: Date?
260260
_forceBridgeFromObjectiveC(source!, result: &result)
261261
return result!
262262
}

stdlib/public/SDK/Foundation/DateComponents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ extension DateComponents : _ObjectiveCBridgeable {
339339
}
340340

341341
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDateComponents?) -> DateComponents {
342-
var result: DateComponents? = nil
342+
var result: DateComponents?
343343
_forceBridgeFromObjectiveC(source!, result: &result)
344344
return result!
345345
}

stdlib/public/SDK/Foundation/DateInterval.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ extension DateInterval : _ObjectiveCBridgeable {
214214
}
215215

216216
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDateInterval?) -> DateInterval {
217-
var result: DateInterval? = nil
217+
var result: DateInterval?
218218
_forceBridgeFromObjectiveC(source!, result: &result)
219219
return result!
220220
}

stdlib/public/SDK/Foundation/Decimal.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ extension Decimal : _ObjectiveCBridgeable {
454454
}
455455

456456
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDecimalNumber?) -> Decimal {
457-
var result: Decimal? = nil
457+
var result: Decimal?
458458
_forceBridgeFromObjectiveC(source!, result: &result)
459459
return result!
460460
}

stdlib/public/SDK/Foundation/FileManager.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension FileManager {
3030

3131
@available(*, deprecated, renamed:"replaceItemAt(_:withItemAt:backupItemName:options:)")
3232
public func replaceItemAtURL(originalItemURL: NSURL, withItemAtURL newItemURL: NSURL, backupItemName: String? = nil, options: FileManager.ItemReplacementOptions = []) throws -> NSURL? {
33-
var error: NSError? = nil
33+
var error: NSError?
3434
if let result = NS_Swift_NSFileManager_replaceItemAtURL_withItemAtURL_backupItemName_options(self, originalItemURL, newItemURL, backupItemName, options, &error) {
3535
return result
3636
}
@@ -39,7 +39,7 @@ extension FileManager {
3939

4040
@available(OSX 10.6, iOS 4.0, *)
4141
public func replaceItemAt(_ originalItemURL: URL, withItemAt newItemURL: URL, backupItemName: String? = nil, options: FileManager.ItemReplacementOptions = []) throws -> NSURL? {
42-
var error: NSError? = nil
42+
var error: NSError?
4343
if let result = NS_Swift_NSFileManager_replaceItemAtURL_withItemAtURL_backupItemName_options(self, originalItemURL as NSURL, newItemURL as NSURL, backupItemName, options, &error) {
4444
return result
4545
}

stdlib/public/SDK/Foundation/Foundation.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ extension NSCoder {
14161416

14171417
@nonobjc
14181418
public func decodeObject(of classes: [AnyClass]?, forKey key: String) -> Any? {
1419-
var classesAsNSObjects: NSSet? = nil
1419+
var classesAsNSObjects: NSSet?
14201420
if let theClasses = classes {
14211421
classesAsNSObjects = NSSet(array: theClasses.map { $0 as AnyObject })
14221422
}
@@ -1474,8 +1474,8 @@ extension NSCoder {
14741474
@nonobjc
14751475
@available(OSX 10.11, iOS 9.0, *)
14761476
public func decodeTopLevelObject(of classes: [AnyClass]?, forKey key: String) throws -> Any? {
1477-
var error: NSError? = nil
1478-
var classesAsNSObjects: NSSet? = nil
1477+
var error: NSError?
1478+
var classesAsNSObjects: NSSet?
14791479
if let theClasses = classes {
14801480
classesAsNSObjects = NSSet(array: theClasses.map { $0 as AnyObject })
14811481
}

stdlib/public/SDK/Foundation/IndexSet.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
683683
public func filteredIndexSet(in range : Range<Element>, includeInteger: (Element) throws -> Bool) rethrows -> IndexSet {
684684
let r : NSRange = _toNSRange(range)
685685
return try _handle.map {
686-
var error : Error? = nil
686+
var error : Error?
687687
let result = $0.indexes(in: r, options: [], passingTest: { (i, stop) -> Bool in
688688
do {
689689
let include = try includeInteger(i)

stdlib/public/SDK/Foundation/Locale.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ extension Locale : _ObjectiveCBridgeable {
476476
}
477477

478478
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSLocale?) -> Locale {
479-
var result: Locale? = nil
479+
var result: Locale?
480480
_forceBridgeFromObjectiveC(source!, result: &result)
481481
return result!
482482
}

stdlib/public/SDK/Foundation/Notification.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extension Notification : _ObjectiveCBridgeable {
121121
}
122122

123123
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNotification?) -> Notification {
124-
var result: Notification? = nil
124+
var result: Notification?
125125
_forceBridgeFromObjectiveC(source!, result: &result)
126126
return result!
127127
}

stdlib/public/SDK/Foundation/PersonNameComponents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ extension PersonNameComponents : _ObjectiveCBridgeable {
130130
}
131131

132132
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSPersonNameComponents?) -> PersonNameComponents {
133-
var result: PersonNameComponents? = nil
133+
var result: PersonNameComponents?
134134
_forceBridgeFromObjectiveC(source!, result: &result)
135135
return result!
136136
}

stdlib/public/SDK/Foundation/TimeZone.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ extension TimeZone : _ObjectiveCBridgeable {
270270
}
271271

272272
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSTimeZone?) -> TimeZone {
273-
var result: TimeZone? = nil
273+
var result: TimeZone?
274274
_forceBridgeFromObjectiveC(source!, result: &result)
275275
return result!
276276
}

stdlib/public/SDK/Foundation/URL.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ public struct URL : ReferenceConvertible, Equatable {
965965
///
966966
/// This method synchronously checks if the resource's backing store is reachable. Checking reachability is appropriate when making decisions that do not require other immediate operations on the resource, e.g. periodic maintenance of UI state that depends on the existence of a specific document. When performing operations such as opening a file or copying resource properties, it is more efficient to simply try the operation and handle failures. This method is currently applicable only to URLs for file system resources. For other URL types, `false` is returned.
967967
public func checkResourceIsReachable() throws -> Bool {
968-
var error : NSError? = nil
968+
var error : NSError?
969969
let result = _url.checkResourceIsReachableAndReturnError(&error)
970970
if let e = error {
971971
throw e
@@ -979,7 +979,7 @@ public struct URL : ReferenceConvertible, Equatable {
979979
/// This method synchronously checks if the resource's backing store is reachable. Checking reachability is appropriate when making decisions that do not require other immediate operations on the resource, e.g. periodic maintenance of UI state that depends on the existence of a specific document. When performing operations such as opening a file or copying resource properties, it is more efficient to simply try the operation and handle failures. This method is currently applicable only to URLs for file system resources. For other URL types, `false` is returned.
980980
@available(OSX 10.10, iOS 8.0, *)
981981
public func checkPromisedItemIsReachable() throws -> Bool {
982-
var error : NSError? = nil
982+
var error : NSError?
983983
let result = _url.checkPromisedItemIsReachableAndReturnError(&error)
984984
if let e = error {
985985
throw e
@@ -1151,7 +1151,7 @@ extension URL : _ObjectiveCBridgeable {
11511151
}
11521152

11531153
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSURL?) -> URL {
1154-
var result: URL? = nil
1154+
var result: URL?
11551155
_forceBridgeFromObjectiveC(source!, result: &result)
11561156
return result!
11571157
}

0 commit comments

Comments
 (0)