A common problem in our Scala code is that a call that returns a Future[T]
will not be awaited (we commonly use the scala-async library because many of us are familiar with async/await from C#) or used at all; it is simply discarded. Is there any way to get a warning or is there a 3rd-party code analysis tool that will catch this?
Obviously this applies primarily to "side effecting" Futures, such as storing an object in a database. I realize this could be solved by using a more purely-functional approach such as the IO Monad, but let's assume we're dead set on using the built-in Future for the sake of argument.
This becomes harmful when (for instance), it is critical that an operation complete successfully before proceeding. A common example for us would be receiving a message from a message broker, storing it to a database, then only when that operation is complete do we commit the receipt of the message to the broker. Often it's just too easy to carelessly say:
storeObject()
commitToBroker()
// instead of...
await(storeObject())
await(commitToBroker())
// or...
storeObject().flatMap(_ => commitToBroker())
We've taken to call this issue a "dangling Future" and we have the policy that at the very least any Future should include an andThen()
that logs any error. However we have no automated way to catch violations of this policy, and I am not aware of a warning flag that would work.