Skip to content

Analyzer should warn if setter contains a return with a value #25228

Closed
@munificent

Description

@munificent

Analyzer currently gives me a friendly warning in:

void set bar(value) {
  return "bad";
}
[warning] The return type 'String' is not a 'void', as defined by the method 'bar' (temp.dart, line 2, col 10)

If I annotate that to any other type, it also correctly tells me I'm doing something wrong:

String set bar(value) {
  return "bad";
}
[warning] The return type of the setter must be 'void' (temp.dart, line 1, col 1)

In fact, it won't even let me use dynamic:

dynamic set bar(value) {
  return "bad";
}
[warning] The return type of the setter must be 'void' (temp.dart, line 1, col 1)

So it, really, clearly, obvious, undoubtedly, has a void return type. Even if I want to jam a dynamic in there to shut it up, it won't let me. Given that, if I omit the type:

set bar(value) {
  return "bad";
}

I should still get a warning on the return. The type of the body is void so the return "bad"; is clearly wrong.

Right now, the looser behavior when the type is omitted means you get better static analysis if you add the "void". That encourages people to adopt a pointlessly verbose style. Can we still be smart even if the "void" is omitted so I can tell people they don't have to add it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-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