Skip to content

report a warning for returning a value within a forEach() callback #57143

@DartBot

Description

@DartBot

This issue was originally filed by [email protected]


The following code is wrong, but looks right. No warning is reported at compile time and no error happens at runtime:

String findFirstError(Map m) {
  m.forEach((k,v) {
    if (!validate(k,v)) {
      return "found a problem with ${k}";
    }
  });
  return "ok";
}

By analogy with regular for loops this looks like it should return early when it finds the first error, but actually the return value from the callback is ignored, so it always returns "ok".

It seems like we should be able to infer that the callback's return type is void, and therefore any return statements within the callback should not return a value.

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-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