avoid-shadowing-type-parameters

Same as avoid_shadowing_type_parameters in Dart lints

Path
lint/suspicious/avoid-shadowing-type-parameters
Default
On (recommended)
Origin
Dart lints
Rule
138 of 512
recommended

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>() {}
}

class StaticAncestors<T> {
  static void method<S>() {
    void local<T>() {}
    local<int>();
  }
  static void repeated<T>() {
    void local<T>() {}
    local<int>();
  }
}
The type parameter `T` shadows a type parameter from the enclosing class.
3class A<T> {
4 void m<T>() {}
The type parameter `S` shadows a type parameter from the enclosing class.
7class B<T, S> {
8 S f<S>(S x) => x;
The type parameter `T` shadows a type parameter from the enclosing mixin.
11mixin M<T> {
12 void p<T>() {}
The type parameter `T` shadows a type parameter from the enclosing function.
15void g<T>() {
16 void h<T>() {}
The type parameter `E` shadows a type parameter from the enclosing class.
21 void m() {
22 void inner<E>() {}
The type parameter `T` shadows a type parameter from the enclosing extension.
27extension Ext<T> on List<T> {
28 void each<T>() {}
The type parameter `T` shadows a type parameter from the enclosing class.
32 static void method<S>() {
33 void local<T>() {}
The type parameter `T` shadows a type parameter from the enclosing class.
36 static void repeated<T>() {
37 void local<T>() {}
The type parameter `T` shadows a type parameter from the enclosing method.
36 static void repeated<T>() {
37 void local<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>() {}
}

class StaticClass<T> {
  static void method<T>() {}
}

mixin StaticMixin<T> {
  static void method<T>() {}
}

enum StaticEnum<T> {
  value<int>();
  static void method<T>() {}
}

extension StaticExtension<T> on List<T> {
  static void method<T>() {}
}

extension type StaticRepresentation<T>(List<T> values) {
  static void method<T>() {}
}

Configure avoid-shadowing-type-parameters

Set its severity in falcon.json. See the configuration guide.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/suspicious/avoid-shadowing-type-parameters: <reason>