PlayWarts

Build Status Dependency Status Codacy Badge Maven Central

WartRemover warts for Play Framework.

Versions

PlayWarts version WartRemover version Play version Scala version sbt version Supported
1.2.0 2.2.1 2.6.7 2.11.12, 2.12.4 1.0.x, 0.13.x
1.0.0 2.1.1 2.6.0 2.11.11, 2.12.2 0.13.x No
0.31.0 (README) 2.0.1 2.5.x 2.11.x 0.13.x No
0.15 (README) 0.14 2.4.x 2.11.x 0.13.x No

Usage

  1. Setup WartRemover.

  2. Add the following to your plugins.sbt:

    addSbtPlugin("org.danielnixon" % "sbt-playwarts" % "1.2.0")
  3. Add the following to your build.sbt:

    wartremoverWarnings ++= Seq(
      PlayWart.CookiesPartial,
      PlayWart.FlashPartial,
      PlayWart.FormPartial,
      PlayWart.HeadersPartial,
      PlayWart.InjectedController,
      PlayWart.JavaApi,
      PlayWart.JsLookupResultPartial,
      PlayWart.JsReadablePartial,
      PlayWart.LangObject,
      PlayWart.SessionPartial,
      PlayWart.TypedMapPartial,
      PlayWart.WSResponsePartial)

Warts

Play Framework

CookiesPartial

play.api.mvc.Cookies has an apply method that can throw. Use Cookies#get instead.

FlashPartial

play.api.mvc.Flash has an apply method that can throw. Use Flash#get instead.

FormPartial

play.api.data.Form has a get method which will throw if the form contains errors. The program should be refactored to use play.api.data.Form#fold to explicitly handle forms with errors and successful form submissions.

HeadersPartial

play.api.mvc.Headers has an apply method that can throw. Use Headers#get instead.

InjectedController

Inheriting from play.api.mvc.InjectedController is disabled because it uses JSR 330 method injection and therefore cannot work without mutability and magic (and it hinders testing). Inherit your controllers from AbstractController instead. See Migration26#Scala-Controller-changes.

JavaApi

The Java API in the play package is disabled. Use the Scala API under play.api instead.

JsLookupResultPartial

play.api.libs.json.JsLookupResult has a get method which can throw. Use JsLookupResult#getOrElse instead.

JsReadablePartial

play.api.libs.json.JsReadable has an as method which can throw. Use JsReadable#asOpt instead.

LangObject

The play.api.i18n.Lang object is disabled. Use play.api.i18n.Langs instead.

SessionPartial

play.api.mvc.Session has an apply method that can throw. Use Session#get instead.

TypedMapPartial

play.api.libs.typedmap.TypedMap has an apply method that can throw. Use TypedMap#get instead.

WSResponsePartial

The play.api.libs.ws.WSResponse trait defines json and xml methods that will throw if the response body can't be parsed as JSON or XML respectively (the default AhcWSResponse implementation of this trait throws JsonParseException and SAXException). You can wrap these unsafe methods in an implicit class that might look something like this:

implicit class WSResponseWrapper(val response: WSResponse) extends AnyVal {
  @SuppressWarnings(Array("org.danielnixon.playwarts.WSResponsePartial"))
  def jsonOpt: Option[JsValue] = catching[JsValue](classOf[JsonParseException]) opt response.json

  @SuppressWarnings(Array("org.danielnixon.playwarts.WSResponsePartial"))
  def xmlOpt: Option[Elem] = catching[Elem](classOf[SAXException]) opt response.xml
}

See also