Skip to content

Commit 9a634bc

Browse files
authored
Retain async initializers in actors in async_without_await (#6436)
1 parent 35e3d0c commit 9a634bc

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
### Bug Fixes
1818

19+
* Retain `async` initializers in actors in `async_without_await` rule.
20+
[SimplyDanny](https://github.com/SimplyDanny)
21+
[#6423](https://github.com/realm/SwiftLint/issues/6423)
22+
1923
* Ignore `override` functions in `async_without_await` rule.
2024
[SimplyDanny](https://github.com/SimplyDanny)
2125
[#6416](https://github.com/realm/SwiftLint/issues/6416)

Source/SwiftLintBuiltInRules/Rules/Lint/AsyncWithoutAwaitRule.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private extension AsyncWithoutAwaitRule {
2323

2424
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
2525
private var functionScopes = Stack<FuncInfo>()
26+
private var actorTypeStack = Stack<Bool>()
2627
private var pendingAsync: TokenSyntax?
2728

2829
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
@@ -72,7 +73,9 @@ private extension AsyncWithoutAwaitRule {
7273

7374
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
7475
if node.body != nil {
75-
let asyncToken = node.needsToKeepAsync ? nil : node.signature.effectSpecifiers?.asyncSpecifier
76+
let asyncToken = node.needsToKeepAsync || actorTypeStack.peek() == true
77+
? nil
78+
: node.signature.effectSpecifiers?.asyncSpecifier
7679
functionScopes.push(.init(asyncToken: asyncToken))
7780
}
7881
return .visitChildren
@@ -113,6 +116,42 @@ private extension AsyncWithoutAwaitRule {
113116
}
114117
}
115118

119+
override func visit(_: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
120+
actorTypeStack.push(true)
121+
return .visitChildren
122+
}
123+
124+
override func visitPost(_: ActorDeclSyntax) {
125+
actorTypeStack.pop()
126+
}
127+
128+
override func visit(_: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
129+
actorTypeStack.push(false)
130+
return .visitChildren
131+
}
132+
133+
override func visitPost(_: ClassDeclSyntax) {
134+
actorTypeStack.pop()
135+
}
136+
137+
override func visit(_: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
138+
actorTypeStack.push(false)
139+
return .visitChildren
140+
}
141+
142+
override func visitPost(_: EnumDeclSyntax) {
143+
actorTypeStack.pop()
144+
}
145+
146+
override func visit(_: StructDeclSyntax) -> SyntaxVisitorContinueKind {
147+
actorTypeStack.push(false)
148+
return .visitChildren
149+
}
150+
151+
override func visitPost(_: StructDeclSyntax) {
152+
actorTypeStack.pop()
153+
}
154+
116155
override func visit(_: FunctionParameterSyntax) -> SyntaxVisitorContinueKind {
117156
.skipChildren
118157
}

Source/SwiftLintBuiltInRules/Rules/Lint/AsyncWithoutAwaitRuleExamples.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
internal struct AsyncWithoutAwaitRuleExamples {
22
static let nonTriggeringExamples = [
3+
Example("""
4+
actor A {
5+
init() async {
6+
foo()
7+
}
8+
func foo() {}
9+
}
10+
"""),
311
Example("""
412
func test() {
513
func test() async {

0 commit comments

Comments
 (0)