Skip to main content

avoid-unused-generics

added in: 1.4.0
⚙️🛠
Pro+
preset: recommended

Warns when a function or method declares unused generic types.

Unused generics are usually a sign of a bug or can appear after code refactoring.

⚙️ Config

Set ignore-overridden (default is false) to ignore methods that have the @override annotation (example).

analysis_options.yaml
dart_code_metrics:
rules:
- avoid-unused-generics:
ignore-overridden: false

Example

❌ Bad:

class SomeClass {
// LINT: This generic type is not used. Try removing it or referencing it in code.
String unused<T extends num>() {
// ...
}

// LINT: This generic type is not used. Try removing it or referencing it in code.
String multiple<T, U>(U param) {
// ...
}
}

// LINT: This generic type is not used. Try removing it or referencing it in code.
void function<T>() {
return;
}

✅ Good:

class SomeClass {
String used<T extends num>() { // Correct, 'T' is used
return (value as T).toString();
}

T multiple<T, U>(U param) { // Correct, both generics are used
// ...
}
}

void function() {
return;
}

Example with "ignore-overridden"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-unused-generics:
ignore-overridden: true

✅ Good:

abstract class Abstract {
void fn<T>();
}

class Child extends Abstract {

void fn<T>() { // Correct, overridden declaration
return;
}
}

Additional Resources