avoid-undisposed-instances
- Group
- Correctness
- Path
lint/correctness/avoid-undisposed-instances- Default
- On (recommended)
- Origin
- Falcon
- Domain
- flutter
- Rule
- 153 of 512
Warns when a disposable constructor result is discarded.
Only resolved constructors with an instance dispose method are checked. Passing a value as a call or constructor argument may transfer ownership: without a callee ownership contract, reporting it would be speculative. Reading a member or interpolating an instance does not transfer the instance. Assignments, variable initializers, returns, and immediately disposed expressions are also ignored. This conservative rule checks temporaries only; it does not prove that retained locals are eventually disposed. Owned fields belong to dispose-fields, dispose-focus-node-and-timer, or proper-controller-dispose. Subscription and sink release obligations remain with cancel-subscriptions and close-sinks.
Invalid
import 'package:flutter/material.dart';
class Resource { void dispose() {} }
void bad() {
TextEditingController();
ScrollController();
FocusNode();
Resource();
new Resource();
}
void consume(Object value) {}
void discardedInsideCallback() {
consume(() {
Resource();
});
}
void discardedArgumentReceivers() {
print(ScrollController().initialScrollOffset);
consume(FocusNode().hasFocus);
Text('${Resource()}');
}void bad() { TextEditingController(); TextEditingController(); ScrollController(); ScrollController(); FocusNode(); FocusNode(); Resource(); Resource(); new Resource(); consume(() { Resource();void discardedArgumentReceivers() { print(ScrollController().initialScrollOffset); print(ScrollController().initialScrollOffset); consume(FocusNode().hasFocus); consume(FocusNode().hasFocus); Text('${Resource()}');Valid
import 'package:flutter/material.dart';
class Resource {
void dispose() {}
static Resource shared() => Resource();
}
class Owner { final resource = Resource(); }
Resource create() => Resource();
Resource createBlock() { return Resource(); }
void consume(Object value) {}
void good() {
final a = TextEditingController();
var b = Resource();
b = Resource();
Resource()..dispose();
consume('not disposable');
a.dispose();
b.dispose();
}
class InitializedOwner {
final Resource resource;
InitializedOwner() : resource = Resource();
}
void wrappers(bool flag, Resource? borrowed) {
final either = flag ? Resource() : Resource();
final effective = borrowed ?? Resource();
final nodes = [FocusNode(), FocusNode()];
Resource().dispose();
Resource.shared();
}
void shadowedConstructor() {
final FocusNode = () => Object();
consume(FocusNode());
}
void transferredArguments(BuildContext context) {
Navigator.push(context, MaterialPageRoute<void>(builder: (_) => const SizedBox()));
consume(TextEditingController());
consume(ScrollController());
consume(FocusNode());
consume(Resource());
consume(new Resource());
consume((Resource()));
consume(Resource()..toString());
final resource = Resource();
consume(resource);
final sink = HiveLogSink();
LoggerService._(sink: sink);
LoggerService._(sink: HiveLogSink());
}
class HiveLogSink {
void dispose() {}
}
class LoggerService {
final HiveLogSink sink;
LoggerService._({required this.sink});
}Configure avoid-undisposed-instances
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-undisposed-instances": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-undisposed-instances: <reason>More correctness rules
- avoid-unmodified-loop-conditionrecommended
Flags loop conditions whose local scalar inputs are never modified by the loop.
- avoid-unnecessary-setstaterecommendedflutter
Avoid setState during build, initState, or didUpdateWidget.
- avoid-unnecessary-stateful-widgetsrecommendedflutter
Warns when a
StatefulWidget's local State contains only a pure build method. - avoid-unreachable-for-loop
Reports iteration of a collection in a branch that requires it to be empty.
- avoid-unrelated-type-castsrecommended
Flags casts between types proven unrelated by canonical type resolution.
- avoid-unsafe-collection-methods
Flags potentially throwing element access on SDK collections.
- avoid-unsafe-reduce
Warn when SDK Iterable.reduce can receive an empty collection.
- avoid-unused-after-null-check
Reports a null-checked local ignored by a branch that passes another local of the same resolved type to a call.