Skip to content

Warn went creating Set<void> #58456

Open
Open
@jamesderlin

Description

@jamesderlin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.linter-lint-requesttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions