Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantParens: check if postfix only is allowed #2195

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
105 changes: 105 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ rewrite.rules = [AvoidInfix, RedundantParens]
===
object a {
val z = (((insertData) op (readDatabase(id))))
val z = (((insertData)
op (readDatabase(id))))
}
>>>
object a {
val z = insertData.op(readDatabase(id))
val z = insertData
.op(readDatabase(id))
}
<<< #1895 4
object a {
Expand Down Expand Up @@ -301,3 +305,104 @@ val map = (Map("A" -> 1) - "Test" - "Test" - "Test" - "Test" - "Test"
>>>
val map = (Map("A" -> 1) - "Test" - "Test" - "Test" - "Test" - "Test"
- "Test" - "Test" - "Test" - "Test" - "Test" - "Test" - "Test" + "B" -> 2)
<<< #2194 1 match
object a {
val b = c match {
case true if (d match {
case true => true
case false => false
}) =>
}
}
>>>
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 {
case true if (d map {
case true => true
case false => false
}) =>
case true if (d
map {
case true => true
case false => false
}) =>
}
}
>>>
object a {
val b = c match {
case true if d map {
case true => true
case false => false
} =>
case true
if d
map {
case true => true
case false => false
} =>
}
}
<<< #2194 3 apply with brace
object a {
val b = c match {
case true if (d.map {
case true => true
case false => false
}) =>
}
}
>>>
object a {
val b = c match {
case true if d.map {
case true => true
case false => false
} =>
}
}
<<< #2194 4 apply with paren
object a {
val b = c match {
case true if (d.map({
case true => true
case false => false
})) =>
}
}
>>>
object a {
val b = c match {
case true if d.map({
case true => true
case false => false
}) =>
}
}
<<< #2194 5 unary
object a {
val b = c match {
case false if (+1) =>
case false if (-1) =>
case false if (~1) =>
case false if (!1) =>
}
}
>>>
object a {
val b = c match {
case false if +1 =>
case false if -1 =>
case false if ~1 =>
case false if !1 =>
}
}