avoid-unused-instances
preset: recommended
Warns when a newly created object is not being used.
Unused newly created objects are usually a sign of a bug and are expected to be returned, assigned or thrown.
⚙️ Config
Set ignored-instances
(default is [Timer
, Debounce
, Catcher
]) to ignore instances that are intended to be unused (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-unused-instances:
ignored-instances:
- Timer
- SomeOtherClass
Example
❌ Bad:
class SomeClass {
...
}
SomeClass function() {
if (someCondition) {
SomeClass(); // LINT: This instance is not used. Try removing it or referencing it in code.
}
return SomeClass();
}
✅ Good:
SomeClass function() {
if (someCondition) {
return SomeClass();
}
return SomeClass();
}
Example with "ignored-instances"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-unused-instances:
ignored-instances:
- Timer
✅ Good:
SomeClass function() {
if (someCondition) {
Timer(); // Correct, ignored
}
return SomeClass();
}