scalacenter / scalafix

Refactoring and linting tool for Scala

Home Page:https://scalacenter.github.io/scalafix/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regression in scalafix v0.11.0 when using language.postfixOps

RCMartins opened this issue · comments

I think I found a regression in scalafix v0.11.0

If you have this rule:

  override def fix(implicit doc: SemanticDocument): Patch = {
    doc.tree.collect { tree =>
      println(s"structure: ${tree.structure}")
      println(s"syntax: ${tree.syntax}")
      println("-" * 40)
    }
    Patch.empty
  }

And this test file:

import scala.language.postfixOps

object Scalafixtest {
  def hexChar(ch: Int): Char =
    (if (ch < 10) '0' else 'A' - 10) + ch toChar
}

The .syntax in one of the inner ApplyInfix terms is wrong:

structure: Term.ApplyInfix(
  Term.If(
    Term.ApplyInfix(
      Term.Name("ch"),
      Term.Name("<"),
      Type.ArgClause(List()),
      Term.ArgClause(List(Lit.Int(10)), None)
    ),
    Lit.Char(0),
    Term.ApplyInfix(
      Lit.Char(A),
      Term.Name("-"),
      Type.ArgClause(List()),
      Term.ArgClause(List(Lit.Int(10)), None)
    ),
    List()
  ),
  Term.Name("+"),
  Type.ArgClause(List()),
  Term.ArgClause(List(Term.Name("ch")), None)
)
syntax: if (ch < 10) '0' else 'A' - 10) + ch
----------------------------------------

Note that the syntax is missing ( at the beginning of the expression. So the .syntax is returning invalid Scala code. If you create a Patch.replaceTree with this term it will likely produce wrong code.
This issue does not happen in scalafix 0.10.4, so it looks like a regression.


I created a repo that reproduces this issue. It has 2 branches, the main branch with scalafix v0.10.4 where the test passes using sbt tests/test, and the bug branch with v0.11.0 where the test fails.

Thanks for the bug report! Let's follow up in scalameta/scalameta#3219

Until v0.11.1 is tagged (no ETA at the moment), snapshots are available to fix that regression.