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

org.wartremover.warts.ListUnapplySeq.scala Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package org.wartremover
package warts

object ListUnapplySeq extends WartTraverser {
  private[wartremover] def message: String = "Don't use List.unapplySeq"

  override def apply(u: WartUniverse): u.Traverser = {
    import u.universe._

    new Traverser {
      def isListConsUnapply(t: Tree): Boolean = {
        PartialFunction.cond(t) {
          case CaseDef(UnApply(Apply(TypeApply(Select(list, TermName("unapplySeq")), _), _), _), _, _) =>
            list.tpe <:< typeOf[List.type]
          case CaseDef(Bind(_, UnApply(Apply(TypeApply(Select(list, TermName("unapplySeq")), _), _), _)), _, _) =>
            list.tpe <:< typeOf[List.type]
        }
      }

      private[this] val listType = rootMirror.staticClass("scala.collection.immutable.List").asType.toTypeConstructor

      override def traverse(tree: Tree): Unit = {

        tree match {
          case t if hasWartAnnotation(u)(t) =>
          case m: Match =>
            if (m.selector.tpe.dealias.typeConstructor <:< listType) {
              // this is a list
            } else {
              // report error for each `case`
              m.cases.withFilter(isListConsUnapply).foreach { badCase =>
                error(u)(badCase.pos, message)
              }
            }
          case _ =>
            super.traverse(tree)
        }
      }
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy