lints
lints copied to clipboard
Consider `omit_local_variable_types` for inclusion
The Dart style guide has long recommended omit_local_variable_types
. This however is somewhat in conflict with Flutter samples and style.
We should consider the path forward here, and if this can be included in recommended lints.
How about considering a variant of omit_local_variable_types
that only flags local variables with a declared type when the variable has an initializing expression whose type is trivial?
There is no need to work on perfecting the notion of 'trivial' here, we just need to recognize some specific cases, e.g.:
- A non-collection literal (
var x = 1; var y = true; var z = #foo;
). - A collection literal with explicit type arguments (
var x = <int>[17, 18]; var y = <String, String>{};
). - A non-empty collection literal whose elements have the same trivial type (
var xs = [1, 2];
but notvar ys = [1, 1.5];
). - An instance creation, and if generic: with explicit type arguments (
var x = C<int>();
).
Surely we could add more elements to this list, but again: It is not required that we get a perfect list of trivial types, we just want a useful one.
For a local variable with any other initializing expression a declared type would be tolerated (not required), we just want to get rid of the ones that are actually useless, and that is not true for some of the cases that are currently flagged by omit_local_variable_types
(cf. https://github.com/dart-lang/linter/issues/1586).
Currently, omit_local_variable_types
is too strict and removes too many useful type information whose omission makes it harder to read the code and requires more context switching to collect type information from various parts of the code base to understand what's going on.
A scaled back version of that lint that only removes obvious types is an interesting idea, though...
See also https://github.com/dart-lang/linter/issues/2591 for discussion along the lines of @eernstg's proposal above.