Skip to content

Commit d80f95d

Browse files
Merge pull request #400 from apex-dev-tools/fix/issue-398-ghosted-type-list-unused
fix: Track variable usage in ghosted type list initializers (fixes #398)
2 parents 5e9a978 + 3dd7a71 commit d80f95d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Loop variables used only in iteration control (condition/increment) are no longer incorrectly flagged as unused (#397)
13+
- Variables used in ghosted type list initializers are no longer incorrectly flagged as unused (#398)
1314

1415
## [6.0.1] - 2025-10-21
1516

jvm/src/main/scala/com/nawforce/apexlink/cst/Creator.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ final case class SetOrListCreatorRest(parts: ArraySeq[Expression]) extends Creat
433433
case Left(error) =>
434434
if (!context.module.isGhostedType(enclosedType.get))
435435
context.log(error.asIssue(location))
436+
// Still verify parts for variable usage tracking, even though we can't type check
437+
parts.foreach(_.verify(input, context))
436438
ExprContext.empty
437439
case Right(toType) =>
438440
// For each expression check we can assign to the generic type

jvm/src/test/scala/com/nawforce/apexlink/plugin/UnusedTest.scala

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,59 @@ class UnusedTest extends AnyFunSuite with TestHelper {
14111411
}
14121412
}
14131413

1414+
// Tests for issue #398 - Variables in ghosted type list initialization
1415+
1416+
test("Variable used in ghosted type list initializer should not be flagged (issue #398)") {
1417+
FileSystemHelper.run(
1418+
Map(
1419+
"sfdx-project.json" ->
1420+
"""{
1421+
|"packageDirectories": [{"path": "force-app"}],
1422+
|"plugins": {"dependencies": [{"namespace": "ext"}]}
1423+
|}""".stripMargin,
1424+
"force-app/Dummy.cls" -> "public class Dummy { { String msg = 'test'; List<ext__Type__c> items = new List<ext__Type__c>{ new ext__Type__c(Name = msg) }; System.debug(items); } }"
1425+
)
1426+
) { root: PathLike =>
1427+
createOrgWithUnused(root)
1428+
assert(getMessages(root.join("force-app/Dummy.cls")).isEmpty)
1429+
}
1430+
}
1431+
1432+
test("Variable used in multiple ghosted type list elements should not be flagged") {
1433+
FileSystemHelper.run(
1434+
Map(
1435+
"sfdx-project.json" ->
1436+
"""{
1437+
|"packageDirectories": [{"path": "force-app"}],
1438+
|"plugins": {"dependencies": [{"namespace": "ext"}]}
1439+
|}""".stripMargin,
1440+
"force-app/Dummy.cls" -> "public class Dummy { { String msg1 = 'test1'; String msg2 = 'test2'; List<ext__Type__c> items = new List<ext__Type__c>{ new ext__Type__c(Name = msg1), new ext__Type__c(Name = msg2) }; System.debug(items); } }"
1441+
)
1442+
) { root: PathLike =>
1443+
createOrgWithUnused(root)
1444+
assert(getMessages(root.join("force-app/Dummy.cls")).isEmpty)
1445+
}
1446+
}
1447+
1448+
test("Unused variable with ghosted type list should still be flagged") {
1449+
FileSystemHelper.run(
1450+
Map(
1451+
"sfdx-project.json" ->
1452+
"""{
1453+
|"packageDirectories": [{"path": "force-app"}],
1454+
|"plugins": {"dependencies": [{"namespace": "ext"}]}
1455+
|}""".stripMargin,
1456+
"force-app/Dummy.cls" -> "public class Dummy { { String msg1 = 'test'; String unused = 'unused'; List<ext__Type__c> items = new List<ext__Type__c>{ new ext__Type__c(Name = msg1) }; System.debug(items); } }"
1457+
)
1458+
) { root: PathLike =>
1459+
createOrgWithUnused(root)
1460+
assert(
1461+
getMessages(root.join("force-app/Dummy.cls")) ==
1462+
"Unused: line 1 at 52-69: Unused local variable 'unused'\n"
1463+
)
1464+
}
1465+
}
1466+
14141467
// Additional tests for issue #397 - for loop variable usage detection
14151468

14161469
test("For loop with multiple variables - unused second variable should be flagged") {

0 commit comments

Comments
 (0)