From 66ed42eb1d873fb477134fdba5d1aaf3726b378c Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Thu, 19 Sep 2024 09:18:20 -0300 Subject: [PATCH] Upgrade SwiftLint To support Swift 6 syntax such as typed throws. The changes are to address the violation of the optional_data_string_conversion rule. --- Mintfile | 2 +- Sources/BuildTool/BuildTool.swift | 4 ++-- Sources/BuildTool/String+Decoding.swift | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 Sources/BuildTool/String+Decoding.swift diff --git a/Mintfile b/Mintfile index 98ff1ee0..3878610b 100644 --- a/Mintfile +++ b/Mintfile @@ -1,2 +1,2 @@ -realm/SwiftLint@0.55.1 +realm/SwiftLint@0.57.0 nicklockwood/SwiftFormat@0.54.3 diff --git a/Sources/BuildTool/BuildTool.swift b/Sources/BuildTool/BuildTool.swift index b6fdc634..1a5bd27e 100644 --- a/Sources/BuildTool/BuildTool.swift +++ b/Sources/BuildTool/BuildTool.swift @@ -83,7 +83,7 @@ struct GenerateMatrices: ParsableCommand { ] // I’m assuming the JSONSerialization output has no newlines - let keyValue = try "matrix=\(String(decoding: JSONSerialization.data(withJSONObject: matrix), as: UTF8.self))" + let keyValue = try "matrix=\(String(data: JSONSerialization.data(withJSONObject: matrix), encoding: .utf8))" fputs("\(keyValue)\n", stderr) print(keyValue) } @@ -173,6 +173,6 @@ struct Lint: AsyncParsableCommand { private func loadUTF8StringFromFile(at path: String) async throws -> String { let (data, _) = try await URLSession.shared.data(from: .init(filePath: path)) - return String(decoding: data, as: UTF8.self) + return try String(data: data, encoding: .utf8) } } diff --git a/Sources/BuildTool/String+Decoding.swift b/Sources/BuildTool/String+Decoding.swift new file mode 100644 index 00000000..13565bdb --- /dev/null +++ b/Sources/BuildTool/String+Decoding.swift @@ -0,0 +1,16 @@ +import Foundation + +extension String { + enum DecodingError: Swift.Error { + case decodingFailed + } + + /// Like `init(data:encoding:)`, but indicates decoding failure by throwing an error instead of returning an optional. + init(data: Data, encoding: String.Encoding) throws { + guard let decoded = String(data: data, encoding: encoding) else { + throw DecodingError.decodingFailed + } + + self = decoded + } +}