disposito 1.0.1
disposito: ^1.0.1 copied to clipboard
Analyzer plugin for object lifetimes: fields annotated with @Disposable must be cleaned up by the class that declares them, objects that own a lifecycle must not be created during a Flutter build, and [...]
// Run `dart pub get && dart analyze` in this directory to see the rules fire.
//
// Expected output: exactly one warning per line marked "reported" below.
import 'package:disposito/disposito.dart';
/// A resource that has to be released.
class Connection {
void close() {}
void detach() {}
}
class GoodService {
@Disposable()
final Connection _primary = Connection();
@Disposable(#detach)
final Connection _secondary = Connection();
void doSomething() {
print('$_secondary');
}
/// Both fields are cleaned up here, so neither is reported.
void dispose() {
_primary.close();
_secondary.detach();
}
}
class LeakyService {
// reported: missing_dispose, nothing in this class ever closes it.
@Disposable()
final Connection _connection = Connection();
void read() {
print(_connection);
}
}
class SuppressedService {
// Diagnostics are suppressed with the plugin name as a prefix. The rule
// reports on the field's name, so the comment goes directly above that
// line, below the annotation.
@Disposable()
// ignore: disposito/missing_dispose
final Connection _connection = Connection();
void read() {
print(_connection);
}
}