Avoid using last

When retrieving the last element of a sequence, use lastOption rather than last.

Reason

Some collections are empty, and last deals with them by throwing an exception:

Seq.empty[Int].last
// java.util.NoSuchElementException
// 	at scala.collection.LinearSeqOptimized.last(LinearSeqOptimized.scala:150)
// 	at scala.collection.LinearSeqOptimized.last$(LinearSeqOptimized.scala:149)
// 	at scala.collection.immutable.List.last(List.scala:89)
// 	at repl.Session$App$$anonfun$1.apply$mcI$sp(traversable_last.md:9)
// 	at repl.Session$App$$anonfun$1.apply(traversable_last.md:9)
// 	at repl.Session$App$$anonfun$1.apply(traversable_last.md:9)

lastOption is a safer alternative, since it encodes the possibility of the empty list in its return type:

Seq(1, 2, 3).lastOption
// res0: Option[Int] = Some(3)

Seq.empty[Int].lastOption
// res1: Option[Int] = None

Checked by

LinterRule
Scapegoat
  • TraversableLast
WartRemover