Skip to content

Commit b869a3c

Browse files
committed
[se-0405] improve readability of double-optional unwrapping
1 parent 4617553 commit b869a3c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

stdlib/public/core/String.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,14 @@ extension String {
523523
validating codeUnits: some Sequence<Encoding.CodeUnit>,
524524
as encoding: Encoding.Type
525525
) {
526-
let newString: String?? = codeUnits.withContiguousStorageIfAvailable {
526+
let contiguousResult = codeUnits.withContiguousStorageIfAvailable {
527527
String._validate($0, as: Encoding.self)
528528
}
529-
if let newString {
530-
guard let newString else { return nil }
531-
self = newString
529+
if let validationResult = contiguousResult {
530+
guard let validatedString = validationResult else {
531+
return nil
532+
}
533+
self = validatedString
532534
return
533535
}
534536

@@ -583,14 +585,16 @@ extension String {
583585
validating codeUnits: some Sequence<Int8>,
584586
as encoding: Encoding.Type
585587
) where Encoding: Unicode.Encoding, Encoding.CodeUnit == UInt8 {
586-
let newString: String?? = codeUnits.withContiguousStorageIfAvailable {
588+
let contiguousResult = codeUnits.withContiguousStorageIfAvailable {
587589
$0.withMemoryRebound(to: UInt8.self) {
588590
String._validate($0, as: Encoding.self)
589591
}
590592
}
591-
if let newString {
592-
guard let newString else { return nil }
593-
self = newString
593+
if let validationResult = contiguousResult {
594+
guard let validatedString = validationResult else {
595+
return nil
596+
}
597+
self = validatedString
594598
return
595599
}
596600

0 commit comments

Comments
 (0)