Skip to content

GCI103 DictionaryItemsUnused #Python #DLG #Build #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add rule GCI 103 Dictionary Items Unused. A rule specifying that dictionary iteration should consider the pertinence of the element used.

### Changed

- compatibility updates for SonarQube 25.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
AvoidFullSQLRequest.class,
AvoidListComprehensionInIterations.class,
DetectUnoptimizedImageFormat.class,
AvoidMultipleIfElseStatementCheck.class
AvoidMultipleIfElseStatementCheck.class,
DictionaryItemsUnused.class
);

public static final String LANGUAGE = "py";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.python.checks;

import java.util.HashMap;
import java.util.Map;

import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
import org.sonar.plugins.python.api.SubscriptionContext;
import org.sonar.plugins.python.api.tree.CallExpression;
import org.sonar.plugins.python.api.tree.Expression;
import org.sonar.plugins.python.api.tree.ForStatement;
import org.sonar.plugins.python.api.tree.Name;
import org.sonar.plugins.python.api.tree.QualifiedExpression;
import org.sonar.plugins.python.api.tree.Tree;


@Rule(key ="GCI103")
public class DictionaryItemsUnused extends PythonSubscriptionCheck {

public static final String DESCRIPTION = "Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used";

private final Map<ForStatement, ItemsLoopInfo> itemsLoops = new HashMap<>();

@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, this::checkForLoop);
context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, this::finalizeCheck);
}

private void checkForLoop(SubscriptionContext context) {
ForStatement forStmt = (ForStatement) context.syntaxNode();

if (forStmt.expressions().size() == 2) {
Expression keyExpr = forStmt.expressions().get(0);
Expression valueExpr = forStmt.expressions().get(1);
Expression iterable = forStmt.testExpressions().get(0);

if (isItemsCall(iterable)) {
String key = ((Name) keyExpr).name();
String value = ((Name) valueExpr).name();

ItemsLoopInfo info = new ItemsLoopInfo(key, value);
itemsLoops.put(forStmt, info);

trackNameUsages(forStmt.body(), info);
}
}
}

private boolean isItemsCall(Expression expr) {
if (expr.is(Tree.Kind.CALL_EXPR)) {
CallExpression callExpr = (CallExpression) expr;
if (callExpr.callee().is(Tree.Kind.QUALIFIED_EXPR)) {
QualifiedExpression qualExpr = (QualifiedExpression) callExpr.callee();
boolean isItems = "items".equals(qualExpr.name().name());
return isItems;
}
}
return false;
}

private void trackNameUsages(Tree node, ItemsLoopInfo info) {
if (node instanceof Name) {
String name = ((Name) node).name();
info.markUsage(name);
}

for (Tree child : node.children()) {
trackNameUsages(child, info);
}
}

private void finalizeCheck(SubscriptionContext context) {
ForStatement forStmt = (ForStatement) context.syntaxNode();
ItemsLoopInfo info = itemsLoops.get(forStmt);

if (info != null) {

if (info.isOnlyOneUsed()) {
context.addIssue(forStmt.firstToken(), DESCRIPTION);
}

itemsLoops.remove(forStmt);
}
}

private static class ItemsLoopInfo {
final String keyVar;
final String valueVar;
boolean keyUsed = false;
boolean valueUsed = false;

ItemsLoopInfo(String keyVar, String valueVar) {
this.keyVar = keyVar;
this.valueVar = valueVar;
}

void markUsage(String var) {
if (var.equals(keyVar)) {
keyUsed = true;
}
if (var.equals(valueVar)) {
valueUsed = true;
}
}

boolean isOnlyOneUsed() {
return (keyUsed && !valueUsed) || (!keyUsed && valueUsed);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.python.checks;

import org.junit.Test;
import org.sonar.python.checks.utils.PythonCheckVerifier;

public class DictionaryItemsUnusedTest {

@Test
public void test() {
PythonCheckVerifier.verify("src/test/resources/checks/DictionaryItemsUnused.py", new DictionaryItemsUnused());
}
}
59 changes: 59 additions & 0 deletions src/test/resources/checks/DictionaryItemsUnused.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

for a, b in my_dict.items():
print(a, b)

for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
result.append(key)

for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
result.append(key)


for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
result.append(value)


for key in my_dict.keys():
result.append(key)


for value in my_dict.values():
result.append(value)


for item in my_dict.items():
result.append(item)


entries = []
for k, v in my_dict.items():
entries.append((k, v))

for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
do_something_with(key)

for k, v in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
do_something_with(v)

for key, value in my_dict.items():
print(f"{key}: {value}")

for k, v in my_dict.items():
some_list.append((k, v))

for k, v in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
used_keys.append(k)

if True:
for k, v in my_dict.items():
print(k)
print(v)

copied_dict = dict(my_dict.items())


for i, (k, v) in enumerate(my_dict.items()):
print(i, k, v)


{(k, v) for k, v in my_dict.items()}