Merge pull request #878 from naithar/guard-in-equal

diff --git a/Foundation/Bridging.swift b/Foundation/Bridging.swift
index 4a6db52..b1c9525 100644
--- a/Foundation/Bridging.swift
+++ b/Foundation/Bridging.swift
@@ -121,20 +121,18 @@
     }
     
     override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? _SwiftValue {
-            if self === other {
-                return true
-            }
-            if let otherHashable = other.value as? AnyHashable,
-               let hashable = self.value as? AnyHashable {
-                return otherHashable == hashable
-            }
+        switch value {
+        case let other as _SwiftValue:
+            guard let left = other.value as? AnyHashable,
+                let right = self.value as? AnyHashable else { return self === other }
             
-        } else if let otherHashable = value as? AnyHashable,
-                  let hashable = self.value as? AnyHashable {
-            return otherHashable == hashable
+            return left == right
+        case let other as AnyHashable:
+            guard let hashable = self.value as? AnyHashable else { return false }
+            return other == hashable
+        default:
+            return false
         }
-        return false
     }
     
     public func copy(with zone: NSZone?) -> Any {
diff --git a/Foundation/NSAffineTransform.swift b/Foundation/NSAffineTransform.swift
index 58b12c4..0b3fc61 100644
--- a/Foundation/NSAffineTransform.swift
+++ b/Foundation/NSAffineTransform.swift
@@ -334,12 +334,9 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let other = object as? NSAffineTransform {
-            return other === self
-                || (other.transformStruct == self.transformStruct)
-        }
-        
-        return false
+        guard let other = object as? NSAffineTransform else { return false }
+        return other === self
+            || (other.transformStruct == self.transformStruct)
     }
     
     public static var supportsSecureCoding: Bool {
diff --git a/Foundation/NSArray.swift b/Foundation/NSArray.swift
index ca1a5cb..c0a81cc 100644
--- a/Foundation/NSArray.swift
+++ b/Foundation/NSArray.swift
@@ -137,12 +137,14 @@
     }
 
     open override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? [Any] {
+        switch value {
+        case let other as [Any]:
             return self.isEqual(to: other)
-        } else if let other = value as? NSArray {
+        case let other as NSArray:
             return self.isEqual(to: other.allObjects)
+        default:
+            return false
         }
-        return false
     }
 
     open override var hash: Int {
diff --git a/Foundation/NSCalendar.swift b/Foundation/NSCalendar.swift
index b21dd17..e3b427f 100644
--- a/Foundation/NSCalendar.swift
+++ b/Foundation/NSCalendar.swift
@@ -225,15 +225,14 @@
     }
     
     open override func isEqual(_ value: Any?) -> Bool {
-        if let cal = value as? Calendar {
-            return CFEqual(_cfObject, cal._cfObject)
-        } else if let cal = value as? NSCalendar {
-            if cal === self {
-                return true
-            }
-            return CFEqual(_cfObject, cal._cfObject)
+        switch value {
+        case let other as Calendar:
+            return CFEqual(_cfObject, other._cfObject)
+        case let other as NSCalendar:
+            return other === self || CFEqual(_cfObject, other._cfObject)
+        default:
+            return false
         }
-        return false
     }
     
     open override var description: String {
@@ -1314,61 +1313,26 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let other = object as? NSDateComponents {
-            if era != other.era {
-                return false
-            }
-            if year != other.year {
-                return false
-            }
-            if quarter != other.quarter {
-                return false
-            }
-            if month != other.month {
-                return false
-            }
-            if day != other.day {
-                return false
-            }
-            if hour != other.hour {
-                return false
-            }
-            if minute != other.minute {
-                return false
-            }
-            if second != other.second {
-                return false
-            }
-            if nanosecond != other.nanosecond {
-                return false
-            }
-            if weekOfYear != other.weekOfYear {
-                return false
-            }
-            if weekOfMonth != other.weekOfMonth {
-                return false
-            }
-            if yearForWeekOfYear != other.yearForWeekOfYear {
-                return false
-            }
-            if weekday != other.weekday {
-                return false
-            }
-            if weekdayOrdinal != other.weekdayOrdinal {
-                return false
-            }
-            if isLeapMonth != other.isLeapMonth {
-                return false
-            }
-            if calendar != other.calendar {
-                return false
-            }
-            if timeZone != other.timeZone {
-                return false
-            }
-            return true
-        }
-        return false
+        guard let other = object as? NSDateComponents else { return false }
+        
+        return self === other
+            || (era == other.era
+                && year == other.year
+                && quarter == other.quarter
+                && month == other.month
+                && day == other.day
+                && hour == other.hour
+                && minute == other.minute
+                && second == other.second
+                && nanosecond == other.nanosecond
+                && weekOfYear == other.weekOfYear
+                && weekOfMonth == other.weekOfMonth
+                && yearForWeekOfYear == other.yearForWeekOfYear
+                && weekday == other.weekday
+                && weekdayOrdinal == other.weekdayOrdinal
+                && isLeapMonth == other.isLeapMonth
+                && calendar == other.calendar
+                && timeZone == other.timeZone)
     }
     
     public convenience required init?(coder aDecoder: NSCoder) {
diff --git a/Foundation/NSCharacterSet.swift b/Foundation/NSCharacterSet.swift
index 6d2e8b3..c2c72b7 100644
--- a/Foundation/NSCharacterSet.swift
+++ b/Foundation/NSCharacterSet.swift
@@ -50,12 +50,14 @@
     }
     
     open override func isEqual(_ value: Any?) -> Bool {
-        if let cs = value as? CharacterSet {
-            return CFEqual(_cfObject, cs._cfObject)
-        } else if let cs = value as? NSCharacterSet {
-            return CFEqual(_cfObject, cs._cfObject)
+        switch value {
+        case let other as CharacterSet:
+            return CFEqual(_cfObject, other._cfObject)
+        case let other as NSCharacterSet:
+            return CFEqual(_cfObject, other._cfObject)
+        default:
+            return false
         }
-        return false
     }
     
     open override var description: String {
diff --git a/Foundation/NSConcreteValue.swift b/Foundation/NSConcreteValue.swift
index e745f34..6b01414 100644
--- a/Foundation/NSConcreteValue.swift
+++ b/Foundation/NSConcreteValue.swift
@@ -167,12 +167,8 @@
     }
     
     override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? NSConcreteValue {
-            return self._typeInfo == other._typeInfo &&
-                   self._isEqualToValue(other)
-        } else {
-            return false
-        }
+        guard let other = value as? NSConcreteValue else { return false }
+        return self._typeInfo == other._typeInfo && self._isEqualToValue(other)
     }
 
     override var hash: Int {
diff --git a/Foundation/NSDate.swift b/Foundation/NSDate.swift
index 7191224..9d4793d 100644
--- a/Foundation/NSDate.swift
+++ b/Foundation/NSDate.swift
@@ -29,12 +29,14 @@
     }
     
     open override func isEqual(_ value: Any?) -> Bool {
-        if let date = value as? Date {
-            return isEqual(to: date)
-        } else if let date = value as? NSDate {
-            return isEqual(to: Date(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate))
+        switch value {
+        case let other as Date:
+            return isEqual(to: other)
+        case let other as NSDate:
+            return isEqual(to: Date(timeIntervalSinceReferenceDate: other.timeIntervalSinceReferenceDate))
+        default:
+            return false
         }
-        return false
     }
     
     deinit {
diff --git a/Foundation/NSDecimalNumber.swift b/Foundation/NSDecimalNumber.swift
index d114bf8..c540332 100644
--- a/Foundation/NSDecimalNumber.swift
+++ b/Foundation/NSDecimalNumber.swift
@@ -341,11 +341,8 @@
     }
 
     open override func isEqual(_ value: Any?) -> Bool {
-        if let number = value as? NSDecimalNumber {
-            return self.decimal == number.decimal
-        } else {
-            return false
-        }
+        guard let other = value as? NSDecimalNumber else { return false }
+        return self.decimal == other.decimal
     }
 
 }
diff --git a/Foundation/NSDictionary.swift b/Foundation/NSDictionary.swift
index b6e1913..defa889 100644
--- a/Foundation/NSDictionary.swift
+++ b/Foundation/NSDictionary.swift
@@ -142,12 +142,14 @@
     }
 
     open override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? Dictionary<AnyHashable, Any> {
+        switch value {
+        case let other as Dictionary<AnyHashable, Any>:
             return isEqual(to: other)
-        } else if let other = value as? NSDictionary {
+        case let other as NSDictionary:
             return isEqual(to: Dictionary._unconditionallyBridgeFromObjectiveC(other))
+        default:
+            return false
         }
-        return false
     }
 
     open override var hash: Int {
diff --git a/Foundation/NSError.swift b/Foundation/NSError.swift
index 576993a..c83403e 100644
--- a/Foundation/NSError.swift
+++ b/Foundation/NSError.swift
@@ -169,11 +169,8 @@
     
     override open func isEqual(_ object: Any?) -> Bool {
         // Pulled from NSObject itself; this works on all platforms.
-        if let obj = object as? NSError {
-            return obj === self
-        }
-        
-        return false
+        guard let obj = object as? NSError else { return false }
+        return obj === self
     }
 }
 
diff --git a/Foundation/NSNumber.swift b/Foundation/NSNumber.swift
index e7334ef..854f4ac 100644
--- a/Foundation/NSNumber.swift
+++ b/Foundation/NSNumber.swift
@@ -212,16 +212,18 @@
     }
     
     open override func isEqual(_ value: Any?) -> Bool {
-        if let number = value as? Int {
-            return intValue == number
-        } else if let number = value as? Double {
-            return doubleValue == number
-        } else if let number = value as? Bool {
-            return boolValue == number
-        } else if let number = value as? NSNumber {
-            return CFEqual(_cfObject, number._cfObject)
+        switch value {
+        case let other as Int:
+            return intValue == other
+        case let other as Double:
+            return doubleValue == other
+        case let other as Bool:
+            return boolValue == other
+        case let other as NSNumber:
+            return CFEqual(_cfObject, other._cfObject)
+        default:
+            return false
         }
-        return false
     }
 
     open override var objCType: UnsafePointer<Int8> {
diff --git a/Foundation/NSObject.swift b/Foundation/NSObject.swift
index 8d4b23f..2c5956f 100644
--- a/Foundation/NSObject.swift
+++ b/Foundation/NSObject.swift
@@ -186,10 +186,8 @@
     /// - Parameter object: The object with which to compare the instance.
     /// - Returns:          `true` if the instance is equal to `object`, otherwise `false`.
     open func isEqual(_ object: Any?) -> Bool {
-        if let obj = object as? NSObject {
-            return obj === self
-        }
-        return false
+        guard let obj = object as? NSObject else { return false }
+        return obj === self
     }
     
     /// Returns an integer that can be used as a table address in a hash table structure.
diff --git a/Foundation/NSOrderedSet.swift b/Foundation/NSOrderedSet.swift
index 79243c0..33b5f4e 100644
--- a/Foundation/NSOrderedSet.swift
+++ b/Foundation/NSOrderedSet.swift
@@ -33,11 +33,8 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let orderedSet = object as? NSOrderedSet {
-            return isEqual(to: orderedSet)
-        } else {
-            return false
-        }
+        guard let orderedSet = object as? NSOrderedSet else { return false }
+        return isEqual(to: orderedSet)
     }
     
     open func encode(with aCoder: NSCoder) {
diff --git a/Foundation/NSPredicate.swift b/Foundation/NSPredicate.swift
index 3aada9c..b5b4642 100644
--- a/Foundation/NSPredicate.swift
+++ b/Foundation/NSPredicate.swift
@@ -73,25 +73,23 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let other = object as? NSPredicate {
-            if other === self {
-                return true
-            } else {
-                switch (other.kind, self.kind) {
-                case (.boolean(let otherBool), .boolean(let selfBool)):
-                    return otherBool == selfBool
-                case (.format, .format):
-                    NSUnimplemented()
-                case (.metadataQuery, .metadataQuery):
-                    NSUnimplemented()
-                default:
-                    // NSBlockPredicate returns false even for copy
-                    return false
-                }
+        guard let other = object as? NSPredicate else { return false }
+        
+        if other === self {
+            return true
+        } else {
+            switch (other.kind, self.kind) {
+            case (.boolean(let otherBool), .boolean(let selfBool)):
+                return otherBool == selfBool
+            case (.format, .format):
+                NSUnimplemented()
+            case (.metadataQuery, .metadataQuery):
+                NSUnimplemented()
+            default:
+                // NSBlockPredicate returns false even for copy
+                return false
             }
         }
-        
-        return false
     }
     
     // Parse predicateFormat and return an appropriate predicate
diff --git a/Foundation/NSSet.swift b/Foundation/NSSet.swift
index 784b372..fdd5b99 100644
--- a/Foundation/NSSet.swift
+++ b/Foundation/NSSet.swift
@@ -114,12 +114,14 @@
     }
 
     open override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? Set<AnyHashable> {
+        switch value {
+        case let other as Set<AnyHashable>:
             return isEqual(to: other)
-        } else if let other = value as? NSSet {
+        case let other as NSSet:
             return isEqual(to: Set._unconditionallyBridgeFromObjectiveC(other))
+        default:
+            return false
         }
-        return false
     }
 
     open override var hash: Int {
diff --git a/Foundation/NSSpecialValue.swift b/Foundation/NSSpecialValue.swift
index af88300..42bc6d9 100644
--- a/Foundation/NSSpecialValue.swift
+++ b/Foundation/NSSpecialValue.swift
@@ -134,14 +134,14 @@
     }
     
     override func isEqual(_ value: Any?) -> Bool {
-        if let object = value as? NSObject {
-            if self === object {
-                return true
-            } else if let special = object as? NSSpecialValue {
-                return _value.isEqual(special._value)
-            }
+        switch value {
+        case let other as NSSpecialValue:
+            return _value.isEqual(other._value)
+        case let other as NSObject:
+            return self === other
+        default:
+            return false
         }
-        return false
     }
     
     override var hash: Int {
diff --git a/Foundation/NSSwiftRuntime.swift b/Foundation/NSSwiftRuntime.swift
index 116ff78..55e3279 100644
--- a/Foundation/NSSwiftRuntime.swift
+++ b/Foundation/NSSwiftRuntime.swift
@@ -39,10 +39,8 @@
     }
     
     override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? NSObject {
-            return CFEqual(self, other)
-        }
-        return false
+        guard let other = value as? NSObject else { return false }
+        return CFEqual(self, other)
     }
     
     override var description: String {
diff --git a/Foundation/NSTimeZone.swift b/Foundation/NSTimeZone.swift
index a8d7227..f022dfa 100644
--- a/Foundation/NSTimeZone.swift
+++ b/Foundation/NSTimeZone.swift
@@ -54,11 +54,8 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let tz = object as? NSTimeZone {
-            return isEqual(to: tz._swiftObject)
-        } else {
-            return false
-        }
+        guard let other = object as? NSTimeZone else { return false }
+        return isEqual(to: other._swiftObject)
     }
     
     open override var description: String {
diff --git a/Foundation/NSURL.swift b/Foundation/NSURL.swift
index d6f3207..7c74030 100644
--- a/Foundation/NSURL.swift
+++ b/Foundation/NSURL.swift
@@ -257,11 +257,8 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let url = object as? NSURL {
-            return CFEqual(_cfObject, url._cfObject)
-        } else {
-            return false
-        }
+        guard let other = object as? NSURL else { return false }
+        return CFEqual(_cfObject, other._cfObject)
     }
     
     open override var description: String {
@@ -940,13 +937,10 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let other = object as? NSURLQueryItem {
-            return other === self
+        guard let other = object as? NSURLQueryItem else { return false }
+        return other === self
                 || (other.name == self.name
                     && other.value == self.value)
-        }
-        
-        return false
     }
     
     open let name: String
@@ -961,34 +955,16 @@
     }
 
     open override func isEqual(_ object: Any?) -> Bool {
-        if let other = object as? NSURLComponents {
-            if scheme != other.scheme {
-                return false
-            }
-            if user != other.user {
-                return false
-            }
-            if password != other.password {
-                return false
-            }
-            if host != other.host {
-                return false
-            }
-            if port != other.port {
-                return false
-            }
-            if path != other.path {
-                return false
-            }
-            if query != other.query {
-                return false
-            }
-            if fragment != other.fragment {
-                return false
-            }
-            return true
-        }
-        return false
+        guard let other = object as? NSURLComponents else { return false }
+        return self === other
+            || (scheme == other.scheme
+                && user == other.user
+                && password == other.password
+                && host == other.host
+                && port == other.port
+                && path == other.path
+                && query == other.query
+                && fragment == other.fragment)
     }
 
     open func copy(with zone: NSZone? = nil) -> Any {
diff --git a/Foundation/NSURLCredential.swift b/Foundation/NSURLCredential.swift
index 32cd075..319bdaa 100644
--- a/Foundation/NSURLCredential.swift
+++ b/Foundation/NSURLCredential.swift
@@ -106,14 +106,11 @@
     }
     
     open override func isEqual(_ object: Any?) -> Bool {
-        if let other = object as? URLCredential {
-            return other === self
-                || (other._user == self._user
-                    && other._password == self._password
-                    && other._persistence == self._persistence)
-        }
-        
-        return false
+        guard let other = object as? URLCredential else { return false }
+        return other === self
+            || (other._user == self._user
+                && other._password == self._password
+                && other._persistence == self._persistence)
     }
     
     /*!
diff --git a/Foundation/NSURLRequest.swift b/Foundation/NSURLRequest.swift
index 438aa6d..0e143f2 100644
--- a/Foundation/NSURLRequest.swift
+++ b/Foundation/NSURLRequest.swift
@@ -240,17 +240,15 @@
         //httBody
         //networkServiceType
         //httpShouldUsePipelining
-        if let other = object as? NSURLRequest {
-            return other === self
-                || (other.url == self.url
-                    && other.mainDocumentURL == self.mainDocumentURL
-                    && other.httpMethod == self.httpMethod
-                    && other.cachePolicy == self.cachePolicy
-                    && other.httpBodyStream == self.httpBodyStream
-                    && other.allowsCellularAccess == self.allowsCellularAccess
-                    && other.httpShouldHandleCookies == self.httpShouldHandleCookies)
-        }
-        return false
+        guard let other = object as? NSURLRequest else { return false }
+        return other === self
+            || (other.url == self.url
+                && other.mainDocumentURL == self.mainDocumentURL
+                && other.httpMethod == self.httpMethod
+                && other.cachePolicy == self.cachePolicy
+                && other.httpBodyStream == self.httpBodyStream
+                && other.allowsCellularAccess == self.allowsCellularAccess
+                && other.httpShouldHandleCookies == self.httpShouldHandleCookies)
     }
     
     /// Indicates that NSURLRequest implements the NSSecureCoding protocol.
diff --git a/Foundation/NSUUID.swift b/Foundation/NSUUID.swift
index e43e06b..37126eb 100644
--- a/Foundation/NSUUID.swift
+++ b/Foundation/NSUUID.swift
@@ -78,7 +78,8 @@
     }
     
     open override func isEqual(_ value: Any?) -> Bool {
-        if let other = value as? UUID {
+        switch value {
+        case let other as UUID:
             return other.uuid.0 == buffer[0] &&
                 other.uuid.1 == buffer[1] &&
                 other.uuid.2 == buffer[2] &&
@@ -95,13 +96,11 @@
                 other.uuid.13 == buffer[13] &&
                 other.uuid.14 == buffer[14] &&
                 other.uuid.15 == buffer[15]
-        } else if let other = value as? NSUUID {
-            if other === self {
-                return true
-            }
-            return _cf_uuid_compare(buffer, other.buffer) == 0
+        case let other as NSUUID:
+            return other === self || _cf_uuid_compare(buffer, other.buffer) == 0
+        default:
+            return false
         }
-        return false
     }
     
     open override var hash: Int {
diff --git a/Foundation/NSValue.swift b/Foundation/NSValue.swift
index 685923b..5ef72d9 100644
--- a/Foundation/NSValue.swift
+++ b/Foundation/NSValue.swift
@@ -52,21 +52,19 @@
     }
     
     open override func isEqual(_ value: Any?) -> Bool {
-        if let object = value as? NSValue {
-            if self === object {
-                return true
-            } else {
-                // bypass _concreteValue accessor in order to avoid acquiring lock twice
-                let (lhs, rhs) = NSValue.SideTableLock.synchronized {
-                    return (NSValue.SideTable[ObjectIdentifier(self)],
-                            NSValue.SideTable[ObjectIdentifier(object)])
-                }
-                if let lhs = lhs, let rhs = rhs {
-                    return lhs.isEqual(rhs)
-                }
+        guard let object = value as? NSValue else { return false }
+        
+        if self === object {
+            return true
+        } else {
+            // bypass _concreteValue accessor in order to avoid acquiring lock twice
+            let (lhs, rhs) = NSValue.SideTableLock.synchronized {
+                return (NSValue.SideTable[ObjectIdentifier(self)],
+                        NSValue.SideTable[ObjectIdentifier(object)])
             }
+            guard let left = lhs, let right = rhs else { return false }
+            return left.isEqual(right)
         }
-        return false
     }
     
     open override var description : String {