Retain async initializers in actors in async_without_await (#6436)

This commit is contained in:
Danny Mösch
2026-01-14 23:01:50 +01:00
committed by GitHub
parent 35e3d0c6f4
commit 9a634bc8a0
3 changed files with 52 additions and 1 deletions
+4
View File
@@ -16,6 +16,10 @@
### Bug Fixes
* Retain `async` initializers in actors in `async_without_await` rule.
[SimplyDanny](https://github.com/SimplyDanny)
[#6423](https://github.com/realm/SwiftLint/issues/6423)
* Ignore `override` functions in `async_without_await` rule.
[SimplyDanny](https://github.com/SimplyDanny)
[#6416](https://github.com/realm/SwiftLint/issues/6416)
@@ -23,6 +23,7 @@ private extension AsyncWithoutAwaitRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
private var functionScopes = Stack<FuncInfo>()
private var actorTypeStack = Stack<Bool>()
private var pendingAsync: TokenSyntax?
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
@@ -72,7 +73,9 @@ private extension AsyncWithoutAwaitRule {
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
if node.body != nil {
let asyncToken = node.needsToKeepAsync ? nil : node.signature.effectSpecifiers?.asyncSpecifier
let asyncToken = node.needsToKeepAsync || actorTypeStack.peek() == true
? nil
: node.signature.effectSpecifiers?.asyncSpecifier
functionScopes.push(.init(asyncToken: asyncToken))
}
return .visitChildren
@@ -113,6 +116,42 @@ private extension AsyncWithoutAwaitRule {
}
}
override func visit(_: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
actorTypeStack.push(true)
return .visitChildren
}
override func visitPost(_: ActorDeclSyntax) {
actorTypeStack.pop()
}
override func visit(_: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
actorTypeStack.push(false)
return .visitChildren
}
override func visitPost(_: ClassDeclSyntax) {
actorTypeStack.pop()
}
override func visit(_: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
actorTypeStack.push(false)
return .visitChildren
}
override func visitPost(_: EnumDeclSyntax) {
actorTypeStack.pop()
}
override func visit(_: StructDeclSyntax) -> SyntaxVisitorContinueKind {
actorTypeStack.push(false)
return .visitChildren
}
override func visitPost(_: StructDeclSyntax) {
actorTypeStack.pop()
}
override func visit(_: FunctionParameterSyntax) -> SyntaxVisitorContinueKind {
.skipChildren
}
@@ -1,5 +1,13 @@
internal struct AsyncWithoutAwaitRuleExamples {
static let nonTriggeringExamples = [
Example("""
actor A {
init() async {
foo()
}
func foo() {}
}
"""),
Example("""
func test() {
func test() async {