Skip to content

Commit

Permalink
Don’t discard the return value of FileManager.createFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoppen committed Sep 30, 2024
1 parent d2b01fc commit acc3c6c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Sources/generate-swift-format/FileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ protocol FileGenerator {
func write(into handle: FileHandle) throws
}

private struct FailedToCreateFileError: Error {
let url: URL
}

extension FileGenerator {
/// Generates a file at the given URL, overwriting it if it already exists.
func generateFile(at url: URL) throws {
Expand All @@ -27,7 +31,9 @@ extension FileGenerator {
try fm.removeItem(at: url)
}

fm.createFile(atPath: url.path, contents: nil, attributes: nil)
if !fm.createFile(atPath: url.path, contents: nil, attributes: nil) {
throw FailedToCreateFileError(url: url)
}
let handle = try FileHandle(forWritingTo: url)
defer { handle.closeFile() }

Expand Down
7 changes: 6 additions & 1 deletion Tests/swift-formatTests/Utilities/FileIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ extension FileIteratorTests {
let fileURL = tmpURL(path)
try FileManager.default.createDirectory(
at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true)
FileManager.default.createFile(atPath: fileURL.path, contents: Data())
struct FailedToCreateFileError: Error {
let url: URL
}
if !FileManager.default.createFile(atPath: fileURL.path, contents: Data()) {
throw FailedToCreateFileError(url: fileURL)
}
}

/// Create a symlink between files or directories in the test's temporary space.
Expand Down

0 comments on commit acc3c6c

Please sign in to comment.