Description
Creating collection literals from void
surprisingly doesn't generate any runtime errors nor analysis warnings:
void voidFunction() {}
void main() {
var list = [voidFunction()];
var set = {voidFunction()};
var f = () => {voidFunction()};
print(list); // Prints: [null]
print(set); // Prints: {null}
print(f()); // Prints: {null}
}
I'm confused why since my understanding was that void
values are not allowed to be read.
I'm not sure if this somehow is intentionally not an error, but at a minimum, I think that we could warn if creating List<void>
or Set<void>
. Those types don't seem like they'd ever be useful. Set<void>
is particularly bad since I've observed at least one person using (arg) => { doSomething() }
when they meant either (arg) => doSomething()
or (arg) { doSomething() }
. (Such a lint wouldn't help if doSomething()
returned a non-void
value, of course ,but I don't know what could be done about that other than requiring => { ... }
to be => <T>{ ... }
if a Set
is actually desired.)
I tried this with version 2.13.4 of the Dart SDK.