From 4f632023856a737500d24eed2a4bfc3c9518c681 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 10 Jul 2023 13:12:26 -0700 Subject: [PATCH] TreeOps: skip empty types in defDefReturnType Historically, the parser returns `Type.Name("Unit")` for declarations or definitions using the procedure syntax (i.e., no explicit `: type`). However, that tree occupies no tokens and hence needs to be ignored. --- .../shared/src/main/scala/org/scalafmt/util/TreeOps.scala | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index f7c0464245..65645eb207 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -350,7 +350,7 @@ object TreeOps { } @tailrec - def defDefReturnType(tree: Tree): Option[Type] = + private def defDefReturnTypeImpl(tree: Tree): Option[Type] = tree match { case d: Decl.Def => Some(d.decltpe) case d: Defn.Def => d.decltpe @@ -363,11 +363,14 @@ object TreeOps { case _: Pat.Var | _: Term.Name | _: Member.ParamClause | _: Member.ParamClauseGroup => tree.parent match { - case Some(p) => defDefReturnType(p) + case Some(p) => defDefReturnTypeImpl(p) case _ => None } case _ => None } + def defDefReturnType(tree: Tree): Option[Type] = + defDefReturnTypeImpl(tree).filter(!_.pos.isEmpty) + val DefDefReturnTypeLeft = new FormatToken.ExtractFromMeta(x => defDefReturnType(x.leftOwner)) val DefDefReturnTypeRight =