Files
sourcekit-lsp/Sources/SwiftExtensions/RunWithCleanup.swift
Alex Hoppen 4c957506e8 Add a code action to remove unused imports in a source file
The idea is pretty simple: When `MemberImportVisibility` is enabled, we know that imports can only affect the current source file. So, we can just try and remove every single `import` declaration in the file, check if a new error occurred and if not, we can safely remove it.
2025-10-22 23:56:09 +02:00

31 lines
1006 B
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6.4)
#warning("Remove this in favor of SE-0493 (Support async calls in defer bodies) when possible")
#endif
/// Run `body` and always ensure that `cleanup` gets run, independently of whether `body` threw an error or returned a
/// value.
package func run<T>(
_ body: () async throws -> T,
cleanup: () async -> Void
) async throws -> T {
do {
let result = try await body()
await cleanup()
return result
} catch {
await cleanup()
throw error
}
}