From 7bb149544fa7a4e749065157f2a11bc382fa39cd Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Mon, 23 Aug 2021 19:22:15 +0200 Subject: [PATCH] Don't call toString on null values --- .../main/scala-3/mdoc/internal/document/Printing.scala | 10 +++++++--- .../test/scala/tests/worksheets/WorksheetSuite.scala | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/runtime/src/main/scala-3/mdoc/internal/document/Printing.scala b/runtime/src/main/scala-3/mdoc/internal/document/Printing.scala index 9a941ef98..c0e1b8059 100644 --- a/runtime/src/main/scala-3/mdoc/internal/document/Printing.scala +++ b/runtime/src/main/scala-3/mdoc/internal/document/Printing.scala @@ -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) = { + if (value != null) value.toString else "null" } } diff --git a/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala b/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala index 58df53400..11dc04b58 100644 --- a/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala +++ b/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala @@ -51,6 +51,16 @@ class WorksheetSuite extends BaseSuite { |""".stripMargin ) + checkDecorations( + "null", + """ + |val x: String = null + |""".stripMargin, + """| // : String = null + |x: String = null + |""".stripMargin + ) + checkDecorations( "import-future".tag(OnlyScala3), """|import $scalac.`-source future`