Skip to content

Commit 11979bf

Browse files
committed
Avoid using withExtendedLifetime in unit tests
1 parent 44685a4 commit 11979bf

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ let package = Package(
4747

4848
.binaryTarget(
4949
name: "swiftformat",
50-
url: "https://github.com/calda/SwiftFormat-nightly/releases/download/2026-08-21-b/SwiftFormat.artifactbundle.zip",
51-
checksum: "a14bac018f3816573ab68ab91923f8697f3266cda2e27c0c5dd6b40dc6f80dee"
50+
url: "https://github.com/calda/SwiftFormat-nightly/releases/download/2026-09-01-b/SwiftFormat.artifactbundle.zip",
51+
checksum: "2747e869d98e8d90db4c8b0612144134cf289d7e817bbe87f7fd64964d714d6a"
5252
),
5353

5454
.binaryTarget(

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5665,6 +5665,52 @@ _You can enable the following settings in Xcode by running [this script](https:/
56655665

56665666
</details>
56675667

5668+
- <a id='avoid-extended-lifetime-in-tests'></a>(<a href='#avoid-extended-lifetime-in-tests'>link</a>) **Avoid using `withExtendedLifetime` in unit tests**. It usually has no effect on runtime behavior, since a variable declared in a function scope is never deallocated before the end of that scope. `withExtendedLifetime` is permitted in cases where the variable would otherwise cause an "unused variable" warning.
5669+
5670+
<details>
5671+
5672+
[![SwiftFormat: redundantExtendedLifetime](https://img.shields.io/badge/SwiftFormat-redundantExtendedLifetime-7B0051.svg)](https://swiftformat.info/rules/prerelease#redundantExtendedLifetime)
5673+
5674+
```swift
5675+
// WRONG
5676+
@Test
5677+
func `telescope tracks target`() {
5678+
let observatory = Observatory()
5679+
let telescope = observatory.veryLargeTelescope()
5680+
5681+
telescope.track(.mars)
5682+
#expect(telescope.isTracking)
5683+
5684+
// Ensure the observatory isn't deallocated before we finish our observation
5685+
withExtendedLifetime(observatory) { }
5686+
}
5687+
5688+
// RIGHT
5689+
@Test
5690+
func `telescope tracks target`() {
5691+
let observatory = Observatory()
5692+
let telescope = observatory.veryLargeTelescope()
5693+
5694+
telescope.track(.mars)
5695+
#expect(telescope.isTracking)
5696+
}
5697+
5698+
// ALSO RIGHT. Without `withExtendedLifetime`, `cancellable` would cause an "unused variable" warning.
5699+
@Test
5700+
func `telescope publishes target updates`() {
5701+
let telescope = Telescope()
5702+
var publishedTarget: Planet?
5703+
let cancellable = telescope.targetPublisher.sink { publishedTarget = $0 }
5704+
5705+
telescope.track(.mars)
5706+
#expect(publishedTarget == .mars)
5707+
5708+
withExtendedLifetime(cancellable) { }
5709+
}
5710+
```
5711+
5712+
</details>
5713+
56685714
**[⬆ back to top](#table-of-contents)**
56695715

56705716
## Performance

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
--rules noForceUnwrapInTests
154154
--rules redundantThrows
155155
--rules redundantAsync
156+
--rules redundantExtendedLifetime
156157
--rules noGuardInTests
157158
--rules testSuiteAccessControl
158159
--rules validateTestCases

0 commit comments

Comments
 (0)