Skip to content

Commit b585092

Browse files
GCI103 DictionaryItemsUnused Fix ClassCastException in checkForLoop by validating expression types; merge syntax node consumers to avoid redundant processing.
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
1 parent ae9833f commit b585092

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/main/java/org/greencodeinitiative/creedengo/python/checks/DictionaryItemsUnused.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ public class DictionaryItemsUnused extends PythonSubscriptionCheck {
4040

4141
@Override
4242
public void initialize(Context context) {
43-
context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, this::checkForLoop);
44-
context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, this::finalizeCheck);
43+
context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, this::processForLoop);
4544
}
4645

47-
private void checkForLoop(SubscriptionContext context) {
46+
private void processForLoop(SubscriptionContext context) {
4847
ForStatement forStmt = (ForStatement) context.syntaxNode();
4948

5049
if (forStmt.expressions().size() == 2) {
@@ -53,15 +52,19 @@ private void checkForLoop(SubscriptionContext context) {
5352
Expression iterable = forStmt.testExpressions().get(0);
5453

5554
if (isItemsCall(iterable)) {
56-
String key = ((Name) keyExpr).name();
57-
String value = ((Name) valueExpr).name();
55+
String key = keyExpr.is(Tree.Kind.NAME) ? ((Name) keyExpr).name() : null;
56+
String value = valueExpr.is(Tree.Kind.NAME) ? ((Name) valueExpr).name() : null;
5857

59-
ItemsLoopInfo info = new ItemsLoopInfo(key, value);
60-
itemsLoops.put(forStmt, info);
58+
if (key != null && value != null) {
59+
ItemsLoopInfo info = new ItemsLoopInfo(key, value);
60+
itemsLoops.put(forStmt, info);
6161

62-
trackNameUsages(forStmt.body(), info);
62+
trackNameUsages(forStmt.body(), info);
63+
}
6364
}
64-
}
65+
}
66+
67+
finalizeCheck(context);
6568
}
6669

6770
private boolean isItemsCall(Expression expr) {

0 commit comments

Comments
 (0)