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

com.sksamuel.scapegoat.inspections.collections.UnsafeTraversableMethods.scala Maven / Gradle / Ivy

package com.sksamuel.scapegoat.inspections.collections

import com.sksamuel.scapegoat.{Inspection, InspectionContext, Inspector, Levels}

class UnsafeTraversableMethods
    extends Inspection(
      text = "Use of unsafe Traversable methods.",
      defaultLevel = Levels.Error,
      description = "Checks for use of unsafe methods on Traversable.",
      explanation =
        "The following methods on Traversable are considered to be unsafe (head, tail, init, last, reduce, reduceLeft, reduceRight, max, maxBy, min, minBy)."
    ) {

  private val unsafeMethods = Set(
    "head",
    "tail",
    "init",
    "last",
    "reduce",
    "reduceLeft",
    "reduceRight",
    "max",
    "maxBy",
    "min",
    "minBy"
  )

  def inspector(context: InspectionContext): Inspector =
    new Inspector(context) {
      override def postTyperTraverser =
        new context.Traverser {

          import context.global._

          override def inspect(tree: Tree): Unit = {
            tree match {
              case Select(left, TermName(method)) =>
                if (isTraversable(left) && unsafeMethods.contains(method))
                  context.warn(tree.pos, self, tree.toString.take(500))
              case _ => continue(tree)
            }
          }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy