Skip to content

Commit ca7d0db

Browse files
LucianoPAlmeidaRobert Widmann
authored and
Robert Widmann
committed
[stdlib][Qol] SR-11295 Removing stdlib unnecessary coercions (#27165)
* Removing unnecessary casts from stdlib * Minor adjustments * [stdlib][qol] Clean up error variable assign NSError * Removing unnecessary coercions after removing DeclRefExpr restriction.
1 parent a1ebe28 commit ca7d0db

File tree

11 files changed

+17
-22
lines changed

11 files changed

+17
-22
lines changed

Darwin/Foundation-swiftoverlay/DateComponents.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,7 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab
204204

205205
/// Returns a `Date` calculated from the current components using the `calendar` property.
206206
public var date: Date? {
207-
if let d = _handle.map({$0.date}) {
208-
return d as Date
209-
} else {
210-
return nil
211-
}
207+
return _handle.map { $0.date }
212208
}
213209

214210
// MARK: - Generic Setter/Getters

Darwin/Foundation-swiftoverlay/FileManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension FileManager {
4848
return __NSFileManagerEnumeratorAtURL(self, url, keys, mask, { (url, error) in
4949
var errorResult = true
5050
if let h = handler {
51-
errorResult = h(url as URL, error)
51+
errorResult = h(url, error)
5252
}
5353
return errorResult
5454
})

Darwin/Foundation-swiftoverlay/IndexSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
600600
stop.pointee = true
601601
return false
602602
}
603-
}) as IndexSet
603+
})
604604
if let e = error {
605605
throw e
606606
} else {

Darwin/Foundation-swiftoverlay/NSCoder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extension NSCoder {
5353
if let theClasses = classes {
5454
classesAsNSObjects = NSSet(array: theClasses.map { $0 as AnyObject })
5555
}
56-
return __NSCoderDecodeObjectOfClassesForKey(self, classesAsNSObjects, key, nil).map { $0 as Any }
56+
return __NSCoderDecodeObjectOfClassesForKey(self, classesAsNSObjects, key, nil).map { $0 }
5757
}
5858

5959
@nonobjc
@@ -62,7 +62,7 @@ extension NSCoder {
6262
var error: NSError?
6363
let result = __NSCoderDecodeObject(self, &error)
6464
try resolveError(error)
65-
return result.map { $0 as Any }
65+
return result.map { $0 }
6666
}
6767

6868
@available(*, unavailable, renamed: "decodeTopLevelObject(forKey:)")
@@ -125,7 +125,7 @@ extension NSCoder {
125125
}
126126
let result = __NSCoderDecodeObjectOfClassesForKey(self, classesAsNSObjects, key, &error)
127127
try resolveError(error)
128-
return result.map { $0 as Any }
128+
return result.map { $0 }
129129
}
130130
}
131131

Darwin/Foundation-swiftoverlay/NSDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension Dictionary {
6464
extension Dictionary : _ObjectiveCBridgeable {
6565
@_semantics("convertToObjectiveC")
6666
public func _bridgeToObjectiveC() -> NSDictionary {
67-
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject,
67+
return unsafeBitCast(_bridgeToObjectiveCImpl(),
6868
to: NSDictionary.self)
6969
}
7070

Darwin/Foundation-swiftoverlay/NSError.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ public func _getErrorDefaultUserInfo<T: Error>(_ error: T)
234234
if domain != NSCocoaErrorDomain {
235235
_errorDomainUserInfoProviderQueue.sync {
236236
if NSError.userInfoValueProvider(forDomain: domain) != nil { return }
237-
NSError.setUserInfoValueProvider(forDomain: domain) { (nsError, key) in
238-
let error = nsError as Error
237+
NSError.setUserInfoValueProvider(forDomain: domain) { (error, key) in
239238

240239
switch key {
241240
case NSLocalizedDescriptionKey:

Darwin/Foundation-swiftoverlay/NSSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension NSOrderedSet : Sequence {
5555
extension Set : _ObjectiveCBridgeable {
5656
@_semantics("convertToObjectiveC")
5757
public func _bridgeToObjectiveC() -> NSSet {
58-
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject, to: NSSet.self)
58+
return unsafeBitCast(_bridgeToObjectiveCImpl(), to: NSSet.self)
5959
}
6060

6161
public static func _forceBridgeFromObjectiveC(_ s: NSSet, result: inout Set?) {

Darwin/Foundation-swiftoverlay/NSStringAPI.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ extension String {
353353
usedEncoding: inout Encoding
354354
) throws {
355355
var enc: UInt = 0
356-
let ns = try NSString(contentsOf: url as URL, usedEncoding: &enc)
356+
let ns = try NSString(contentsOf: url, usedEncoding: &enc)
357357
usedEncoding = Encoding(rawValue: enc)
358358
self = String._unconditionallyBridgeFromObjectiveC(ns)
359359
}
@@ -566,7 +566,7 @@ extension StringProtocol where Index == String.Index {
566566
/// Case transformations aren’t guaranteed to be symmetrical or to produce
567567
/// strings of the same lengths as the originals.
568568
public var capitalized: String {
569-
return _ns.capitalized as String
569+
return _ns.capitalized
570570
}
571571

572572
// @property (readonly, copy) NSString *localizedCapitalizedString NS_AVAILABLE(10_11, 9_0);
@@ -583,7 +583,7 @@ extension StringProtocol where Index == String.Index {
583583
/// Returns a capitalized representation of the string
584584
/// using the specified locale.
585585
public func capitalized(with locale: Locale?) -> String {
586-
return _ns.capitalized(with: locale) as String
586+
return _ns.capitalized(with: locale)
587587
}
588588

589589
// - (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
@@ -1166,7 +1166,7 @@ extension StringProtocol where Index == String.Index {
11661166
/// locale.
11671167
@available(macOS 10.11, iOS 9.0, *)
11681168
public var localizedUppercase: String {
1169-
return _ns.localizedUppercase as String
1169+
return _ns.localizedUppercase
11701170
}
11711171

11721172
// - (NSString *)uppercaseStringWithLocale:(Locale *)locale

Darwin/Foundation-swiftoverlay/Progress.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension Progress {
1717
public var estimatedTimeRemaining: TimeInterval? {
1818
get {
1919
guard let v = self.__estimatedTimeRemaining else { return nil }
20-
return v.doubleValue as TimeInterval
20+
return v.doubleValue
2121
}
2222
set {
2323
guard let nv = newValue else {

Darwin/Foundation-swiftoverlay/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension String : _ObjectiveCBridgeable {
3232
// This method should not do anything extra except calling into the
3333
// implementation inside core. (These two entry points should be
3434
// equivalent.)
35-
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject, to: NSString.self)
35+
return unsafeBitCast(_bridgeToObjectiveCImpl(), to: NSString.self)
3636
}
3737

3838
public static func _forceBridgeFromObjectiveC(

Darwin/Foundation-swiftoverlay/URL.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ public struct URL : ReferenceConvertible, Equatable {
10901090
/// Returns bookmark data for the URL, created with specified options and resource values.
10911091
public func bookmarkData(options: BookmarkCreationOptions = [], includingResourceValuesForKeys keys: Set<URLResourceKey>? = nil, relativeTo url: URL? = nil) throws -> Data {
10921092
let result = try _url.bookmarkData(options: options, includingResourceValuesForKeys: keys.flatMap { Array($0) }, relativeTo: url)
1093-
return result as Data
1093+
return result
10941094
}
10951095

10961096
/// Returns the resource values for properties identified by a specified array of keys contained in specified bookmark data. If the result dictionary does not contain a resource value for one or more of the requested resource keys, it means those resource properties are not available in the bookmark data.
@@ -1107,7 +1107,7 @@ public struct URL : ReferenceConvertible, Equatable {
11071107
/// Initializes and returns bookmark data derived from an alias file pointed to by a specified URL. If bookmarkFileURL refers to an alias file created prior to OS X v10.6 that contains Alias Manager information but no bookmark data, this method synthesizes bookmark data for the file.
11081108
public static func bookmarkData(withContentsOf url: URL) throws -> Data {
11091109
let result = try NSURL.bookmarkData(withContentsOf: url)
1110-
return result as Data
1110+
return result
11111111
}
11121112

11131113
/// Given an NSURL created by resolving a bookmark data created with security scope, make the resource referenced by the url accessible to the process. When access to this resource is no longer needed the client must call stopAccessingSecurityScopedResource. Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource (Note: this is not reference counted).

0 commit comments

Comments
 (0)