Skip to content

Commit

Permalink
RedundantParens: check if postfix only is allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 14, 2020
1 parent 54b7b4a commit 7eee1bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class RedundantParens(implicit ctx: RewriteCtx) extends RewriteSession {
private[rewrite] val rewriteFunc: PartialFunction[Tree, Unit] = {
case t @ (_: Term.Tuple | _: Type.Tuple | _: Lit.Unit) => remove(t, 2)

case g: Enumerator.Guard => remove(g.cond)
case g: Enumerator.Guard => maybeRemovePostfix(g.cond)

case t: Case => t.cond.foreach(remove(_))
case t: Case => t.cond.foreach(maybeRemovePostfix)

case t if usesAvoidInfix && t.parent.exists {
case p: Term.ApplyInfix => p.lhs ne t
Expand Down Expand Up @@ -75,6 +75,15 @@ class RedundantParens(implicit ctx: RewriteCtx) extends RewriteSession {
case _ => remove(tree, minToKeep)
}

// https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#prefix-infix-and-postfix-operations
private def maybeRemovePostfix(tree: Tree): Unit =
tree match {
case _: Lit | _: Term.Name | _: Term.ApplyInfix | _: Term.Select |
_: Term.Apply | _: Term.ApplyUnary =>
remove(tree)
case _ =>
}

private def remove(tree: Tree, minToKeep: Int = 0): Unit =
removeByTokens(tree.tokens, minToKeep)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,14 @@ object a {
}
}
>>>
test does not parse
object a {
val b = c match {
case true if (d match {
^
case true => true
case false => false
}) =>
}
}
<<< #2194 2 infix
object a {
val b = c match {
Expand Down

0 comments on commit 7eee1bd

Please sign in to comment.