Skip to content

[gardening] Remove redundant nil-initialization of optional variables #4852

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
Sep 19, 2016
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3663,7 +3663,7 @@ Swift 1.0
accepts inouts or `nil`:

```swift
var error: NSError? = nil
var error: NSError?
let words = NSString.stringWithContentsOfFile("/usr/share/dict/words",
encoding: .UTF8StringEncoding,
error: &error)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/utils/DriverUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ struct TestConfig {

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

/// The list of tests to run.
var tests = [Test]()
Expand Down
2 changes: 1 addition & 1 deletion docs/SequencesAndCollections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ implement a generic `for`\ …\ `in` loop.
latest = _baseIterator.next() ?? latest
return latest
}
public private(set) var latest: I.Element? = nil
public private(set) var latest: I.Element?
private var _baseIterator: I
}

Expand Down
2 changes: 1 addition & 1 deletion docs/proposals/CPointerInteropLanguageModel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ So if you have a function declared::

You can call it as any of::

var x: NSBas? = nil
var x: NSBas?
var p: AutoreleasingUnsafeMutablePointer<NSBas?> = nil
bas(nil)
bas(p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1937,8 +1937,8 @@ self.test("\(testNamePrefix)._preprocessingPass/semantics") {
for test in forEachTests {
let s = makeWrappedSequence(test.sequence.map(OpaqueValue.init))
var wasInvoked = false
var caughtError: Error? = nil
var result: OpaqueValue<Int>? = nil
var caughtError: Error?
var result: OpaqueValue<Int>?
do {
result = try s._preprocessingPass {
(sequence) -> OpaqueValue<Int> in
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnicodeUnittest/Collation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public struct StringComparisonTest {
public let collationElements: [UInt64]
public let loc: SourceLoc

public var order: Int? = nil
public var order: Int?

public init(
_ string: String,
Expand Down
14 changes: 7 additions & 7 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func _childProcess() {
}

struct _ParentProcess {
internal var _pid: pid_t? = nil
internal var _pid: pid_t?
internal var _childStdin: _FDOutputStream = _FDOutputStream(fd: -1)
internal var _childStdout: _FDInputStream = _FDInputStream(fd: -1)
internal var _childStderr: _FDInputStream = _FDInputStream(fd: -1)
Expand Down Expand Up @@ -849,7 +849,7 @@ struct _ParentProcess {

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

var expectCrash = false
var childTerminationStatus: ProcessTerminationStatus? = nil
var childTerminationStatus: ProcessTerminationStatus?
var crashStdout: [String] = []
var crashStderr: [String] = []
if _runTestsInProcess {
Expand Down Expand Up @@ -1145,7 +1145,7 @@ public func runAllTests() {
_childProcess()
} else {
var runTestsInProcess: Bool = false
var filter: String? = nil
var filter: String?
var args = [String]()
var i = 0
i += 1 // Skip the name of the executable.
Expand Down Expand Up @@ -1331,10 +1331,10 @@ public final class TestSuite {
internal final class _Data {
var _xfail: [TestRunPredicate] = []
var _skip: [TestRunPredicate] = []
var _stdinText: String? = nil
var _stdinText: String?
var _stdinEndsWithEOF: Bool = false
var _crashOutputMatches: [String] = []
var _testLoc: SourceLoc? = nil
var _testLoc: SourceLoc?
}

init(testSuite: TestSuite, name: String, loc: SourceLoc) {
Expand Down Expand Up @@ -1507,7 +1507,7 @@ func _getOSVersion() -> OSVersion {
}

var _runningOSVersion: OSVersion = _getOSVersion()
var _overrideOSVersion: OSVersion? = nil
var _overrideOSVersion: OSVersion?

/// Override the OS version for testing.
public func _setOverrideOSVersion(_ v: OSVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ObjectiveC
import Foundation

internal var _temporaryLocaleCurrentLocale: NSLocale? = nil
internal var _temporaryLocaleCurrentLocale: NSLocale?

extension NSLocale {
@objc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public var _stdlib_PTHREAD_BARRIER_SERIAL_THREAD: CInt {
}

public struct _stdlib_pthread_barrier_t {
var mutex: UnsafeMutablePointer<pthread_mutex_t>? = nil
var cond: UnsafeMutablePointer<pthread_cond_t>? = nil
var mutex: UnsafeMutablePointer<pthread_mutex_t>?
var cond: UnsafeMutablePointer<pthread_cond_t>?

/// The number of threads to synchronize.
var count: CUnsignedInt = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public func _stdlib_pthread_join<Result>(
_ thread: pthread_t,
_ resultType: Result.Type
) -> (CInt, Result?) {
var threadResultRawPtr: UnsafeMutableRawPointer? = nil
var threadResultRawPtr: UnsafeMutableRawPointer?
let result = pthread_join(thread, &threadResultRawPtr)
if result == 0 {
let threadResultPtr = threadResultRawPtr!.assumingMemoryBound(
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/CryptoTokenKit/CryptoTokenKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension TKSmartCard {

@available(OSX 10.12, *)
public func withSession<T>(_ body: @escaping () throws -> T) throws -> T {
var result: T? = nil
var result: T?
try self.__inSession(executeBlock: {
(errorPointer: NSErrorPointer) -> Bool in
do {
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/SDK/Dispatch/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
public func withUnsafeBytes<Result, ContentType>(
body: (UnsafePointer<ContentType>) throws -> Result) rethrows -> Result
{
var ptr: UnsafeRawPointer? = nil
var ptr: UnsafeRawPointer?
var size = 0
let data = __dispatch_data_create_map(__wrapped, &ptr, &size)
let contentPtr = ptr!.bindMemory(
Expand Down Expand Up @@ -183,7 +183,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
var offset = 0
let subdata = __dispatch_data_copy_region(__wrapped, index, &offset)

var ptr: UnsafeRawPointer? = nil
var ptr: UnsafeRawPointer?
var size = 0
let map = __dispatch_data_create_map(subdata, &ptr, &size)
defer { _fixLifetime(map) }
Expand Down Expand Up @@ -280,7 +280,7 @@ extension DispatchData {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: __DispatchData?) -> DispatchData {
var result: DispatchData? = nil
var result: DispatchData?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Dispatch/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public extension DispatchQueue {
public static let initiallyInactive = Attributes(rawValue: 1<<2)

fileprivate func _attr() -> __OS_dispatch_queue_attr? {
var attr: __OS_dispatch_queue_attr? = nil
var attr: __OS_dispatch_queue_attr?

if self.contains(.concurrent) {
attr = _swift_dispatch_queue_concurrent()
Expand Down
12 changes: 6 additions & 6 deletions stdlib/public/SDK/Foundation/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
/// - parameter date: The specified date.
/// - returns: `true` if the starting time and duration of a component could be calculated, otherwise `false`.
public func dateInterval(of component: Component, start: inout Date, interval: inout TimeInterval, for date: Date) -> Bool {
var nsDate : NSDate? = nil
var nsDate : NSDate?
var ti : TimeInterval = 0
if _handle.map({ $0.range(of: Calendar._toCalendarUnit([component]), start: &nsDate, interval: &ti, for: date) }) {
start = nsDate as! Date
Expand Down Expand Up @@ -688,7 +688,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
/// - returns: `true` if a date range could be found, and `false` if the date is not in a weekend.
@available(iOS 8.0, *)
public func dateIntervalOfWeekend(containing date: Date, start: inout Date, interval: inout TimeInterval) -> Bool {
var nsDate : NSDate? = nil
var nsDate : NSDate?
var ti : TimeInterval = 0
if _handle.map({ $0.range(ofWeekendStart: &nsDate, interval: &ti, containing: date) }) {
start = nsDate as! Date
Expand All @@ -705,7 +705,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
/// - returns: A `DateInterval`, or nil if the date is not in a weekend.
@available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
public func dateIntervalOfWeekend(containing date: Date) -> DateInterval? {
var nsDate : NSDate? = nil
var nsDate : NSDate?
var ti : TimeInterval = 0
if _handle.map({ $0.range(ofWeekendStart: &nsDate, interval: &ti, containing: date) }) {
return DateInterval(start: nsDate as! Date, duration: ti)
Expand All @@ -729,7 +729,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
@available(iOS 8.0, *)
public func nextWeekend(startingAfter date: Date, start: inout Date, interval: inout TimeInterval, direction: SearchDirection = .forward) -> Bool {
// The implementation actually overrides previousKeepSmaller and nextKeepSmaller with matchNext, always - but strict still trumps all.
var nsDate : NSDate? = nil
var nsDate : NSDate?
var ti : TimeInterval = 0
if _handle.map({ $0.nextWeekendStart(&nsDate, interval: &ti, options: direction == .backward ? [.searchBackwards] : [], after: date) }) {
start = nsDate as! Date
Expand All @@ -751,7 +751,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
@available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
public func nextWeekend(startingAfter date: Date, direction: SearchDirection = .forward) -> DateInterval? {
// The implementation actually overrides previousKeepSmaller and nextKeepSmaller with matchNext, always - but strict still trumps all.
var nsDate : NSDate? = nil
var nsDate : NSDate?
var ti : TimeInterval = 0
if _handle.map({ $0.nextWeekendStart(&nsDate, interval: &ti, options: direction == .backward ? [.searchBackwards] : [], after: date) }) {
/// WARNING: searching backwards is totally broken! 26643365
Expand Down Expand Up @@ -1122,7 +1122,7 @@ extension Calendar : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSCalendar?) -> Calendar {
var result: Calendar? = nil
var result: Calendar?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ extension Data : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSData?) -> Data {
var result: Data? = nil
var result: Data?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ extension Date : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDate?) -> Date {
var result: Date? = nil
var result: Date?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/DateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ extension DateComponents : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDateComponents?) -> DateComponents {
var result: DateComponents? = nil
var result: DateComponents?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/DateInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ extension DateInterval : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDateInterval?) -> DateInterval {
var result: DateInterval? = nil
var result: DateInterval?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ extension Decimal : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSDecimalNumber?) -> Decimal {
var result: Decimal? = nil
var result: Decimal?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension FileManager {

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

@available(OSX 10.6, iOS 4.0, *)
public func replaceItemAt(_ originalItemURL: URL, withItemAt newItemURL: URL, backupItemName: String? = nil, options: FileManager.ItemReplacementOptions = []) throws -> NSURL? {
var error: NSError? = nil
var error: NSError?
if let result = NS_Swift_NSFileManager_replaceItemAtURL_withItemAtURL_backupItemName_options(self, originalItemURL as NSURL, newItemURL as NSURL, backupItemName, options, &error) {
return result
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/SDK/Foundation/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ extension NSCoder {

@nonobjc
public func decodeObject(of classes: [AnyClass]?, forKey key: String) -> Any? {
var classesAsNSObjects: NSSet? = nil
var classesAsNSObjects: NSSet?
if let theClasses = classes {
classesAsNSObjects = NSSet(array: theClasses.map { $0 as AnyObject })
}
Expand Down Expand Up @@ -1474,8 +1474,8 @@ extension NSCoder {
@nonobjc
@available(OSX 10.11, iOS 9.0, *)
public func decodeTopLevelObject(of classes: [AnyClass]?, forKey key: String) throws -> Any? {
var error: NSError? = nil
var classesAsNSObjects: NSSet? = nil
var error: NSError?
var classesAsNSObjects: NSSet?
if let theClasses = classes {
classesAsNSObjects = NSSet(array: theClasses.map { $0 as AnyObject })
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/IndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
public func filteredIndexSet(in range : Range<Element>, includeInteger: (Element) throws -> Bool) rethrows -> IndexSet {
let r : NSRange = _toNSRange(range)
return try _handle.map {
var error : Error? = nil
var error : Error?
let result = $0.indexes(in: r, options: [], passingTest: { (i, stop) -> Bool in
do {
let include = try includeInteger(i)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Locale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ extension Locale : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSLocale?) -> Locale {
var result: Locale? = nil
var result: Locale?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Notification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ extension Notification : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSNotification?) -> Notification {
var result: Notification? = nil
var result: Notification?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/PersonNameComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension PersonNameComponents : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSPersonNameComponents?) -> PersonNameComponents {
var result: PersonNameComponents? = nil
var result: PersonNameComponents?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/TimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ extension TimeZone : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSTimeZone?) -> TimeZone {
var result: TimeZone? = nil
var result: TimeZone?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/SDK/Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ public struct URL : ReferenceConvertible, Equatable {
///
/// 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.
public func checkResourceIsReachable() throws -> Bool {
var error : NSError? = nil
var error : NSError?
let result = _url.checkResourceIsReachableAndReturnError(&error)
if let e = error {
throw e
Expand All @@ -979,7 +979,7 @@ public struct URL : ReferenceConvertible, Equatable {
/// 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.
@available(OSX 10.10, iOS 8.0, *)
public func checkPromisedItemIsReachable() throws -> Bool {
var error : NSError? = nil
var error : NSError?
let result = _url.checkPromisedItemIsReachableAndReturnError(&error)
if let e = error {
throw e
Expand Down Expand Up @@ -1151,7 +1151,7 @@ extension URL : _ObjectiveCBridgeable {
}

public static func _unconditionallyBridgeFromObjectiveC(_ source: NSURL?) -> URL {
var result: URL? = nil
var result: URL?
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
Expand Down
Loading