[analyzer] Potential quick fix for incomplete function literal #46972
Labels
analyzer-quick-fix
analyzer-server
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
P3
A lower priority bug or feature request
type-enhancement
A request for a change that isn't a bug
Inspired by the discussion here, I noted that there is little help in IntelliJ if we're writing an actual argument to a function
f
which is a function literal, and the given function literal has the wrong type because some formal parameters are missing:I think it could be helpful if this situation could be detected:
e
occurs in a position where it represents a data flow with a context typeT
; this could be an actual parameter, an initializing expression in a variable declaration, a returned expression, etc.e
can be parsed as a function literal (if possible, we may even allow for syntax errors in the body, but the formal parameters must parse, and they cannot have any compile-time errors, e.g., if there are any types then they must exist and be well-bounded). If we cannot determine the return type of the function literal then we usedynamic
(we might even usedynamic
always, because this feature is not going to touch the function body anyway).e
is not assignable toT
.[]
element can be added to the parameter list in the function literal if there are no optional positional and no named parameters.{}
element in the function literal parameter list, or a new one can be introduced if there are no optional positional parameters.The names of newly added non-named parameters would be fresh. E.g., they could be named
'pN'
whereN
is a numeral that makes the name fresh in the given scope), and they could also be taken from the declaration of the context type (e.g., the relevant parameter type could be written as the type of the parameterg
invoid f(void Function(int height, int width) g)
, where the type itself contains a meaningful choice of names, possibly extended with a numeral if that name is in scope already.I think this could be helpful in a significant number of situations that actually occur. It would allow developers to write just some parameters if they don't actually care about the rest, and it allows developers to get those parameter declarations for free in a situation where it might be inconvenient to obtain them in other ways.
The text was updated successfully, but these errors were encountered: