Skip to content

Commit 5ac3493

Browse files
authored
Add rule to always inline let keyword when destructuring enum cases and tuples (#126)
1 parent ffe5acd commit 5ac3493

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,59 @@ _You can enable the following settings in Xcode by running [this script](resourc
537537

538538
</details>
539539

540+
* <a id='inline-let-when-destructuring'></a> (<a href='#inline-let-when-destructuring'>link</a>) **When destructuring an enum case or a tuple, place the `let` keyword inline, adjacent to each individual property assignment.** [![SwiftFormat: hoistPatternLet](https://img.shields.io/badge/SwiftFormat-hoistPatternLet-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#hoistPatternLet)
541+
542+
<details>
543+
544+
```swift
545+
// WRONG
546+
switch result {
547+
case let .success(value):
548+
// ...
549+
case let .error(errorCode, errorReason):
550+
// ...
551+
}
552+
553+
// WRONG
554+
guard let case .success(value) else {
555+
return
556+
}
557+
558+
// RIGHT
559+
switch result {
560+
case .success(let value):
561+
// ...
562+
case .error(let errorCode, let errorReason):
563+
// ...
564+
}
565+
566+
// RIGHT
567+
guard case .success(let value) else {
568+
return
569+
}
570+
```
571+
572+
#### Why?
573+
574+
1. **Consistency**: We should prefer to either _always_ inline the `let` keyworkd or _never_ inline the `let` keyword. In Airbnb's Swift codebase, we [observed](https://github.com/airbnb/swift/pull/126#discussion_r631979244) that inline `let` is used far more often in practice (especially when destructuring enum cases with a single associated value).
575+
576+
2. **Clarity**: Inlining the `let` keyword makes it more clear which identifiers are part of the conditional check and which identifiers are binding new variables, since the `let` keyword is always adjacent to the variable identifier.
577+
578+
```swift
579+
// `let` is adjacent to the variable identifier, so it is immediately obvious
580+
// at a glance that these identifiers represent new variable bindings
581+
case .enumCaseWithSingleAssociatedValue(let string):
582+
case .enumCaseWithMultipleAssociatedValues(let string, let int):
583+
584+
// The `let` keyword is quite far from the variable identifiers,
585+
// so its less obvious that they represent new variable bindings
586+
case let .enumCaseWithSingleAssociatedValue(string):
587+
case let .enumCaseWithMultipleAssociatedValues(string, int):
588+
589+
```
590+
591+
</details>
592+
540593
* <a id='attributes-on-prev-line'></a>(<a href='#attributes-on-prev-line'>link</a>) **Place function/type attributes on the line above the declaration**. [![SwiftFormat: wrapAttributes](https://img.shields.io/badge/SwiftFormat-wrapAttributes-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#wrapAttributes)
541594

542595
<details>

resources/airbnb.swiftformat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
--enumthreshold 20 # organizeDeclarations
2020
--organizetypes class,struct,enum,extension # organizeDeclarations
2121
--extensionacl on-declarations # extensionAccessControl
22+
--patternlet inline # hoistPatternLet
2223
--swiftversion 5.1
2324

2425
# rules
25-
--rules anyObjectProtocol,redundantParens,redundantReturn,redundantSelf,sortedImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,organizeDeclarations,markTypes,extensionAccessControl,duplicateImports,redundantType,consecutiveSpaces
26+
--rules anyObjectProtocol,redundantParens,redundantReturn,redundantSelf,sortedImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,organizeDeclarations,markTypes,extensionAccessControl,duplicateImports,redundantType,hoistPatternLet,consecutiveSpaces

0 commit comments

Comments
 (0)