avoid_generics_shadowing
v0.4.0 Warning Fix Code Organization
Flags generic type parameters that shadow a top-level type declaration (class, mixin, enum, typedef, or extension type) in the same file. When a type parameter has the same name as a real class, it becomes confusing whether a reference points to the generic or the concrete type.
Why use this rule
Section titled “Why use this rule”Shadowing a top-level type with a generic parameter silently replaces the concrete type with an unbounded generic within that scope. This can lead to subtle bugs where code appears to reference a specific class but actually operates on an unrelated type parameter. Using conventional single-letter names like T, R, or E eliminates the ambiguity.
See also: Dart language - Generics | Dart lint: avoid_shadowing_type_parameters
class MyModel {}enum MyEnum { first, second }
// Generic type parameter shadows the top-level class MyModelclass Repository<MyModel> { MyModel get(int id) => throw '';}
class SomeClass { // MyEnum shadows the top-level enum void method<MyEnum>(MyEnum p) {}}
// Both type parameters shadow top-level typesclass BadPair<MyModel, AnotherClass> { final MyModel first; final AnotherClass second; BadPair(this.first, this.second);}class MyModel {}enum MyEnum { first, second }
// Use conventional single-letter type parametersclass GoodRepository<T> { T get(int id) => throw '';}
// Descriptive names that don't shadow top-level typesclass GoodPair<TFirst, TSecond> { final TFirst first; final TSecond second; GoodPair(this.first, this.second);}
class Processor { void process<T>(T item) {} R transform<R>(Object input) => throw '';}Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
avoid_generics_shadowing: true.
To turn it off:
rules: avoid_generics_shadowing: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”arguments_ordering— Keep named arguments in a configured order.avoid_duplicate_mixins— Flag a mixin applied twice in onewithclause.avoid_unnecessary_constructor— Remove a constructor identical to the default one.avoid_unnecessary_extends— Remove an explicitextends Object.