linter icon indicating copy to clipboard operation
linter copied to clipboard

proposal: rule for when an argument at position i has the same name signature as a parameter at a position that is not i

Open cedvdb opened this issue 3 years ago • 1 comments

name

avoid_clashing_argument_names I don't know really.

Description

an argument at position i has the same name signature as a parameter at a position that is not i

Details

Generally speaking, positional parameters can create bug when the library they use change the order of the parameters. This rule would make catching such bugs easier.

Kind

*Guard against bug from api positional parameters change *

Good Examples

class A {
  int x;
  int y;
  A(this.x, this.y);
}
void main() {
  int x = 3;
  int y = 4;
  // A api originally had y param at first position, but the API author swapped x, y in a new release. Now this is a bug:
  final a = A(y, x);
}

Bad Examples

Here is a counter example where this is deliberate

class Pair<F, S> {
  final F first;
  final S second;
  Pair(this.first, this.second);
  // ignore: avoid_clashing_argument_names
  Pair<S, F> get swapped => Pair<S, F>(second, first);  // <- deliberate
}

Discussion

https://github.com/dart-lang/language/issues/2056#issuecomment-1016242727

cedvdb avatar Jan 19 '22 10:01 cedvdb

This would be an extremely limited lint rule if it only checked simple identifiers (x, y) against the names of mis-matched parameters. E.g. I don't think we'd go through the trouble of checking foo.x and foo.y as mismatched. I know I've hit bugs like this before, but the expense might not be worth the value.

srawlins avatar Oct 04 '22 15:10 srawlins