Skip to content

Linter rule to prevent returning futures from the try block #62555

@DaniilAlpha

Description

@DaniilAlpha

There have been multiple times when I’ve accidentally written code like this:

Future<Foo> doSomethingUsefulAndGetFoo() async {
  try {
    /* do something useful */
    return getFooButMayFail(); // wrong!
  } on GettingFooFailedException {
    /* do something with the exception */
    rethrow;
  }
}

While the right code would look like so:

Future<Foo> doSomethingUsefulAndGetFoo() async {
  try {
    /* do something useful */
    return await getFooButMayFail();
  } on GettingFooFailedException {
    /* do something with the exception */
    rethrow;
  }
}

The problem can be somewhat hard to spot: we are returning a Future<Foo> from the function instead of awaiting it and returning the result. In the first case, the exception does not reach the catch block, causing it to be thrown outside without the intended handling. Even when the goal is only to catch and not rethrow, this remains an issue—and I can’t recall ever writing this intentionally.

I thought it would be great if there were a linter rule for this, but since there isn’t, I’m submitting this feature request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4area-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-lint-proposaltype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions