Skip to content

Commit c0bdc56

Browse files
HarrisL2olhotak
authored andcommitted
Fix off-by-one and add tests
1 parent 9fab237 commit c0bdc56

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3596,7 +3596,7 @@ class UnnecessaryNN(reason: String, sourcePosition: SourcePosition)(using Contex
35963596

35973597
override def explain(using Context) = ""
35983598

3599-
private val nnSourcePosition = SourcePosition(sourcePosition.source, Span(sourcePosition.span.end, sourcePosition.span.end + 2, sourcePosition.span.end), sourcePosition.outer)
3599+
private val nnSourcePosition = SourcePosition(sourcePosition.source, Span(sourcePosition.span.end, sourcePosition.span.end + 3, sourcePosition.span.end), sourcePosition.outer)
36003600

36013601
override def actions(using Context) =
36023602
List(

compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import dotty.tools.DottyTest
44
import dotty.tools.dotc.rewrites.Rewrites
55
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
66
import dotty.tools.dotc.util.SourceFile
7+
import dotty.tools.dotc.core.Contexts._
78

89
import scala.annotation.tailrec
910
import scala.jdk.CollectionConverters.*
@@ -149,14 +150,43 @@ class CodeActionTest extends DottyTest:
149150
afterPhase = "patternMatcher"
150151
)
151152

153+
@Test def removeNN =
154+
val ctxx = newContext
155+
ctxx.setSetting(ctxx.settings.YexplicitNulls, true)
156+
checkCodeAction(
157+
code =
158+
"""|val s: String|Null = "foo".nn
159+
|""".stripMargin,
160+
title = "Remove unnecessary .nn",
161+
expected =
162+
"""|val s: String|Null = "foo"
163+
|""".stripMargin,
164+
ctxx = ctxx
165+
)
166+
167+
168+
@Test def removeNN2 =
169+
val ctxx = newContext
170+
ctxx.setSetting(ctxx.settings.YexplicitNulls, true)
171+
checkCodeAction(
172+
code =
173+
"""val s: String|Null = null.nn
174+
|""".stripMargin,
175+
title = "Remove unnecessary .nn",
176+
expected =
177+
"""val s: String|Null = null
178+
|""".stripMargin,
179+
ctxx = ctxx
180+
)
181+
152182
// Make sure we're not using the default reporter, which is the ConsoleReporter,
153183
// meaning they will get reported in the test run and that's it.
154184
private def newContext =
155185
val rep = new StoreReporter(null) with UniqueMessagePositions with HideNonSensicalMessages
156186
initialCtx.setReporter(rep).withoutColors
157187

158-
private def checkCodeAction(code: String, title: String, expected: String, afterPhase: String = "typer") =
159-
ctx = newContext
188+
private def checkCodeAction(code: String, title: String, expected: String, afterPhase: String = "typer", ctxx: Context = newContext) =
189+
ctx = ctxx
160190
val source = SourceFile.virtual("test", code).content
161191
val runCtx = checkCompile(afterPhase, code) { (_, _) => () }
162192
val diagnostics = runCtx.reporter.removeBufferedMessages

0 commit comments

Comments
 (0)