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

Don't call toString on null values #550

Merged
merged 1 commit into from
Aug 23, 2021
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
10 changes: 7 additions & 3 deletions runtime/src/main/scala-3/mdoc/internal/document/Printing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package mdoc.internal.document
import Compat.TPrint

object Printing {
inline def stringValue[T](value: T) = value.toString
inline def stringValue[T](value: T) = nullableToString(value)
inline def typeString[T](tprint: TPrint[T]) = tprint.render

inline def print[T](value: T, out : StringBuilder, width: Int, height: Int) = {
out.append(value.toString)
out.append(nullableToString(value))
}

inline def printOneLine[T](value: T, out : StringBuilder, width: Int) = {
out.append(value.toString.replace("\n",""))
out.append(nullableToString(value).replace("\n",""))
}

inline private def nullableToString[T](value: T) = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be replaced with String.valueOf or an interpolated string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be equivalent. I can change it later on when doing some work around it. Or do you think this might work more correctly?

if (value != null) value.toString else "null"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class WorksheetSuite extends BaseSuite {
|""".stripMargin
)

checkDecorations(
"null",
"""
|val x: String = null
|""".stripMargin,
"""|<val x: String = null> // : String = null
|x: String = null
|""".stripMargin
)

checkDecorations(
"import-future".tag(OnlyScala3),
"""|import $scalac.`-source future`
Expand Down