Skip to content

MessageRendering.explanation may have duplicate EOL Chars #12385

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

Closed
kevin-lee opened this issue May 9, 2021 · 0 comments · Fixed by #12386
Closed

MessageRendering.explanation may have duplicate EOL Chars #12385

kevin-lee opened this issue May 9, 2021 · 0 comments · Fixed by #12386

Comments

@kevin-lee
Copy link
Contributor

kevin-lee commented May 9, 2021

Summary

The result from MessageRendering.explanation may have duplicate EOL Chars at the end.

Compiler version

3.0.0-RC3 from print scalaVersion on sbt console.

Minimized code

Sorry I don't have any use case for this but found some logic error in MessageRendering.explanation

  def explanation(m: Message)(using Context): String = {
    val sb = new StringBuilder(
      s"""|
          |${Blue("Explanation").show}
          |${Blue("===========").show}""".stripMargin
    )
    sb.append(EOL).append(m.explanation)
    if (m.explanation.lastOption != Some(EOL)) sb.append(EOL) // This is always true so it always adds EOL
    sb.toString
  }

m.explanation.lastOption and Some(EOL) can never be equal since m.explanation is String which means m.explanation.lastOption is Option[Char] not Option[String] whereas Some(EOL) is Option[String].

Therefore, m.explanation.lastOption != Some(EOL) is always true.
So whether the sb above has EOL at the end or not, it always adds EOL at the end which may result in duplicate EOL chars at the end of the String result from MessageRendering.explanation.

Besides, checking just the last Char might not be good enough.
EOL is from java.lang.System.lineSeparator() so it might be just one Char like \n on xNix systems while it can be two Chars like \r\n on MS Windows systems.

Output

Expectation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant