From 3c1340122c65c50fc086ca8f76b2b5b0f34234a8 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:01:14 -0700 Subject: [PATCH 1/2] Test exposed partial function within if-else --- .../resources/rewrite/RedundantBraces-if.stat | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat index 35fe9ca762..9811bf8405 100644 --- a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat +++ b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat @@ -393,3 +393,32 @@ object a: end if else checkJSNativeLoadSpecOf(treePos, sym) end if +<<< #4133 partial function within if-else +val toIterator: Any => Iterator[_] = if (lenient) { + { + case i: scala.collection.Iterable[_] => i.iterator + case l: java.util.List[_] => l.iterator().asScala + case a: Array[_] => a.iterator + case o => unsupportedCollectionType(o.getClass) + } +} else { + unsupportedCollectionType(tag.runtimeClass) +} +>>> +test does not parse: [dialect scala213] illegal start of simple expression +val toIterator: Any => Iterator[_] = + if (lenient) + case i: scala.collection.Iterable[_] => i.iterator + ^ + case l: java.util.List[_] => l.iterator().asScala + case a: Array[_] => a.iterator + case o => unsupportedCollectionType(o.getClass) +====== full result: ====== +val toIterator: Any => Iterator[_] = + if (lenient) + case i: scala.collection.Iterable[_] => i.iterator + case l: java.util.List[_] => l.iterator().asScala + case a: Array[_] => a.iterator + case o => unsupportedCollectionType(o.getClass) + else + unsupportedCollectionType(tag.runtimeClass) From f0490c2c2e1bc411080208bc425aab39e855a0fb Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:04:29 -0700 Subject: [PATCH 2/2] RedundantBraces: keep some around PartialFunction --- .../org/scalafmt/rewrite/RedundantBraces.scala | 15 +++++++++++---- .../resources/rewrite/RedundantBraces-if.stat | 17 ++++------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 3e76d3c978..12946b11b3 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -253,10 +253,17 @@ class RedundantBraces(implicit val ftoks: FormatTokens) owner match { case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] => if (!okToRemoveFunctionInApplyOrInit(t)) null else removeToken - case t: Term.PartialFunction if t.parent.exists { p => - SingleArgInBraces.orBlock(p).exists(_._2 eq t) && - t.pos.start != p.pos.start - } => removeToken + case t: Term.PartialFunction => t.parent match { + case Some(SingleArgInBraces.OrBlock(lft, `t`, _)) + if lft.left ne ft.right => + val ok = ftoks.findTokenWith(lft, ftoks.prev) { xft => + if (!xft.left.is[Token.LeftBrace]) Some(false) + else if (session.isRemovedOnLeft(xft, ok = true)) None + else Some(true) + }.contains(true) + if (ok) removeToken else null + case _ => null + } case t: Term.Block => t.parent match { case Some(f: Term.FunctionTerm) if okToReplaceFunctionInSingleArgApply(f) => removeToken diff --git a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat index 9811bf8405..14dd45356d 100644 --- a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat +++ b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat @@ -405,20 +405,11 @@ val toIterator: Any => Iterator[_] = if (lenient) { unsupportedCollectionType(tag.runtimeClass) } >>> -test does not parse: [dialect scala213] illegal start of simple expression -val toIterator: Any => Iterator[_] = - if (lenient) - case i: scala.collection.Iterable[_] => i.iterator - ^ - case l: java.util.List[_] => l.iterator().asScala - case a: Array[_] => a.iterator - case o => unsupportedCollectionType(o.getClass) -====== full result: ====== -val toIterator: Any => Iterator[_] = - if (lenient) +val toIterator: Any => Iterator[_] = if (lenient) { case i: scala.collection.Iterable[_] => i.iterator case l: java.util.List[_] => l.iterator().asScala case a: Array[_] => a.iterator case o => unsupportedCollectionType(o.getClass) - else - unsupportedCollectionType(tag.runtimeClass) +} +else + unsupportedCollectionType(tag.runtimeClass)