com.sksamuel.scapegoat.inspections.unsafe.AsInstanceOf.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalac-scapegoat-plugin_2.13.13 Show documentation
Show all versions of scalac-scapegoat-plugin_2.13.13 Show documentation
Scala compiler plugin for static code analysis
The newest version!
package com.sksamuel.scapegoat.inspections.unsafe
import com.sksamuel.scapegoat._
class AsInstanceOf
extends Inspection(
text = "Use of asInstanceOf",
defaultLevel = Levels.Warning,
description = "Checks for use of asInstanceOf.",
explanation =
"Use of asInstanceOf is considered a bad practice - consider using pattern matching instead."
) {
def inspector(ctx: InspectionContext): Inspector =
new Inspector(ctx) {
override def postTyperTraverser: context.Traverser =
new context.Traverser {
import context.global._
override def inspect(tree: Tree): Unit = {
tree match {
// this will skip any uses of manifest etc
case TypeApply(Select(qual, TermName("asInstanceOf")), _)
if qual.toString != "classOf[java.lang.Class]" =>
context.warn(tree.pos, self, tree.toString.take(500))
case DefDef(modifiers, _, _, _, _, _) if modifiers.hasFlag(Flag.SYNTHETIC) => // no further
case Match(_, cases) => // ignore selector and process cases
cases.foreach(traverse)
case _ => continue(tree)
}
}
}
}
}