Skip to content

Commit ab3c598

Browse files
committed
Use shorthand optional binding
1 parent 1011098 commit ab3c598

12 files changed

+58
-56
lines changed

.swiftlint.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
opt_in_rules:
2+
- shorthand_optional_binding
13
disabled_rules: # rule identifiers to exclude from running
24
- todo
35
- operator_whitespace

Sources/SQLite/Core/Connection+Attach.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension Connection {
1313
#if SQLITE_SWIFT_SQLCIPHER
1414
/// See https://www.zetetic.net/sqlcipher/sqlcipher-api/#attach
1515
public func attach(_ location: Location, as schemaName: String, key: String? = nil) throws {
16-
if let key = key {
16+
if let key {
1717
try run("ATTACH DATABASE ? AS ? KEY ?", location.description, schemaName, key)
1818
} else {
1919
try run("ATTACH DATABASE ? AS ?", location.description, schemaName)

Sources/SQLite/Core/Connection.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public final class Connection {
414414
/// times it’s been called for this lock. If it returns `true`, it will
415415
/// try again. If it returns `false`, no further attempts will be made.
416416
public func busyHandler(_ callback: ((_ tries: Int) -> Bool)?) {
417-
guard let callback = callback else {
417+
guard let callback else {
418418
sqlite3_busy_handler(handle, nil, nil)
419419
busyHandler = nil
420420
return
@@ -449,7 +449,7 @@ public final class Connection {
449449
@available(watchOS, deprecated: 3.0)
450450
@available(tvOS, deprecated: 10.0)
451451
fileprivate func trace_v1(_ callback: ((String) -> Void)?) {
452-
guard let callback = callback else {
452+
guard let callback else {
453453
sqlite3_trace(handle, nil /* xCallback */, nil /* pCtx */)
454454
trace = nil
455455
return
@@ -458,7 +458,7 @@ public final class Connection {
458458
callback(String(cString: pointer.assumingMemoryBound(to: UInt8.self)))
459459
}
460460
sqlite3_trace(handle, { (context: UnsafeMutableRawPointer?, SQL: UnsafePointer<Int8>?) in
461-
if let context = context, let SQL = SQL {
461+
if let context, let SQL {
462462
unsafeBitCast(context, to: Trace.self)(SQL)
463463
}
464464
},
@@ -469,7 +469,7 @@ public final class Connection {
469469

470470
@available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *)
471471
fileprivate func trace_v2(_ callback: ((String) -> Void)?) {
472-
guard let callback = callback else {
472+
guard let callback else {
473473
// If the X callback is NULL or if the M mask is zero, then tracing is disabled.
474474
sqlite3_trace_v2(handle, 0 /* mask */, nil /* xCallback */, nil /* pCtx */)
475475
trace = nil
@@ -485,7 +485,7 @@ public final class Connection {
485485
// callback was invoked. The C argument is a copy of the context pointer.
486486
// The P and X arguments are pointers whose meanings depend on T.
487487
(_: UInt32, context: UnsafeMutableRawPointer?, pointer: UnsafeMutableRawPointer?, _: UnsafeMutableRawPointer?) in
488-
if let pointer = pointer,
488+
if let pointer,
489489
let expandedSQL = sqlite3_expanded_sql(OpaquePointer(pointer)) {
490490
unsafeBitCast(context, to: Trace.self)(expandedSQL)
491491
sqlite3_free(expandedSQL)
@@ -507,7 +507,7 @@ public final class Connection {
507507
/// `.Insert`, `.Update`, or `.Delete`), database name, table name, and
508508
/// rowid.
509509
public func updateHook(_ callback: ((_ operation: Operation, _ db: String, _ table: String, _ rowid: Int64) -> Void)?) {
510-
guard let callback = callback else {
510+
guard let callback else {
511511
sqlite3_update_hook(handle, nil, nil)
512512
updateHook = nil
513513
return
@@ -535,7 +535,7 @@ public final class Connection {
535535
/// committed. If this callback throws, the transaction will be rolled
536536
/// back.
537537
public func commitHook(_ callback: (() throws -> Void)?) {
538-
guard let callback = callback else {
538+
guard let callback else {
539539
sqlite3_commit_hook(handle, nil, nil)
540540
commitHook = nil
541541
return
@@ -562,7 +562,7 @@ public final class Connection {
562562
/// - Parameter callback: A callback invoked when a transaction is rolled
563563
/// back.
564564
public func rollbackHook(_ callback: (() -> Void)?) {
565-
guard let callback = callback else {
565+
guard let callback else {
566566
sqlite3_rollback_hook(handle, nil, nil)
567567
rollbackHook = nil
568568
return
@@ -650,7 +650,7 @@ public final class Connection {
650650
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8,
651651
unsafeBitCast(box, to: UnsafeMutableRawPointer.self), { (callback: UnsafeMutableRawPointer?, _,
652652
lhs: UnsafeRawPointer?, _, rhs: UnsafeRawPointer?) in /* xCompare */
653-
if let lhs = lhs, let rhs = rhs {
653+
if let lhs, let rhs {
654654
return unsafeBitCast(callback, to: Collation.self)(lhs, rhs)
655655
} else {
656656
fatalError("sqlite3_create_collation_v2 callback called with NULL pointer")

Sources/SQLite/Core/Result.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ extension Result: CustomStringConvertible {
5151
public var description: String {
5252
switch self {
5353
case let .error(message, errorCode, statement):
54-
if let statement = statement {
54+
if let statement {
5555
return "\(message) (\(statement)) (code: \(errorCode))"
5656
} else {
5757
return "\(message) (code: \(errorCode))"
5858
}
5959
case let .extendedError(message, extendedCode, statement):
60-
if let statement = statement {
60+
if let statement {
6161
return "\(message) (\(statement)) (extended code: \(extendedCode))"
6262
} else {
6363
return "\(message) (extended code: \(extendedCode))"

Sources/SQLite/Extensions/FTS4.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public struct Tokenizer {
9898
separators: Set<Character> = []) -> Tokenizer {
9999
var arguments = [String]()
100100

101-
if let removeDiacritics = removeDiacritics {
101+
if let removeDiacritics {
102102
arguments.append("remove_diacritics=\(removeDiacritics ? 1 : 0)".quote())
103103
}
104104

@@ -208,13 +208,13 @@ open class FTSConfig {
208208
func options() -> Options {
209209
var options = Options()
210210
options.append(formatColumnDefinitions())
211-
if let tokenizer = tokenizer {
211+
if let tokenizer {
212212
options.append("tokenize", value: Expression<Void>(literal: tokenizer.description))
213213
}
214214
options.appendCommaSeparated("prefix", values: prefixes.sorted().map { String($0) })
215215
if isContentless {
216216
options.append("content", value: "")
217-
} else if let externalContentSchema = externalContentSchema {
217+
} else if let externalContentSchema {
218218
options.append("content", value: externalContentSchema.tableName())
219219
}
220220
return options
@@ -306,19 +306,19 @@ open class FTS4Config: FTSConfig {
306306
for (column, _) in (columnDefinitions.filter { $0.options.contains(.unindexed) }) {
307307
options.append("notindexed", value: column)
308308
}
309-
if let languageId = languageId {
309+
if let languageId {
310310
options.append("languageid", value: languageId)
311311
}
312-
if let compressFunction = compressFunction {
312+
if let compressFunction {
313313
options.append("compress", value: compressFunction)
314314
}
315-
if let uncompressFunction = uncompressFunction {
315+
if let uncompressFunction {
316316
options.append("uncompress", value: uncompressFunction)
317317
}
318-
if let matchInfo = matchInfo {
318+
if let matchInfo {
319319
options.append("matchinfo", value: matchInfo.rawValue)
320320
}
321-
if let order = order {
321+
if let order {
322322
options.append("order", value: order.rawValue)
323323
}
324324
return options

Sources/SQLite/Extensions/FTS5.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ open class FTS5Config: FTSConfig {
6969

7070
override func options() -> Options {
7171
var options = super.options()
72-
if let contentRowId = contentRowId {
72+
if let contentRowId {
7373
options.append("content_rowid", value: contentRowId)
7474
}
75-
if let columnSize = columnSize {
75+
if let columnSize {
7676
options.append("columnsize", value: Expression<Int>(value: columnSize))
7777
}
78-
if let detail = detail {
78+
if let detail {
7979
options.append("detail", value: detail.rawValue)
8080
}
8181
return options

Sources/SQLite/Helpers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ extension String {
109109
}
110110

111111
func transcode(_ literal: Binding?) -> String {
112-
guard let literal = literal else { return "NULL" }
112+
guard let literal else { return "NULL" }
113113

114114
switch literal {
115115
case let blob as Blob:

Sources/SQLite/Schema/SchemaDefinitions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public enum LiteralValue: Equatable, CustomStringConvertible {
170170
// swiftlint:enable identifier_name
171171

172172
init(_ string: String?) {
173-
guard let string = string else {
173+
guard let string else {
174174
self = .NULL
175175
return
176176
}

Sources/SQLite/Schema/SchemaReader.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public class SchemaReader {
3939
type: ObjectDefinition.ObjectType? = nil,
4040
temp: Bool = false) throws -> [ObjectDefinition] {
4141
var query: QueryType = SchemaTable.get(for: connection, temp: temp)
42-
if let name = name {
42+
if let name {
4343
query = query.where(SchemaTable.nameColumn == name)
4444
}
45-
if let type = type {
45+
if let type {
4646
query = query.where(SchemaTable.typeColumn == type.rawValue)
4747
}
4848
return try connection.prepare(query).map { row -> ObjectDefinition in

Sources/SQLite/Typed/Coding.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,39 @@ private class SQLiteEncoder: Encoder {
215215
}
216216

217217
func encodeIfPresent(_ value: Int?, forKey key: SQLiteEncoder.SQLiteKeyedEncodingContainer<Key>.Key) throws {
218-
if let value = value {
218+
if let value {
219219
try encode(value, forKey: key)
220220
} else if forcingNilValueSetters {
221221
encoder.setters.append(Expression<Int?>(key.stringValue) <- nil)
222222
}
223223
}
224224

225225
func encodeIfPresent(_ value: Bool?, forKey key: Key) throws {
226-
if let value = value {
226+
if let value {
227227
try encode(value, forKey: key)
228228
} else if forcingNilValueSetters {
229229
encoder.setters.append(Expression<Bool?>(key.stringValue) <- nil)
230230
}
231231
}
232232

233233
func encodeIfPresent(_ value: Float?, forKey key: Key) throws {
234-
if let value = value {
234+
if let value {
235235
try encode(value, forKey: key)
236236
} else if forcingNilValueSetters {
237237
encoder.setters.append(Expression<Double?>(key.stringValue) <- nil)
238238
}
239239
}
240240

241241
func encodeIfPresent(_ value: Double?, forKey key: Key) throws {
242-
if let value = value {
242+
if let value {
243243
try encode(value, forKey: key)
244244
} else if forcingNilValueSetters {
245245
encoder.setters.append(Expression<Double?>(key.stringValue) <- nil)
246246
}
247247
}
248248

249249
func encodeIfPresent(_ value: String?, forKey key: MyKey) throws {
250-
if let value = value {
250+
if let value {
251251
try encode(value, forKey: key)
252252
} else if forcingNilValueSetters {
253253
encoder.setters.append(Expression<String?>(key.stringValue) <- nil)
@@ -270,7 +270,7 @@ private class SQLiteEncoder: Encoder {
270270
}
271271

272272
func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T: Swift.Encodable {
273-
guard let value = value else {
273+
guard let value else {
274274
guard forcingNilValueSetters else {
275275
return
276276
}

Sources/SQLite/Typed/CoreFunctions.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extension ExpressionType where UnderlyingType == Double {
100100
///
101101
/// - Returns: A copy of the expression wrapped with the `round` function.
102102
public func round(_ precision: Int? = nil) -> Expression<UnderlyingType> {
103-
guard let precision = precision else {
103+
guard let precision else {
104104
return Function.round.wrap([self])
105105
}
106106
return Function.round.wrap([self, Int(precision)])
@@ -120,7 +120,7 @@ extension ExpressionType where UnderlyingType == Double? {
120120
///
121121
/// - Returns: A copy of the expression wrapped with the `round` function.
122122
public func round(_ precision: Int? = nil) -> Expression<UnderlyingType> {
123-
guard let precision = precision else {
123+
guard let precision else {
124124
return Function.round.wrap(self)
125125
}
126126
return Function.round.wrap([self, Int(precision)])
@@ -250,7 +250,7 @@ extension ExpressionType where UnderlyingType == String {
250250
/// - Returns: A copy of the expression appended with a `LIKE` query against
251251
/// the given pattern.
252252
public func like(_ pattern: String, escape character: Character? = nil) -> Expression<Bool> {
253-
guard let character = character else {
253+
guard let character else {
254254
return "LIKE".infix(self, pattern)
255255
}
256256
return Expression("(\(template) LIKE ? ESCAPE ?)", bindings + [pattern, String(character)])
@@ -274,7 +274,7 @@ extension ExpressionType where UnderlyingType == String {
274274
/// - Returns: A copy of the expression appended with a `LIKE` query against
275275
/// the given pattern.
276276
public func like(_ pattern: Expression<String>, escape character: Character? = nil) -> Expression<Bool> {
277-
guard let character = character else {
277+
guard let character else {
278278
return Function.like.infix(self, pattern)
279279
}
280280
let like: Expression<Bool> = Function.like.infix(self, pattern, wrap: false)
@@ -349,7 +349,7 @@ extension ExpressionType where UnderlyingType == String {
349349
///
350350
/// - Returns: A copy of the expression wrapped with the `ltrim` function.
351351
public func ltrim(_ characters: Set<Character>? = nil) -> Expression<UnderlyingType> {
352-
guard let characters = characters else {
352+
guard let characters else {
353353
return Function.ltrim.wrap(self)
354354
}
355355
return Function.ltrim.wrap([self, String(characters)])
@@ -367,7 +367,7 @@ extension ExpressionType where UnderlyingType == String {
367367
///
368368
/// - Returns: A copy of the expression wrapped with the `rtrim` function.
369369
public func rtrim(_ characters: Set<Character>? = nil) -> Expression<UnderlyingType> {
370-
guard let characters = characters else {
370+
guard let characters else {
371371
return Function.rtrim.wrap(self)
372372
}
373373
return Function.rtrim.wrap([self, String(characters)])
@@ -385,7 +385,7 @@ extension ExpressionType where UnderlyingType == String {
385385
///
386386
/// - Returns: A copy of the expression wrapped with the `trim` function.
387387
public func trim(_ characters: Set<Character>? = nil) -> Expression<UnderlyingType> {
388-
guard let characters = characters else {
388+
guard let characters else {
389389
return Function.trim.wrap([self])
390390
}
391391
return Function.trim.wrap([self, String(characters)])
@@ -409,7 +409,7 @@ extension ExpressionType where UnderlyingType == String {
409409
}
410410

411411
public func substring(_ location: Int, length: Int? = nil) -> Expression<UnderlyingType> {
412-
guard let length = length else {
412+
guard let length else {
413413
return Function.substr.wrap([self, location])
414414
}
415415
return Function.substr.wrap([self, location, length])
@@ -475,7 +475,7 @@ extension ExpressionType where UnderlyingType == String? {
475475
/// - Returns: A copy of the expression appended with a `LIKE` query against
476476
/// the given pattern.
477477
public func like(_ pattern: String, escape character: Character? = nil) -> Expression<Bool?> {
478-
guard let character = character else {
478+
guard let character else {
479479
return Function.like.infix(self, pattern)
480480
}
481481
return Expression("(\(template) LIKE ? ESCAPE ?)", bindings + [pattern, String(character)])
@@ -499,7 +499,7 @@ extension ExpressionType where UnderlyingType == String? {
499499
/// - Returns: A copy of the expression appended with a `LIKE` query against
500500
/// the given pattern.
501501
public func like(_ pattern: Expression<String>, escape character: Character? = nil) -> Expression<Bool?> {
502-
guard let character = character else {
502+
guard let character else {
503503
return Function.like.infix(self, pattern)
504504
}
505505
let like: Expression<Bool> = Function.like.infix(self, pattern, wrap: false)
@@ -574,7 +574,7 @@ extension ExpressionType where UnderlyingType == String? {
574574
///
575575
/// - Returns: A copy of the expression wrapped with the `ltrim` function.
576576
public func ltrim(_ characters: Set<Character>? = nil) -> Expression<UnderlyingType> {
577-
guard let characters = characters else {
577+
guard let characters else {
578578
return Function.ltrim.wrap(self)
579579
}
580580
return Function.ltrim.wrap([self, String(characters)])
@@ -592,7 +592,7 @@ extension ExpressionType where UnderlyingType == String? {
592592
///
593593
/// - Returns: A copy of the expression wrapped with the `rtrim` function.
594594
public func rtrim(_ characters: Set<Character>? = nil) -> Expression<UnderlyingType> {
595-
guard let characters = characters else {
595+
guard let characters else {
596596
return Function.rtrim.wrap(self)
597597
}
598598
return Function.rtrim.wrap([self, String(characters)])
@@ -610,7 +610,7 @@ extension ExpressionType where UnderlyingType == String? {
610610
///
611611
/// - Returns: A copy of the expression wrapped with the `trim` function.
612612
public func trim(_ characters: Set<Character>? = nil) -> Expression<UnderlyingType> {
613-
guard let characters = characters else {
613+
guard let characters else {
614614
return Function.trim.wrap(self)
615615
}
616616
return Function.trim.wrap([self, String(characters)])
@@ -649,7 +649,7 @@ extension ExpressionType where UnderlyingType == String? {
649649
///
650650
/// - Returns: A copy of the expression wrapped with the `substr` function.
651651
public func substring(_ location: Int, length: Int? = nil) -> Expression<UnderlyingType> {
652-
guard let length = length else {
652+
guard let length else {
653653
return Function.substr.wrap([self, location])
654654
}
655655
return Function.substr.wrap([self, location, length])
@@ -726,7 +726,7 @@ extension String {
726726
/// - Returns: A copy of the expression appended with a `LIKE` query against
727727
/// the given pattern.
728728
public func like(_ pattern: Expression<String>, escape character: Character? = nil) -> Expression<Bool> {
729-
guard let character = character else {
729+
guard let character else {
730730
return Function.like.infix(self, pattern)
731731
}
732732
let like: Expression<Bool> = Function.like.infix(self, pattern, wrap: false)

0 commit comments

Comments
 (0)