Skip to content

Commit 24b9b68

Browse files
committed
Remove unused private member
1 parent 597849d commit 24b9b68

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,7 +3205,7 @@ extends Message(UnusedSymbolID) {
32053205
}
32063206

32073207
object UnusedSymbol {
3208-
private def removeLocalDefAction(treePos: SourcePosition)(using Context): List[CodeAction] = {
3208+
private def removeDefinitionAtPos(treePos: SourcePosition)(using Context): List[CodeAction] = {
32093209
val source = treePos.source
32103210
val sourcePos = treePos.sourcePos
32113211
val endLine = source.offsetToLine(sourcePos.end - 1)
@@ -3226,9 +3226,9 @@ object UnusedSymbol {
32263226
)
32273227
}
32283228
def imports(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused import", List.empty)
3229-
def localDefs(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused local definition", removeLocalDefAction(treePos))
3229+
def localDefs(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused local definition", removeDefinitionAtPos(treePos))
32303230
def explicitParams(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused explicit parameter", List.empty)
32313231
def implicitParams(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused implicit parameter", List.empty)
3232-
def privateMembers(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused private member", List.empty)
3232+
def privateMembers(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused private member", removeDefinitionAtPos(treePos))
32333233
def patVars(treePos: SourcePosition)(using Context): UnusedSymbol = new UnusedSymbol(treePos, i"unused pattern variable", List.empty)
32343234
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,49 @@ class CodeActionTest extends DottyTest:
153153
afterPhase = "checkUnusedPostInlining"
154154
)
155155

156+
@Test def removeUnusedLocalDefinitionBlock =
157+
checkCodeAction(
158+
code = """object Test:
159+
| def foo(): Int = {
160+
| val a = {
161+
| 456
162+
| }
163+
| 123
164+
| }
165+
|""".stripMargin,
166+
title = "Remove unused code",
167+
expected = """object Test:
168+
| def foo(): Int = {
169+
| 123
170+
| }
171+
|""".stripMargin ,
172+
afterPhase = "checkUnusedPostInlining"
173+
)
174+
175+
@Test def removeUnusedPrivateMember =
176+
checkCodeAction(
177+
code = """object Test:
178+
| private def foo: String = "123"
179+
|""".stripMargin,
180+
title = "Remove unused code",
181+
expected = """object Test:
182+
|""".stripMargin ,
183+
afterPhase = "checkUnusedPostInlining"
184+
)
185+
186+
@Test def removeUnusedPrivateMemberBlock =
187+
checkCodeAction(
188+
code = """object Test:
189+
| private def foo: String = {
190+
| println("hello")
191+
| "456"
192+
| }
193+
|""".stripMargin,
194+
title = "Remove unused code",
195+
expected = """object Test:
196+
|""".stripMargin ,
197+
afterPhase = "checkUnusedPostInlining"
198+
)
156199

157200
// Make sure we're not using the default reporter, which is the ConsoleReporter,
158201
// meaning they will get reported in the test run and that's it.

0 commit comments

Comments
 (0)