Rules / Suspicious / avoid-shadowing-type-parameters

avoid-shadowing-type-parameters

lint/suspicious/avoid-shadowing-type-parameters
recommended
Same as avoid_shadowing_type_parameters · Dart lints

Flags a type parameter that shadows a type parameter of an enclosing declaration.

When a nested declaration — a method, local function, function expression, or generic function type or typedef — reuses the name of a type parameter from its surrounding class or function, the inner name hides the outer one. Code in the nested scope can no longer refer to the enclosing type, and a reader can easily mistake the two unrelated types for the same one, which invites subtle type errors. Rename the inner parameter to something distinct.

Invalid

example.dartdart
// A nested declaration's type parameter must not shadow an enclosing one.

class A<T> {
  void m<T>() {}
}

class B<T, S> {
  S f<S>(S x) => x;
}

mixin M<T> {
  void p<T>() {}
}

void g<T>() {
  void h<T>() {}
  h<int>();
}

class C<E> {
  void m() {
    void inner<E>() {}
    inner<int>();
  }
}

extension Ext<T> on List<T> {
  void each<T>() {}
}
Avoid shadowing type parameters.
3class A<T> {
4 void m<T>() {}
Avoid shadowing type parameters.
7class B<T, S> {
8 S f<S>(S x) => x;
Avoid shadowing type parameters.
11mixin M<T> {
12 void p<T>() {}
Avoid shadowing type parameters.
15void g<T>() {
16 void h<T>() {}
Avoid shadowing type parameters.
21 void m() {
22 void inner<E>() {}
Avoid shadowing type parameters.
27extension Ext<T> on List<T> {
28 void each<T>() {}

Valid

example.dartdart
// Nested type parameters use distinct names.

class A<T> {
  void m<S>() {}
}

class B<T> {
  void m() {}
}

void f<T>() {
  void g<S>() {}
  g<int>();
}

typedef Fn<T> = void Function(T value);

class C<T, S> {
  S convert(T x) => x as S;
}

void h<T>(T x) {
  print(x);
}

class D {
  void m<T>() {}
}

How to configure

Set the severity of avoid-shadowing-type-parameters in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-shadowing-type-parameters": "error"
      }
    }
  }
}