@@ -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