linter icon indicating copy to clipboard operation
linter copied to clipboard

avoid_function_literals_in_foreach_calls should accept expression body

Open yelliver opened this issue 3 years ago • 1 comments

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

yelliver avatar Dec 17 '21 05:12 yelliver

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.

srawlins avatar Dec 17 '21 07:12 srawlins

(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.)

lrhn avatar Oct 26 '22 08:10 lrhn