Skip to content

unawaited_futures linter rule does not trigger in switch statements #60006

@mrudigier

Description

@mrudigier

We are using the flutter_lint package in our project. Recently we noticed, that, when we have mixed Future and sync return values in a switch statement, dart "sets" the return value as sync. However, the futures are then not marked as "unawaited" by the linter rule, as expected.

  • Turn on the unawaited_futures rule
  • Create a Method with a Future return type, which returns the result of a switch statement (=> notation).
  • Make it so, that at least one case points to a Future, and one not.

Expected results

It is expected, that the Future in the switch statement triggers the unawaited_futures linter rule. I guess it is implicitly unawaited by the switch. This is a potential bug, and the linter rule can help to identify it.

Actual results

The linter does not mark the future as unawaited.

I have posted this as a flutter issue first ( flutter/flutter#161752 ), but it is probably better situated here.

Sample code to illustrate the issue:

class Test {
  void doSomethingSync() {}

  Future<void> doSomethingAsync() async {
    Future<void>.value(); // linter rule unawaited_futures is triggered by this
  }

  void onlySyncSwitch(TestOption option) {
    final result = switch (option) {
      TestOption.optionA => doSomethingSync(),
      TestOption.optionB => doSomethingSync(),
    };
    return result; // result is void
  }

  Future<void> onlyAsyncSwitch(TestOption option) {
    final result = switch (option) {
      TestOption.optionA => doSomethingAsync(),
      TestOption.optionB => doSomethingAsync(),
    };
    return result; // result is Future<void>
  }

  Future<void> mixedSyncAndAsyncSwitch(TestOption option) async {
    final result = switch (option) {
      TestOption.optionA => doSomethingSync(),
      TestOption.optionB =>
        doSomethingAsync(), // expect that unawaited_futures triggers, but it does not.
    };
    return result; // result is void
  }
}

enum TestOption {
  optionA,
  optionB,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-false-negativeIssues related to lint rules that fail to report a problem.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions