|
| 1 | +// |
| 2 | +// RedundantAsync.swift |
| 3 | +// SwiftFormat |
| 4 | +// |
| 5 | +// Created by Cal Stephens on 2025-09-18. |
| 6 | +// Copyright © 2024 Nick Lockwood. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +public extension FormatRule { |
| 12 | + static let redundantAsync = FormatRule( |
| 13 | + help: "Remove redundant `async` keyword from function declarations that don't contain any await expressions.", |
| 14 | + disabledByDefault: true, |
| 15 | + options: ["redundant-async"] |
| 16 | + ) { formatter in |
| 17 | + let testFramework = formatter.detectTestingFramework() |
| 18 | + if formatter.options.redundantAsync == .testsOnly, testFramework == nil { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + formatter.forEach(.keyword) { keywordIndex, keyword in |
| 23 | + guard ["func", "init", "subscript"].contains(keyword.string), |
| 24 | + let functionDecl = formatter.parseFunctionDeclaration(keywordIndex: keywordIndex), |
| 25 | + functionDecl.effects.contains(where: { $0.hasPrefix("async") }), |
| 26 | + let bodyRange = functionDecl.bodyRange |
| 27 | + else { return } |
| 28 | + |
| 29 | + // Don't modify override functions - they need to match their parent's signature |
| 30 | + if formatter.modifiersForDeclaration(at: keywordIndex, contains: "override") { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + if formatter.options.redundantAsync == .testsOnly { |
| 35 | + // Only process test functions |
| 36 | + guard let testFramework, functionDecl.returnType == nil else { return } |
| 37 | + |
| 38 | + switch testFramework { |
| 39 | + case .xcTest: |
| 40 | + guard functionDecl.name?.starts(with: "test") == true else { return } |
| 41 | + case .swiftTesting: |
| 42 | + guard formatter.modifiersForDeclaration(at: keywordIndex, contains: "@Test") else { return } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Check if the function body contains any await keywords |
| 47 | + var bodyContainsAwait = false |
| 48 | + for index in bodyRange { |
| 49 | + if formatter.tokens[index] == .keyword("await") { |
| 50 | + // Only count await keywords that are directly in this function's body |
| 51 | + // (not in nested closures or functions) |
| 52 | + if formatter.isInFunctionBody(of: functionDecl, at: index) { |
| 53 | + bodyContainsAwait = true |
| 54 | + break |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // If the body doesn't contain any await, remove the async |
| 60 | + if !bodyContainsAwait { |
| 61 | + formatter.removeEffect("async", from: functionDecl) |
| 62 | + } |
| 63 | + } |
| 64 | + } examples: { |
| 65 | + """ |
| 66 | + ```diff |
| 67 | + // With --redundant-async tests-only (default) |
| 68 | + import Testing |
| 69 | +
|
| 70 | + - @Test func myFeature() async { |
| 71 | + + @Test func myFeature() { |
| 72 | + #expect(foo == 1) |
| 73 | + } |
| 74 | +
|
| 75 | + import XCTest |
| 76 | +
|
| 77 | + class TestCase: XCTestCase { |
| 78 | + - func testMyFeature() async { |
| 79 | + + func testMyFeature() { |
| 80 | + XCTAssertEqual(foo, 1) |
| 81 | + } |
| 82 | + } |
| 83 | + ``` |
| 84 | +
|
| 85 | + Also supports `--redundant-async always`. |
| 86 | + This will cause warnings anywhere the updated method is called with `await`, since `await` is now redundant at the callsite. |
| 87 | +
|
| 88 | + ```diff |
| 89 | + // With --redundant-async always |
| 90 | + - func myNonAsyncMethod() async -> Int { |
| 91 | + + func myNonAsyncMethod() -> Int { |
| 92 | + return 0 |
| 93 | + } |
| 94 | +
|
| 95 | + // Possibly elsewhere in codebase: |
| 96 | + let value = await myNonAsyncMethod() |
| 97 | + + `- warning: no 'async' operations occur within 'await' expression |
| 98 | + ``` |
| 99 | + """ |
| 100 | + } |
| 101 | +} |
0 commit comments