Open goderbauer opened 1 year ago
This lint likly needs a different name, since a lint by the very same name already exists: https://dart-lang.github.io/linter/lints/unnecessary_late.html. It catches different cases of unnecessary late
s, though.
I like the guard as an improvement over an exception thrown at runtime.
The lint should probably also fire in the most trivial case:
int foo() {
late int bar = 0; // unnecessary late
// ...
return bar;
}
unnecessary_late
Description
If a variable is assigned in all code branches before use, the late keyword is unnecessary.
Details
The analyzer can determine statically whether a
final
local variable is assigned in all code paths before it is accessed. For that reason, it is not necessary to mark the variable aslate
.Kind
Guard against errors: if during a refactoring you accidentally forget to assign
foo
in one of the branches, the analyzer will tell you instead of crashing at runtime.Bad Examples
Good Examples
Discussion
n/a
Discussion checklist