All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sksamuel.scapegoat.inspections.unsafe.AsInstanceOf.scala Maven / Gradle / Ivy

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)
            }
          }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy