dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
630 stars 170 forks source link

avoid_function_literals_in_foreach_calls should accept expression body #3114

Open yelliver opened 2 years ago

yelliver commented 2 years ago

Describe the issue This rule https://dart-lang.github.io/linter/lints/avoid_function_literals_in_foreach_calls.html

To Reproduce Having this line of code (and got the waring violating the rule): people.forEach((person) => person.foo(param));

Expected behavior I think the above code still satisfies the principle of the rule because when using expression body, it is just 1 line

srawlins commented 2 years ago

The rule is not in place to encourage shorter code, but to encourage more performant code (I think) which doesn't involve unnecessary closures. In any case it's more idiomatic (https://dart.dev/guides/language/effective-dart/usage#avoid-using-iterableforeach-with-a-function-literal).

Of course there isn't documentation on the lint rule explaining this; I'll leave it open for now to support adding such documentation.

lrhn commented 1 year ago

(We could also lobby for the formatter/style guide to accept for (var person in people) person.foo(param); on one line, like we do for single-line ifs.)