Skip to content

Commit d454c55

Browse files
authored
Fix SwiftLint warnings (#1198)
1 parent 1ac2ed6 commit d454c55

File tree

11 files changed

+49
-53
lines changed

11 files changed

+49
-53
lines changed

Sources/SwifterSwift/Foundation/URLRequestExtensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public extension URLRequest {
3838
}
3939
}
4040

41-
if let data = httpBody,
42-
let body = String(data: data, encoding: .utf8) {
41+
if let data = httpBody {
42+
let body = String(decoding: data, as: UTF8.self)
4343
command.append("-d '\(body)'")
4444
}
4545

Sources/SwifterSwift/SwiftStdlib/DictionaryExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public extension Dictionary {
9292
let options = (prettify == true) ? JSONSerialization.WritingOptions.prettyPrinted : JSONSerialization
9393
.WritingOptions()
9494
guard let jsonData = try? JSONSerialization.data(withJSONObject: self, options: options) else { return nil }
95-
return String(data: jsonData, encoding: .utf8)
95+
return String(decoding: jsonData, as: UTF8.self)
9696
}
9797
#endif
9898

Sources/SwifterSwift/SwiftStdlib/SequenceExtensions.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public extension Sequence {
176176
/// - Parameter compare: Comparison function that will determine the ordering.
177177
/// - Returns: The sorted array.
178178
func sorted<T>(by map: (Element) throws -> T, with compare: (T, T) -> Bool) rethrows -> [Element] {
179-
return try sorted { compare(try map($0), try map($1)) }
179+
return try sorted { try compare(map($0), map($1)) }
180180
}
181181

182182
/// SwifterSwift: Return a sorted array based on a key path.
@@ -211,8 +211,8 @@ public extension Sequence {
211211
}
212212
}
213213

214-
/// SwifterSwift: Returns a sorted sequence based on two map functions. The second one will be used in case the values
215-
/// of the first one match.
214+
/// SwifterSwift: Returns a sorted sequence based on two map functions. The second one will be used in case the
215+
/// values of the first one match.
216216
///
217217
/// - Parameters:
218218
/// - map1: Map function to sort by. Output type must be Comparable.
@@ -250,13 +250,14 @@ public extension Sequence {
250250
}
251251
}
252252

253-
/// SwifterSwift: Returns a sorted sequence based on three map functions. Whenever the values of one map function match, the
254-
/// next one will be used.
253+
/// SwifterSwift: Returns a sorted sequence based on three map functions. Whenever the values of one map function
254+
/// match, the next one will be used.
255255
///
256256
/// - Parameters:
257257
/// - map1: Map function to sort by. Output type must be Comparable.
258258
/// - map2: Map function to sort by in case the values of `map1` match. Output type must be Comparable.
259-
/// - map3: Map function to sort by in case the values of `map1` and `map2` match. Output type must be Comparable.
259+
/// - map3: Map function to sort by in case the values of `map1` and `map2` match.
260+
/// Output type must be Comparable.
260261
func sorted<T: Comparable, U: Comparable, V: Comparable>(by map1: (Element) throws -> T,
261262
and map2: (Element) throws -> U,
262263
and map3: (Element) throws -> V) rethrows -> [Element] {
@@ -266,7 +267,7 @@ public extension Sequence {
266267
if value10 != value11 {
267268
return value10 < value11
268269
}
269-
270+
270271
let value20 = try map2($0)
271272
let value21 = try map2($1)
272273
if value20 != value21 {
@@ -326,8 +327,8 @@ public extension Sequence {
326327
/// - Parameters:
327328
/// - map: Function for `Element` to compare.
328329
/// - value: The value to compare with `Element` property.
329-
/// - Returns: The first element of the collection that has property by given map function equals to given `value` or
330-
/// `nil` if there is no such element.
330+
/// - Returns: The first element of the collection that has property by given map function equals to given `value`
331+
/// or `nil` if there is no such element.
331332
func first<T: Equatable>(where map: (Element) throws -> T, equals value: T) rethrows -> Element? {
332333
return try first { try map($0) == value }
333334
}

Sources/SwifterSwift/SwiftStdlib/StringExtensions.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public extension String {
2727
var base64Decoded: String? {
2828
if let data = Data(base64Encoded: self,
2929
options: .ignoreUnknownCharacters) {
30-
return String(data: data, encoding: .utf8)
30+
return String(decoding: data, as: UTF8.self)
3131
}
3232

3333
let remainder = count % 4
@@ -40,7 +40,7 @@ public extension String {
4040
guard let data = Data(base64Encoded: self + padding,
4141
options: .ignoreUnknownCharacters) else { return nil }
4242

43-
return String(data: data, encoding: .utf8)
43+
return String(decoding: data, as: UTF8.self)
4444
}
4545
#endif
4646

@@ -1228,8 +1228,7 @@ public extension String {
12281228
/// - Parameter base64: base64 string.
12291229
init?(base64: String) {
12301230
guard let decodedData = Data(base64Encoded: base64) else { return nil }
1231-
guard let str = String(data: decodedData, encoding: .utf8) else { return nil }
1232-
self.init(str)
1231+
self.init(String(decoding: decodedData, as: UTF8.self))
12331232
}
12341233
#endif
12351234
}

Tests/CryptoKitTests/DigestExtensionsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import CryptoKit
99
final class DigestExtensionsTests: XCTestCase {
1010
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
1111
func testHexString() {
12-
guard let data = "Hello, World!".data(using: .utf8) else { return }
12+
let data = Data("Hello, World!".utf8)
1313
XCTAssertEqual(
1414
SHA256.hash(data: data).hexString,
1515
"DFFD6021BB2BD5B0AF676290809EC3A53191DD81C7F70A4B28688A362182986F")

Tests/FoundationTests/DataExtensionsTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@ import Foundation
88

99
final class DataExtensionsTests: XCTestCase {
1010
func testString() {
11-
let dataFromString = "hello".data(using: .utf8)
11+
let dataFromString = Data("hello".utf8)
1212
XCTAssertNotNil(dataFromString)
13-
XCTAssertNotNil(dataFromString?.string(encoding: .utf8))
14-
XCTAssertEqual(dataFromString?.string(encoding: .utf8), "hello")
13+
XCTAssertNotNil(dataFromString.string(encoding: .utf8))
14+
XCTAssertEqual(dataFromString.string(encoding: .utf8), "hello")
1515
}
1616

1717
func testBytes() {
18-
let dataFromString = "hello".data(using: .utf8)
19-
let bytes = dataFromString?.bytes
18+
let dataFromString = Data("hello".utf8)
19+
let bytes = dataFromString.bytes
2020
XCTAssertNotNil(bytes)
21-
XCTAssertEqual(bytes?.count, 5)
21+
XCTAssertEqual(bytes.count, 5)
2222
}
2323

2424
func testJsonObject() {
25-
let invalidData = "hello".data(using: .utf8)
26-
XCTAssertThrowsError(try invalidData?.jsonObject())
27-
XCTAssertThrowsError(try invalidData?.jsonObject(options: [.allowFragments]))
25+
let invalidData = Data("hello".utf8)
26+
XCTAssertThrowsError(try invalidData.jsonObject())
27+
XCTAssertThrowsError(try invalidData.jsonObject(options: [.allowFragments]))
2828

29-
let stringData = "\"hello\"".data(using: .utf8)
30-
XCTAssertThrowsError(try stringData?.jsonObject())
31-
XCTAssertEqual((try? stringData?.jsonObject(options: [.allowFragments])) as? String, "hello")
29+
let stringData = Data("\"hello\"".utf8)
30+
XCTAssertThrowsError(try stringData.jsonObject())
31+
XCTAssertEqual((try? stringData.jsonObject(options: [.allowFragments])) as? String, "hello")
3232

33-
let objectData = "{\"message\": \"hello\"}".data(using: .utf8)
34-
let object = (try? objectData?.jsonObject()) as? [String: String]
33+
let objectData = Data("{\"message\": \"hello\"}".utf8)
34+
let object = (try? objectData.jsonObject()) as? [String: String]
3535
XCTAssertNotNil(object)
3636
XCTAssertEqual(object?["message"], "hello")
3737

38-
let arrayData = "[\"hello\"]".data(using: .utf8)
39-
let array = (try? arrayData?.jsonObject()) as? [String]
38+
let arrayData = Data("[\"hello\"]".utf8)
39+
let array = (try? arrayData.jsonObject()) as? [String]
4040
XCTAssertNotNil(array)
4141
XCTAssertEqual(array?.first, "hello")
4242
}

Tests/FoundationTests/URLSessionExtensionsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class URLSessionExtensionsTests: XCTestCase {
2727
XCTAssertNotNil(data)
2828
XCTAssertNotNil(response)
2929
let httpResponse = response as? HTTPURLResponse
30-
let content = String(data: data!, encoding: .utf8)
30+
let content = String(decoding: data!, as: UTF8.self)
3131
XCTAssertEqual(content, gemfileContent)
3232
XCTAssertNotNil(httpResponse)
3333
XCTAssertEqual(httpResponse!.statusCode, 200)

Tests/StoreKitTests/SKProductTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class SKProductTests: XCTestCase {
4141
price: "100.00",
4242
priceLocale: Locale(identifier: "nb_NO"))
4343

44-
XCTAssertEqual(noMockedProduct.localizedPrice, "kr 100,00")
44+
XCTAssertEqual(noMockedProduct.localizedPrice, "100,00 kr")
4545

4646
let frMockedProduct = SKProduct(
4747
identifier: "com.swifterswift.frmockedproduct",

Tests/SwiftStdlibTests/DecodableExtensionsTests.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ private struct City: Decodable {
1414

1515
final class DecodableExtensionsTests: XCTestCase {
1616
private var mockJsonData: Data {
17-
return #"{"id": 1, "name": "Şanlıurfa", "url": "https://cdn.pixabay.com/photo/2017/09/27/20/55/sanliurfa-2793424_1280.jpg"}"#
18-
.data(
19-
using: .utf8)!
17+
Data(#"{"id": 1, "name": "Şanlıurfa", "url": "https://cdn.pixabay.com/photo/2017/09/27/20/55/sanliurfa-2793424_1280.jpg"}"#
18+
.utf8)
2019
}
2120

2221
private var invalidMockJsonData: Data {
23-
return #"{"id": "1", "name": "Şanlıurfa", "url": "https://cdn.pixabay.com/photo/2017/09/27/20/55/sanliurfa-2793424_1280.jpg"}"#
24-
.data(
25-
using: .utf8)!
22+
Data(#"{"id": "1", "name": "Şanlıurfa", "url": "https://cdn.pixabay.com/photo/2017/09/27/20/55/sanliurfa-2793424_1280.jpg"}"#
23+
.utf8)
2624
}
2725

2826
func testDecodeModel() {

Tests/SwiftStdlibTests/SequenceExtensionsTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ final class SequenceExtensionsTests: XCTestCase {
112112
XCTAssert([1, 2, 3].contains([2, 3]))
113113
XCTAssert([1, 2, 3].contains([1, 3]))
114114
XCTAssertFalse([1, 2, 3].contains([4, 5]))
115-
116-
XCTAssert([Int]().contains(AnyIterator({ nil })))
115+
116+
XCTAssert([Int]().contains(AnyIterator { nil }))
117117
XCTAssertFalse([Int]().contains(AnyIterator([1, 2].makeIterator())))
118118
XCTAssert([1, 2, 3].contains(AnyIterator([1, 2].makeIterator())))
119119
XCTAssert([1, 2, 3].contains(AnyIterator([2, 3].makeIterator())))
120120
XCTAssert([1, 2, 3].contains(AnyIterator([1, 3].makeIterator())))
121121
XCTAssertFalse([1, 2, 3].contains(AnyIterator([4, 5].makeIterator())))
122-
122+
123123
XCTAssert([Int]().contains(Set<Int>()))
124124
XCTAssertFalse([Int]().contains(Set([1, 2])))
125125
XCTAssert([1, 2, 3].contains(Set([1, 2])))
@@ -148,12 +148,12 @@ final class SequenceExtensionsTests: XCTestCase {
148148
XCTAssertEqual(["James", "Wade", "Bryant"].sum(for: \.count), 15)
149149
XCTAssertEqual(["a", "b", "c", "d"].sum(for: \.count), 4)
150150
}
151-
151+
152152
func testMapFunctionSum() {
153153
XCTAssertEqual(["James", "Wade", "Bryant"].sum(for: { $0.count }), 15)
154154
XCTAssertEqual(["a", "b", "c", "d"].sum(for: { $0.count }), 4)
155155
}
156-
156+
157157
func testProduct() {
158158
XCTAssertEqual([1, 2, 3, 4, 5].product(), 120)
159159
XCTAssertEqual([1.2, 2.3, 3.4, 4.5, 5.6].product(), 236.4768, accuracy: 0.001)
@@ -181,7 +181,7 @@ final class SequenceExtensionsTests: XCTestCase {
181181
let array2 = ["James", "Wade", "Bryant", ""]
182182
XCTAssertEqual(array2.sorted(by: \String.first, with: optionalCompare), ["Bryant", "James", "Wade", ""])
183183
}
184-
184+
185185
func testMapFunctionSorted() {
186186
let array = ["James", "Wade", "Bryant"]
187187
XCTAssertEqual(array.sorted(by: { $0.count }, with: <), ["Wade", "James", "Bryant"])
@@ -213,7 +213,7 @@ final class SequenceExtensionsTests: XCTestCase {
213213
]
214214
XCTAssertEqual(people.sorted(by: \.surname, and: \.age), expectedResult)
215215
}
216-
216+
217217
func testSortedByTwoMapFunctions() {
218218
let people = [
219219
SimplePerson(forename: "Tom", surname: "James", age: 32),
@@ -243,7 +243,7 @@ final class SequenceExtensionsTests: XCTestCase {
243243
]
244244
XCTAssertEqual(people.sorted(by: \.surname, and: \.forename, and: \.age), expectedResult)
245245
}
246-
246+
247247
func testSortedByThreeMapFunctions() {
248248
let people = [
249249
SimplePerson(forename: "Tom", surname: "James", age: 32),
@@ -275,7 +275,7 @@ final class SequenceExtensionsTests: XCTestCase {
275275

276276
XCTAssertNil(missingPerson)
277277
}
278-
278+
279279
func testFirstByMapFunction() {
280280
let array1 = [
281281
Person(name: "John", age: 30, location: Location(city: "Boston")),

0 commit comments

Comments
 (0)