avoid-shadowing-type-parameters
Same as avoid_shadowing_type_parameters in Dart lints
- Group
- Suspicious
- Path
lint/suspicious/avoid-shadowing-type-parameters- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 138 of 512
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
// 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>();
}
}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>() {} void m() { void inner<E>() {}extension Ext<T> on List<T> { void each<T>() {} static void method<S>() { void local<T>() {} static void repeated<T>() { void local<T>() {} static void repeated<T>() { void local<T>() {}Valid
// 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.
{
"linter": {
"rules": {
"suspicious": {
"avoid-shadowing-type-parameters": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/avoid-shadowing-type-parameters: <reason>More suspicious rules
- avoid-suspicious-super-overrides
Finds getters hiding fields supplied via the immediate super constructor's
this.formals or declaring primary-constructor parameters. - avoid-throw-in-catch-block
avoid-throw-in-catch-blockFlags
throwstatements inside acatchblock. - avoid-top-level-members-in-teststest
Requires public declarations in test files, except main, to be private.
- avoid-types-as-parameter-names
avoid_types_as_parameter_namesrecommendedFlags implicit formal parameters, catch variables, and type parameters that shadow a type in their enclosing scope.
- avoid-unrelated-type-assertions
avoid-unrelated-type-assertionsrecommendedFlags an
ischeck proven impossible by canonical resolved types. - banned-usage
Ban resolved API references, not identifiers that merely share their spelling.
- cascade-after-null-coalescing
Flags a cascade after an unparenthesized null-coalescing expression.
- control-flow-in-finally
control_flow_in_finallyrecommendedFlags
return,break, orcontinuethat escapes afinallyblock.