linter
linter copied to clipboard
Add a new lint to catch dead code in try catch around syncronous code working with futures
Describe the rule you'd like to see added and to what rule set I have a function like this in a library wrapper class:
/// Read from a characteristic
///
/// Throws [MyException] on error.
Future<List<int>> readCharacteristic(
QualifiedCharacteristic characteristic,
) {
try {
return _impl.readCharacteristic(characteristic);
} on ImplementationException catch (e, stacktrace) {
throw MyException();
}
}
This does not work as intended, because the function is not marked async
. Instead of catching the error from the implementation, it returns the Future.error
value containing an ImplementationException
.
The correct implementation would look like this:
/// Read from a characteristic
///
/// Throws [MyException] on error.
Future<List<int>> readCharacteristic(
QualifiedCharacteristic characteristic,
) async {
try {
return await _impl.readCharacteristic(characteristic);
} on ImplementationException catch (e, stacktrace) {
throw MyException();
}
}
Additional context I would like some sort of lint message for the function, as it contains dead code in the try catch.
I'm not sure if this can be generalised for all functions. Dart can't really know if I wanted to catch a synchronous or async error with the try catch. Maybe having a lint that forces all functions returning Futures to be async
would help? I would prefer if there were no syncronous functions operating on futures at all in the project to avoid issues like this.