From 05f9f4bebcc276699f9d9df0f0efc82de92be714 Mon Sep 17 00:00:00 2001 From: Mitama Date: Sun, 17 Sep 2023 13:57:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20Material=20=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E3=81=99=E3=82=8B=20match=20=E3=82=92=20error=20=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B=20Scalafix=20rule=20=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...atchForMaterialToErrorForPerformance.scala | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala diff --git a/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala b/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala new file mode 100644 index 0000000000..4859ddd310 --- /dev/null +++ b/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala @@ -0,0 +1,27 @@ +package fix + +import scalafix.v1._ + +import scala.meta._ + +/** + * Lints on string interpolation where its variable part contains `case class` without `toString`. + */ +// noinspection ScalaUnusedSymbol; referred from scalafix implicitly +// NOTE: see AST on https://xuwei-k.github.io/scalameta-ast/ or https://astexplorer.net +class MatchForMaterialToErrorForPerformance extends SemanticRule("MatchForMaterialToErrorForPerformance") { + override def fix(implicit doc: SemanticDocument): Patch = { + doc.tree.collect { + case t @ Term.Match.After_4_4_5(term, _, _) => + term.symbol.info match { + case Some(info) if info.signature.toString == "Material" => + val message = + s""" + |Don't use org.bukkit.Material in scrutinee of match expressions! + |See https://github.com/GiganticMinecraft/SeichiAssist/issues/2226 for more detail.""".stripMargin + Patch.lint(Diagnostic("error", message, t.pos)) + case _ => Patch.empty + } + }.asPatch + } +} From 2e7ede117b73421d2b9b7df1ed9d294ba7404a7a Mon Sep 17 00:00:00 2001 From: Mitama Date: Sun, 17 Sep 2023 23:35:55 +0900 Subject: [PATCH 2/2] feat: matches a fully qualified class name using SymbolMatcher.normalized --- .../MatchForMaterialToErrorForPerformance.scala | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala b/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala index 4859ddd310..a1d058274a 100644 --- a/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala +++ b/src/scalafix/scala/fix/MatchForMaterialToErrorForPerformance.scala @@ -15,11 +15,15 @@ class MatchForMaterialToErrorForPerformance extends SemanticRule("MatchForMateri case t @ Term.Match.After_4_4_5(term, _, _) => term.symbol.info match { case Some(info) if info.signature.toString == "Material" => - val message = - s""" - |Don't use org.bukkit.Material in scrutinee of match expressions! - |See https://github.com/GiganticMinecraft/SeichiAssist/issues/2226 for more detail.""".stripMargin - Patch.lint(Diagnostic("error", message, t.pos)) + info.signature match { + case ValueSignature(TypeRef(_, symbol, _)) if SymbolMatcher.normalized("org/bukkit/Material").matches(symbol) => + val message = + s""" + |Don't use org.bukkit.Material in scrutinee of match expressions! + |See https://github.com/GiganticMinecraft/SeichiAssist/issues/2226 for more detail.""".stripMargin + Patch.lint(Diagnostic("error", message, t.pos)) + case _ => Patch.empty + } case _ => Patch.empty } }.asPatch