Skip to content

Commit

Permalink
RedundantParens: don't rewrite unary within select
Browse files Browse the repository at this point in the history
In reality, it's unclear why ApplyUnary extends Term.Ref, but since
Term.Select does, too, let's introduce a special-case handling for it.
  • Loading branch information
kitbellew committed Jul 25, 2024
1 parent b978df4 commit b1b2b49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ class RedundantParens(implicit val ftoks: FormatTokens)
private def okToReplaceOther(
t: Tree,
)(implicit style: ScalafmtConfig): Boolean = t match {
case _: Lit | _: Member.Apply | _: Term.Interpolate |
_: Term.PartialFunction => true
case _: Lit => !t.parent.exists(_.is[Term.Ref]) ||
!t.tokens.headOption.exists(_.is[Token.Ident]) // unary
case _: Term.ApplyUnary => !t.parent.exists(_.is[Term.Ref])
case _: Member.Apply | _: Term.Interpolate | _: Term.PartialFunction => true
case t: Term.Select => ftoks.tokenBefore(t.name).left.is[Token.Dot]
case _: Ref => true // Ref must be after Select
case _: Ref => true // Ref must be after Select and ApplyUnary
case t: Term.Match => style.dialect.allowMatchAsOperator &&
ftoks.tokenAfter(t.expr).right.is[Token.Dot] && // like select
ftoks.tokenBefore(t.cases).left.is[Token.LeftBrace]
Expand Down
35 changes: 14 additions & 21 deletions scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1480,9 +1480,9 @@ object a {
}
>>>
object a {
~validByte.toByte
+validByte.toByte
-validByte.toByte
(~validByte).toByte
(+validByte).toByte
(-validByte).toByte
}
<<< #4116 2 int literal
object a {
Expand All @@ -1492,9 +1492,9 @@ object a {
}
>>>
object a {
~1.toByte
+1.toByte
-1.toByte
(~1).toByte
(+1).toByte
(-1).toByte
}
<<< #4116 3 bool literal
object a {
Expand All @@ -1505,25 +1505,18 @@ object a {
>>>
object a {
false.toByte
!true.toByte
~true.toByte
(!true).toByte
(~true).toByte
}
<<< #4116 3.1 !!bool literal
object a {
(!(!true))
(!(!true)).toByte
}
>>>
test does not parse: [dialect scala213] `;` expected but `true` found
object a {
! !true
^
! !true.toByte
}
====== full result: ======
object a {
! !true
! !true.toByte
!(!true)
(!(!true)).toByte
}
<<< #4116 4 string literal
object a {
Expand All @@ -1534,8 +1527,8 @@ object a {
>>>
object a {
"foo".toByte
!"bar".toByte
~"baz".toByte
(!"bar").toByte
(~"baz").toByte
}
<<< #4116 5 char literal
object a {
Expand All @@ -1546,6 +1539,6 @@ object a {
>>>
object a {
'\n'.toByte
!'\r'.toByte
~'\f'.toByte
(!'\r').toByte
(~'\f').toByte
}

0 comments on commit b1b2b49

Please sign in to comment.