From 1e89fb7a889369c601e936b6d24b38a8655da919 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] 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)