Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created February 4, 2020 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/4a5dd4f90b52010ce1a4cde066232ee5 to your computer and use it in GitHub Desktop.
Save xuwei-k/4a5dd4f90b52010ce1a4cde066232ee5 to your computer and use it in GitHub Desktop.
package example
import org.wartremover.{ WartTraverser, WartUniverse }
object ScalapbDefaultArgumentWarts extends WartTraverser {
def apply(u: WartUniverse): u.Traverser = {
import u.universe._
val ApplyName = TermName("apply")
new Traverser {
override def traverse(tree: Tree): Unit = {
tree match {
case t if hasWartAnnotation(u)(t) =>
case t if t.pos.source.path.contains("src_managed") =>
// 自動生成コードは除くための設定。
// TODO 除く方法が少し無理矢理感があるので、もっと適切な方法・・・
case Apply(Select(a, ApplyName), _)
if a.tpe.baseClasses.exists(x => x.fullName.toString.startsWith("scalapb.GeneratedMessageCompanion")) && a.symbol.fullName != "com.google.protobuf.empty.Empty" =>
warning(u)(
tree.pos,
"scalapbが生成したobjectのapplyはデフォルト引数を意図せず使ってしまうバグの原因になりやすいので使用禁止です。ofを使ってください"
)
case _ =>
}
super.traverse(tree)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment