New rule: prefer_iterable_as_function_parameter #57727
Labels
area-devexp
For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.
devexp-linter
Issues with the analyzer's support for the linter package
linter-lint-request
P4
type-enhancement
A request for a change that isn't a bug
Skip to TL;DR to skip the premise.
Here's some Dart 2 fun:
The fun, of course, is that there is no static analysis warning for this code, but it might fail at runtime, because an Iterable was passed where a List was expected. Because we do fun implicit casts (#31410), its allowed statically. If, however, we add something like
--no-implicit-cast-in-function-arguments
, this code above would not be allowed.I think, in general, authors of Dart code write too many methods which expect a List as a parameter where an Iterable would work fine. Cleaning up method signatures to expect Iterable arguments will help understand and prevent implicit casts.
TL;DR
I want a lint rule that examines functions with
List
(orQueue
orSet
...) parameters, and reports when such a parameter should be anIterable
instead.Examples
Neither
fn1
norfn2
use any List-specific APIs. Nor islist
passed to other functions, which might expectList
arguments. Their signatures should be more lenient, and acceptIterable<String>
.Here,
fn3
does passlist
to other functions, but their signatures expectIterable
ordynamic
, so we're all good there.To be conservative against false positives, we can report on more restrictive situations than this... like if its expensive/tricky to see signatures of all methods where
list
is passed.Risk of False Positives
I'm not sure... maybe people write APIs that spell out "I expect a List!" because of performance stuff, like... "I call
.length
all the time on this thing, so don't pass an Iterable which has an expensive.length
call" (I'm not sure if that's true, just an example).CC @alorenzen @matanlurey
The text was updated successfully, but these errors were encountered: