dart-lang / linter

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

[Wildcard Variables][lint] consider a `no_unused_parameters` lint #4972

Open pq opened 1 month ago

pq commented 1 month ago

With wildcard variables, we have a proper way to mark unused parameters as intentionally unused. Given that, we might consider a lint that flags unused parameters with the expectation that intentionally unused params should either be removed or converted to wildcards.

For example:

BAD

int f(int x) => 42;

abstract class A {
  int f(int x);
}

class A extends C {
  @override
  int f(int x) => 42;
}

GOOD

int f(int x) => x;
int f() => 42;
int f(int _) => 42;

abstract class A {
  int f(int x);
}

class A extends C {
  @override
  int f(int x) => x;
}

class A extends C {
  @override
  int f(int _) => 42;
}

/fyi @kallentu @lrhn @munificent

lrhn commented 1 month ago

As described here, the lint would conflict with avoid_renaming_method_parameters for instance methods. That lint is there for several reasons, one being to ensure that inherited documentation stays correct.

Generally, there can be good reasons for having a name for a parameter that is not used, if the function is publicly visible. The parameter is there for a reason, its name may describe that reason. Even for private function declarations, it may be more useful to know the names of parameters that aren't used by this particular function implementation. If the parameter is there, it's probably because it means something, so if we can't remove the entire parameter, there can be reasons for the name.

Not always, it's also fine to decide that you don't care, or that you know that nobody will see this seemingly public function.

For function literals, used as a callback, it makes much more sense, because that's not also exposed as something you can call.