Skip to content

Commit cd3a060

Browse files
authored
Merge pull request swiftlang#2665 from compnerd/coalesce
Foundation: slight cleanup of `FileManager+Win32` (NFC)
2 parents de12d43 + 0e4de8f commit cd3a060

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -647,22 +647,21 @@ extension FileManager {
647647

648648
@discardableResult
649649
internal func _changeCurrentDirectoryPath(_ path: String) -> Bool {
650-
guard let bResult = try? FileManager.default._fileSystemRepresentation(withPath: path, {
650+
return (try? FileManager.default._fileSystemRepresentation(withPath: path) {
651651
SetCurrentDirectoryW($0)
652-
}) else { return false }
653-
return bResult
652+
}) ?? false
654653
}
655654

656655
internal func _fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
657656
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = WIN32_FILE_ATTRIBUTE_DATA()
658657
do { faAttributes = try windowsFileAttributes(atPath: path) } catch { return false }
659658
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_REPARSE_POINT) == DWORD(FILE_ATTRIBUTE_REPARSE_POINT) {
660-
guard let handle: HANDLE = try? FileManager.default._fileSystemRepresentation(withPath: path, {
659+
let handle: HANDLE = (try? FileManager.default._fileSystemRepresentation(withPath: path) {
661660
CreateFileW($0, /* dwDesiredAccess= */ DWORD(0),
662661
DWORD(FILE_SHARE_READ), /* lpSecurityAttributes= */ nil,
663662
DWORD(OPEN_EXISTING),
664663
DWORD(FILE_FLAG_BACKUP_SEMANTICS), /* hTemplateFile= */ nil)
665-
}) else { return false }
664+
}) ?? INVALID_HANDLE_VALUE
666665
if handle == INVALID_HANDLE_VALUE { return false }
667666
defer { CloseHandle(handle) }
668667

@@ -775,23 +774,23 @@ extension FileManager {
775774
}
776775

777776
internal func _contentsEqual(atPath path1: String, andPath path2: String) -> Bool {
778-
guard let path1Handle: HANDLE = try? FileManager.default._fileSystemRepresentation(withPath: path1, {
777+
let path1Handle: HANDLE = (try? FileManager.default._fileSystemRepresentation(withPath: path1) {
779778
CreateFileW($0, GENERIC_READ,
780779
DWORD(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
781780
nil, DWORD(OPEN_EXISTING),
782781
DWORD(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS),
783782
nil)
784-
}) else { return false }
783+
}) ?? INVALID_HANDLE_VALUE
785784
if path1Handle == INVALID_HANDLE_VALUE { return false }
786785
defer { CloseHandle(path1Handle) }
787786

788-
guard let path2Handle: HANDLE = try? FileManager.default._fileSystemRepresentation(withPath: path2, {
787+
let path2Handle: HANDLE = (try? FileManager.default._fileSystemRepresentation(withPath: path2) {
789788
CreateFileW($0, GENERIC_READ,
790789
DWORD(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
791790
nil, DWORD(OPEN_EXISTING),
792791
DWORD(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS),
793792
nil)
794-
}) else { return false }
793+
}) ?? INVALID_HANDLE_VALUE
795794
if path2Handle == INVALID_HANDLE_VALUE { return false }
796795
defer { CloseHandle(path2Handle) }
797796

@@ -937,9 +936,9 @@ extension FileManager {
937936
var ffd = WIN32_FIND_DATAW()
938937
let capacity = MemoryLayout.size(ofValue: ffd.cFileName)
939938

940-
guard let handle = try? FileManager.default._fileSystemRepresentation(withPath: joinPath(prefix: _lastReturned.path, suffix: "*"), {
939+
let handle = (try? FileManager.default._fileSystemRepresentation(withPath: joinPath(prefix: _lastReturned.path, suffix: "*")) {
941940
FindFirstFileW($0, &ffd)
942-
}) else { return firstValidItem() }
941+
}) ?? INVALID_HANDLE_VALUE
943942
if handle == INVALID_HANDLE_VALUE { return firstValidItem() }
944943
defer { FindClose(handle) }
945944

0 commit comments

Comments
 (0)