Skip to content

Commit 902bdcd

Browse files
committed
Avoid using withExtendedLifetime in unit tests
1 parent 44685a4 commit 902bdcd

3 files changed

Lines changed: 52 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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5665,6 +5665,55 @@ _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+
// Also wrong / redundant: underscored assignment is often used for the same purpose as `withExtendedLifetime`
5688+
_ = observatory
5689+
}
5690+
5691+
// RIGHT
5692+
@Test
5693+
func `telescope tracks target`() {
5694+
let observatory = Observatory()
5695+
let telescope = observatory.veryLargeTelescope()
5696+
5697+
telescope.track(.mars)
5698+
#expect(telescope.isTracking)
5699+
}
5700+
5701+
// ALSO RIGHT. Without `withExtendedLifetime`, `cancellable` would cause an "unused variable" warning.
5702+
@Test
5703+
func `telescope publishes target updates`() {
5704+
let telescope = Telescope()
5705+
var publishedTarget: Planet?
5706+
let cancellable = telescope.targetPublisher.sink { publishedTarget = $0 }
5707+
5708+
telescope.track(.mars)
5709+
#expect(publishedTarget == .mars)
5710+
5711+
withExtendedLifetime(cancellable) { }
5712+
}
5713+
```
5714+
5715+
</details>
5716+
56685717
**[⬆ back to top](#table-of-contents)**
56695718

56705719
## 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)