Skip to content

Rule request: [no-async-without-await] Functions marked async must contain an await or return statement #5082

@pm-dev

Description

@pm-dev

New Issue Checklist

New rule request

Disallow async functions which have no await expression.

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.
    async functions that don't suspend contradict the Swift Concurrency documentation which specifies they are able to suspend partway through execution.
    Asynchronous functions that don’t use await can cause confusion in that a programmer awaiting such a function may assume it's guaranteed to execute its work in the background. Although such an assumption is a fundamental misunderstanding of Swift Concurrency, there are plenty of examples.

Async functions without await are a common and unintentional result of refactoring.

  1. Provide several examples of what would and wouldn't trigger violations.

Pass:

func f() async {
    await fetch();
}

Fail:

func add(a: Int, b: Int) async {
    return a + b
}
  1. Should the rule be configurable, if so what parameters should be configurable?
    There may be edge cases where async without await is appropriate that I'm not thinking of.

  2. Should the rule be opt-in or enabled by default? Why?
    Enabled by default
    I believe this rule would be fast, is not prone to false positives and is general consensus.

Linters for other languages have this rule:
TS Lint
ES Lint

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionTopics that cannot be categorized as bugs or enhancements yet. They require further discussions.rule-requestRequests for a new rules.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions