dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
633 stars 170 forks source link

unnecessary return #2836

Open asashour opened 3 years ago

asashour commented 3 years ago

Description Some return statements are redundant, and can safely be removed, if they are not followed by other statements.

This is specially true for void functions.

I am not sure if a lint exists, or should it be part of unnecessary_statements?

Examples

void m(bool x) {
  if (x) {
    doSomething();
    return; // LINT
  }
  return; // LINT
}

void m2(bool x) {
  if (x) {
    doSomething();
    return; // OK
  }
  another();
  return; // LINT
}
bwilkerson commented 3 years ago

I am not sure if a lint exists, or should it be part of unnecessary_statements?

I don't believe there is currently such a lint. Do you have an estimate for how often this occurs? I personally don't remember ever seeing it in a code review, but that doesn't prove anything.

I believe that the primary purpose of 'unnecessary_statements' is to find places that are likely to be bugs because of missing code. This kind of error doesn't really qualify as a bug. But if we think it's worth adding support for it we might consider adding it to the 'dead_code' support.

asashour commented 3 years ago

Actually, two unneeded returns are currently in master and were detected (and removed) in #2800.

https://github.com/dart-lang/linter/blob/def6bf20f22a8b974673606a942f392700efef31/lib/src/rules/unnecessary_parenthesis.dart#L127-L131

roman-petrov commented 3 years ago

I've seen a couple of times such return statements while reviewing inexperienced developers and was quite surprised, such return statements are in fact just garbage.

I don't think that this occurs very often, but such lint would be nice to have.