Skip to content

Commit

Permalink
Add WartRemover
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydmeta committed Dec 16, 2016
1 parent 4e3234b commit 9c8d3d5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 46 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ lazy val compilerSettings = Seq(
case Some((2, 11)) => base ++ Seq("-deprecation:false", "-Ywarn-unused-import")
case _ => base
}
}
},
wartremoverErrors in (Compile, compile) ++= Warts.unsafe.filterNot(_ == Wart.DefaultArguments)
)

lazy val scoverageSettings = Seq(
Expand Down
34 changes: 18 additions & 16 deletions enumeratum-core/src/main/scala/enumeratum/Enum.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package enumeratum

import scala.language.experimental.macros
import scala.language.postfixOps

import scala.collection.immutable._

Expand Down Expand Up @@ -39,19 +38,19 @@ trait Enum[A <: EnumEntry] {
* Map of [[A]] object names to [[A]]s
*/
lazy final val namesToValuesMap: Map[String, A] =
values map (v => v.entryName -> v) toMap
values.map(v => v.entryName -> v).toMap

/**
* Map of [[A]] object names in lower case to [[A]]s for case-insensitive comparison
*/
lazy final val lowerCaseNamesToValuesMap: Map[String, A] =
values map (v => v.entryName.toLowerCase -> v) toMap
values.map(v => v.entryName.toLowerCase -> v).toMap

/**
* Map of [[A]] object names in upper case to [[A]]s for case-insensitive comparison
*/
lazy final val upperCaseNameValuesToMap: Map[String, A] =
values map (v => v.entryName.toUpperCase -> v) toMap
values.map(v => v.entryName.toUpperCase -> v).toMap

/**
* Map of [[A]] to their index in the values sequence.
Expand All @@ -77,14 +76,14 @@ trait Enum[A <: EnumEntry] {
* Like [[Enumeration]]'s `withName`, this method will throw if the name does not match any of the values'
* .entryName values.
*/
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def withName(name: String): A =
withNameOption(name) getOrElse
(throw new NoSuchElementException(buildNotFoundMessage(name)))
withNameOption(name).getOrElse(throw new NoSuchElementException(buildNotFoundMessage(name)))

/**
* Optionally returns an [[A]] for a given name.
*/
def withNameOption(name: String): Option[A] = namesToValuesMap get name
def withNameOption(name: String): Option[A] = namesToValuesMap.get(name)

/**
* Tries to get an [[A]] by the supplied name. The name corresponds to the .name
Expand All @@ -93,9 +92,10 @@ trait Enum[A <: EnumEntry] {
* Like [[Enumeration]]'s `withName`, this method will throw if the name does not match any of the values'
* .entryName values.
*/
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def withNameInsensitive(name: String): A =
withNameInsensitiveOption(name) getOrElse
(throw new NoSuchElementException(buildNotFoundMessage(name)))
withNameInsensitiveOption(name).getOrElse(
throw new NoSuchElementException(buildNotFoundMessage(name)))

/**
* Tries to get an [[A]] by the supplied name. The name corresponds to the .name
Expand All @@ -104,9 +104,10 @@ trait Enum[A <: EnumEntry] {
* Like [[Enumeration]]'s `withName`, this method will throw if the name does not match any of the values'
* .entryName values.
*/
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def withNameUppercaseOnly(name: String): A =
withNameUppercaseOnlyOption(name) getOrElse
(throw new NoSuchElementException(buildNotFoundMessage(name)))
withNameUppercaseOnlyOption(name).getOrElse(
throw new NoSuchElementException(buildNotFoundMessage(name)))

/**
* Tries to get an [[A]] by the supplied name. The name corresponds to the .name
Expand All @@ -115,27 +116,28 @@ trait Enum[A <: EnumEntry] {
* Like [[Enumeration]]'s `withName`, this method will throw if the name does not match any of the values'
* .entryName values.
*/
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def withNameLowercaseOnly(name: String): A =
withNameLowercaseOnlyOption(name) getOrElse
(throw new NoSuchElementException(buildNotFoundMessage(name)))
withNameLowercaseOnlyOption(name).getOrElse(
throw new NoSuchElementException(buildNotFoundMessage(name)))

/**
* Optionally returns an [[A]] for a given name, disregarding case
*/
def withNameInsensitiveOption(name: String): Option[A] =
lowerCaseNamesToValuesMap get name.toLowerCase
lowerCaseNamesToValuesMap.get(name.toLowerCase)

/**
* Optionally returns an [[A]] for a given name assuming the value is upper case
*/
def withNameUppercaseOnlyOption(name: String): Option[A] =
upperCaseNameValuesToMap get name
upperCaseNameValuesToMap.get(name)

/**
* Optionally returns an [[A]] for a given name assuming the value is lower case
*/
def withNameLowercaseOnlyOption(name: String): Option[A] =
lowerCaseNamesToValuesMap get name
lowerCaseNamesToValuesMap.get(name)

/**
* Returns the index number of the member passed in the values picked up by this enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sealed trait ValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] {
* Like [[Enumeration]]'s `withValue`, this method will throw if the value does not match any of the values'
* `.value` values.
*/
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
def withValue(i: ValueType): EntryType =
withValueOpt(i).getOrElse(throw new NoSuchElementException(buildNotFoundMessage(i)))

Expand Down
15 changes: 6 additions & 9 deletions enumeratum-play/src/main/scala/enumeratum/values/Forms.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ object Forms {
* Taken from Play 2.4.x implementation
*/
private[values] val charFormatter: Formatter[Char] = new Formatter[Char] {
def bind(key: String, data: Map[String, String]) =
data
.get(key)
.filter(s => s.length == 1 && s != " ")
.map(s => Right(s.charAt(0)))
.getOrElse(
Left(Seq(FormError(key, "error.required", Nil)))
)
def unbind(key: String, value: Char) = Map(key -> value.toString)
def bind(key: String, data: Map[String, String]): Either[Seq[FormError], Char] = {
val maybeChar: Option[Either[Seq[FormError], Char]] =
data.get(key).filter(s => s.length == 1 && s != " ").map(s => Right(s.charAt(0)))
maybeChar.getOrElse(Left(Seq(FormError(key, "error.required", Nil))))
}
def unbind(key: String, value: Char): Map[String, String] = Map(key -> value.toString)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import reactivemongo.bson._
* @author Alessandro Lacava (@lambdista)
* @since 2016-04-23
*/
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
object EnumHandler {

/**
Expand Down Expand Up @@ -95,8 +96,8 @@ object EnumHandler {
insensitive: Boolean = false
): BSONHandler[BSONValue, A] =
new BSONHandler[BSONValue, A] {
val concreteReader = reader(enum, insensitive)
val concreteWriter = writer(enum)
private val concreteReader = reader(enum, insensitive)
private val concreteWriter = writer(enum)

override def read(bson: BSONValue): A = concreteReader.read(bson)

Expand All @@ -110,8 +111,8 @@ object EnumHandler {
*/
def handlerLowercaseOnly[A <: EnumEntry](enum: Enum[A]): BSONHandler[BSONValue, A] =
new BSONHandler[BSONValue, A] {
val concreteReader = readerLowercaseOnly(enum)
val concreteWriter = writerLowercase(enum)
private val concreteReader = readerLowercaseOnly(enum)
private val concreteWriter = writerLowercase(enum)

override def read(bson: BSONValue): A = concreteReader.read(bson)

Expand All @@ -125,8 +126,8 @@ object EnumHandler {
*/
def handlerUppercaseOnly[A <: EnumEntry](enum: Enum[A]): BSONHandler[BSONValue, A] =
new BSONHandler[BSONValue, A] {
val concreteReader = readerUppercaseOnly(enum)
val concreteWriter = writerUppercase(enum)
private val concreteReader = readerUppercaseOnly(enum)
private val concreteWriter = writerUppercase(enum)

override def read(bson: BSONValue): A = concreteReader.read(bson)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,48 @@ object BSONValueHandlers extends BSONValueReads with BSONValueWrites {

}

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
trait BSONValueReads {

implicit val bsonReaderShort = new BSONReader[BSONValue, Short] {
implicit val bsonReaderShort: BSONReader[BSONValue, Short] = new BSONReader[BSONValue, Short] {
def read(bson: BSONValue): Short = bson match {
case BSONInteger(x) if Short.MaxValue >= x && Short.MinValue <= x =>
x.toShort
case _ => throw new RuntimeException(s"Could not convert $bson to Short")
}
}

implicit val bsonReaderInt = new BSONReader[BSONValue, Int] {
implicit val bsonReaderInt: BSONReader[BSONValue, Int] = new BSONReader[BSONValue, Int] {
def read(bson: BSONValue): Int = bson match {
case BSONInteger(x) => x
case _ => throw new RuntimeException(s"Could not convert $bson to Int")
}
}

implicit val bsonReaderLong = new BSONReader[BSONValue, Long] {
implicit val bsonReaderLong: BSONReader[BSONValue, Long] = new BSONReader[BSONValue, Long] {
def read(bson: BSONValue): Long = bson match {
case BSONLong(x) => x
case _ => throw new RuntimeException(s"Could not convert $bson to Long")
}
}

implicit val bsonReaderString = new BSONReader[BSONValue, String] {
def read(bson: BSONValue): String = bson match {
case BSONString(x) => x
case _ =>
throw new RuntimeException(s"Could not convert $bson to String")
implicit val bsonReaderString: BSONReader[BSONValue, String] =
new BSONReader[BSONValue, String] {
def read(bson: BSONValue): String = bson match {
case BSONString(x) => x
case _ =>
throw new RuntimeException(s"Could not convert $bson to String")
}
}
}

implicit val bsonReaderChar = new BSONReader[BSONValue, Char] {
implicit val bsonReaderChar: BSONReader[BSONValue, Char] = new BSONReader[BSONValue, Char] {
def read(bson: BSONValue): Char = bson match {
case BSONString(x) if x.length == 1 => x.charAt(0)
case _ => throw new RuntimeException(s"Could not convert $bson to Char")
}
}

implicit val bsonReaderByte = new BSONReader[BSONValue, Byte] {
implicit val bsonReaderByte: BSONReader[BSONValue, Byte] = new BSONReader[BSONValue, Byte] {
def read(bson: BSONValue): Byte = bson match {
case BSONInteger(x) => x.toByte
case _ => throw new RuntimeException(s"Could not convert $bson to Byte")
Expand All @@ -80,15 +82,15 @@ trait BSONValueReads {

trait BSONValueWrites {

implicit val bsonWriterShort = new BSONWriter[Short, BSONValue] {
implicit val bsonWriterShort: BSONWriter[Short, BSONValue] = new BSONWriter[Short, BSONValue] {
def write(t: Short): BSONValue = BSONInteger(t.toInt)
}

implicit val bsonWriterInt = new BSONWriter[Int, BSONValue] {
implicit val bsonWriterInt: BSONWriter[Int, BSONValue] = new BSONWriter[Int, BSONValue] {
def write(t: Int): BSONValue = BSONInteger(t)
}

implicit val bsonWriterLong = new BSONWriter[Long, BSONValue] {
implicit val bsonWriterLong: BSONWriter[Long, BSONValue] = new BSONWriter[Long, BSONValue] {
def write(t: Long): BSONValue = BSONLong(t)
}

Expand Down
1 change: 1 addition & 0 deletions macros/src/main/scala/enumeratum/EnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ object EnumMacros {
/**
* Builds and returns an expression for an IndexedSeq containing the given symbols
*/
@SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
private[enumeratum] def buildSeqExpr[A: c.WeakTypeTag](c: Context)(
subclassSymbols: Seq[c.universe.Symbol]
) = {
Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.13")

addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.6")

addSbtPlugin("org.wartremover" % "sbt-wartremover" % "1.2.1")

0 comments on commit 9c8d3d5

Please sign in to comment.