Skip to content

Commit 4788665

Browse files
committed
small correction on GCI103 after merge
1 parent e43d6e7 commit 4788665

File tree

5 files changed

+9
-31
lines changed

5 files changed

+9
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- [#76](https://github.com/green-code-initiative/creedengo-python/pull/76) Add rule GCI 103 Dictionary Items Unused. A rule specifying that dictionary iteration should consider the pertinence of the element used.
12+
- [#76](https://github.com/green-code-initiative/creedengo-python/pull/76) Add rule GCI103 Dictionary Items Unused. A rule specifying that dictionary iteration should consider the pertinence of the element used.
1313
- [#79](https://github.com/green-code-initiative/creedengo-python/pull/79) Add rule GCI106 Avoid SQRT in a loop
1414
- [#71](https://github.com/green-code-initiative/creedengo-python/pull/71) Add rule GCI96 Require Usecols Argument in Pandas Read Functions
1515
- [#72](https://github.com/green-code-initiative/creedengo-python/pull/72) Add rule GCI97 Optimize square computation (scalar vs vectorized method)

src/it/java/org/greencodeinitiative/creedengo/python/integration/tests/GCIRulesIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ void testGCI103(){
310310
String ruleId = "creedengo-python:GCI103";
311311
String ruleMsg = "Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used";
312312
int[] startLines = new int[]{
313-
5, 8, 12, 32, 35, 44
313+
5, 8, 24, 27, 36
314314
};
315315
int[] endLines = new int[]{
316-
5, 8, 12, 32, 35, 44
316+
5, 8, 24, 27, 36
317317
};
318318

319319
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);

src/it/test-projects/creedengo-python-plugin-test-project/src/dictionaryItemsUnused.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@
55
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}}
66
result.append(key)
77

8-
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}}
9-
result.append(key)
10-
11-
128
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}}
139
result.append(value)
1410

15-
1611
for key in my_dict.keys():
1712
result.append(key)
1813

19-
2014
for value in my_dict.values():
2115
result.append(value)
2216

23-
2417
for item in my_dict.items():
2518
result.append(item)
2619

27-
2820
entries = []
2921
for k, v in my_dict.items():
3022
entries.append((k, v))
@@ -51,9 +43,7 @@
5143

5244
copied_dict = dict(my_dict.items())
5345

54-
5546
for i, (k, v) in enumerate(my_dict.items()):
5647
print(i, k, v)
5748

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

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,15 @@ private boolean isItemsCall(Expression expr) {
7272
CallExpression callExpr = (CallExpression) expr;
7373
if (callExpr.callee().is(Tree.Kind.QUALIFIED_EXPR)) {
7474
QualifiedExpression qualExpr = (QualifiedExpression) callExpr.callee();
75-
boolean isItems = "items".equals(qualExpr.name().name());
76-
return isItems;
75+
return "items".equals(qualExpr.name().name());
7776
}
7877
}
7978
return false;
8079
}
8180

8281
private void trackNameUsages(Tree node, ItemsLoopInfo info) {
83-
if (node instanceof Name) {
84-
String name = ((Name) node).name();
85-
info.markUsage(name);
82+
if (node instanceof Name nodeName) {
83+
info.markUsage(nodeName.name());
8684
}
8785

8886
for (Tree child : node.children()) {
@@ -115,11 +113,11 @@ private static class ItemsLoopInfo {
115113
this.valueVar = valueVar;
116114
}
117115

118-
void markUsage(String var) {
119-
if (var.equals(keyVar)) {
116+
void markUsage(String val) {
117+
if (val.equals(keyVar)) {
120118
keyUsed = true;
121119
}
122-
if (var.equals(valueVar)) {
120+
if (val.equals(valueVar)) {
123121
valueUsed = true;
124122
}
125123
}

src/test/resources/checks/DictionaryItemsUnused.py renamed to src/test/resources/checks/dictionaryItemsUnused.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@
55
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}}
66
result.append(key)
77

8-
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}}
9-
result.append(key)
10-
11-
128
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}}
139
result.append(value)
1410

15-
1611
for key in my_dict.keys():
1712
result.append(key)
1813

19-
2014
for value in my_dict.values():
2115
result.append(value)
2216

23-
2417
for item in my_dict.items():
2518
result.append(item)
2619

27-
2820
entries = []
2921
for k, v in my_dict.items():
3022
entries.append((k, v))
@@ -51,9 +43,7 @@
5143

5244
copied_dict = dict(my_dict.items())
5345

54-
5546
for i, (k, v) in enumerate(my_dict.items()):
5647
print(i, k, v)
5748

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

0 commit comments

Comments
 (0)