2

Futures are executed in my code and are not being detected.

def f(): Future[String] = {
  functionReturningFuture() // How to detect this?
  Future("")
}

Ideally, a static analysis tool would help detect this.

Todd
  • 43
  • 1
  • 7
  • https://stackoverflow.com/questions/13415307/suppress-discarded-non-unit-value-warning – Viktor Klang Apr 23 '19 at 07:56
  • @ViktorKlang as far as I remember `-Ywarn-value-discard` doesn't work if you return (without discarding) some non-`Unit` value, e.g. another `Future` as in example? – Mateusz Kubuszok Apr 23 '19 at 10:03

1 Answers1

3

The closer you can get is NonUnitStatements wart from WartRemover, but it cannot error only Future statements and skip all the others.

The fact that you have such issue could be used as an argument against using Future and replacing it with some IO: Cats' IO, Monix's Task or Scalaz ZIO. When it comes to them, you build your pipeline first, and the you run it. If you omitted IO value in return and you didn't compose it into the result in any other way (flatMap, map2, for comprehension etc) it would not get executed - it would still be there but it would cause no harm.

If you wanted to have greater control and error only on Future, you would probably have to write your own WartRemover's wart or ScalaFix rule.

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64