SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

`async_without_await` triggers on methods which are marked with `@MainActor`

Open Heckscheibe opened this issue 8 months ago • 1 comments
trafficstars

New Issue Checklist

Bug Description

A method which has the @MainActor annotation also has to be marked with async, otherwise it will not run on the MainActor. This does not necessarily mean, that within the method, there is a await function call.

@MainActor
func() runOnMainActor async {
print("This runs on MainActor")
}
$ swiftlint lint 

Environment

  • SwiftLint version (58.2)
  • Xcode version (16B40)
  • Installation method used ( MacOS binary )

Heckscheibe avatar Mar 06 '25 12:03 Heckscheibe

I tried

@MainActor
func f() {
    MainActor.assertIsolated("Not running on main actor")
    print("Running on main actor")
}

await f()

and it worked. Removing @MainActor and adding async to f panics. If MainActor.assertIsolated passes in the first case, we can assume the code runs on the main actor even without async.

The attribute rather seems to imply async as f needs to be called with await in any case.

What lets you assume it doesn't run on the main actor? Perhaps you can provide a little more context.

SimplyDanny avatar Mar 09 '25 18:03 SimplyDanny