Skip to content

Commit

Permalink
Replace (Typing)Transformer with simple Traverser to avoid unnecessar…
Browse files Browse the repository at this point in the history
…y tree copying
  • Loading branch information
mkubala committed May 3, 2015
1 parent 0d0bf95 commit 6549b8a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ class GuardedBlocksPlugin(val global: Global) extends Plugin {

override val components: List[PluginComponent] = List(new GuardedBlocksComponent(global))



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.softwaremill.guardedblocks.components

import scala.tools.nsc.Global

class BlockTreesGuardian[G <: Global](val global: G) {
import global._

val guard: Tree => Unit = BlockTreesGuardianTraverser.traverse

private object BlockTreesGuardianTraverser extends Traverser {
override def traverse(tree: Tree): Unit = {
tree match {
case block@Block(stats, expr) =>
for {
stat <- stats.filter(_.tpe <:< block.tpe)
} global.reporter.error(stat.pos, s"Found unused statement!")
case _ =>
}
super.traverse(tree)
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,19 @@ package com.softwaremill.guardedblocks.components

import scala.tools.nsc._
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform._

class GuardedBlocksComponent(val global: Global) extends PluginComponent with Transform with TypingTransformers {
class GuardedBlocksComponent(val global: Global) extends PluginComponent {

import global._

override val phaseName: String = "guardblocks"

override def description: String = "check for unused statements in code blocks"

override val runsAfter: List[String] = List("typer")

override protected def newTransformer(unit: CompilationUnit): Transformer =
new GuardedBlockTransformer(unit)

class GuardedBlockTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {
override def transform(tree: Tree): Tree = {
tree match {
case block@Block(stats, expr) =>
for {
stat <- stats.filter(_.tpe <:< block.tpe)
} global.reporter.error(stat.pos, s"Found unused statement!")
case _ =>
}
super.transform(tree)
}
private val guardian = new BlockTreesGuardian[global.type](global)

override def newPhase(prev: Phase): Phase = new StdPhase(prev) {
override def apply(unit: CompilationUnit): Unit = guardian.guard(unit.body)
}

}

0 comments on commit 6549b8a

Please sign in to comment.