Skip to content

Commit 4a7435a

Browse files
authored
Merge pull request #44 from max-208/67-java
GCI67 S.T.E.P Challenge2025
2 parents 2db61c3 + 43bd134 commit 4a7435a

File tree

5 files changed

+106
-21
lines changed

5 files changed

+106
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- upgrade libraries versions
1616
- correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library)
1717
- upgrade JDK from 11 to 17
18+
- [#4](https://github.com/green-code-initiative/creedengo-java/issues/4) Improvement: "++i" statement is not so bad
1819

1920
### Deleted
2021

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ void testGCI67() {
429429
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java";
430430
String ruleId = "creedengo-java:GCI67";
431431
String ruleMsg = "Use ++i instead of i++";
432-
int[] startLines = new int[]{9, 19, 38};
433-
int[] endLines = new int[]{9, 19, 38};
432+
int[] startLines = new int[]{9, 24, 47};
433+
int[] endLines = new int[]{9, 24, 47};
434434

435435
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines);
436436
}

src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,71 @@ int foo1() {
99
return counter++; // Noncompliant {{Use ++i instead of i++}}
1010
}
1111

12+
private int j = 0;
13+
int foo10() {
14+
return this.j++; // Compliant because maybe the use case needs to return j AND increment it
15+
}
16+
1217
int foo11() {
1318
int counter = 0;
1419
return ++counter;
1520
}
1621

17-
void foo2(int value) {
22+
int foo2() {
1823
int counter = 0;
1924
counter++; // Noncompliant {{Use ++i instead of i++}}
25+
return counter;
2026
}
2127

22-
void foo22(int value) {
28+
int foo22() {
2329
int counter = 0;
2430
++counter;
31+
return counter;
2532
}
2633

27-
void foo3(int value) {
34+
int foo3() {
2835
int counter = 0;
2936
counter = counter + 197845 ;
37+
return counter;
3038
}
3139

32-
void foo4(int value) {
40+
int foo4() {
3341
int counter = 0;
3442
counter = counter + 35 + 78 ;
43+
return counter;
3544
}
3645

37-
void foo50(int value) {
46+
void foo50() {
3847
for (int i=0; i < 10; i++) { // Noncompliant {{Use ++i instead of i++}}
39-
System.out.println(i);
48+
System.out.println(i); //NOSONAR
4049
}
4150
}
4251

43-
void foo51(int value) {
52+
void foo51() {
4453
for (int i=0; i < 10; ++i) {
45-
System.out.println(i);
54+
System.out.println(i); //NOSONAR
4655
}
4756
}
57+
58+
void bar61(int value) {
59+
// For test purpose
60+
}
61+
62+
int foo61() {
63+
int i = 0;
64+
bar61(i++); // Compliant because maybe bar61 needs the unincremented value
65+
return i;
66+
}
67+
68+
int foo62() {
69+
int i = 0;
70+
bar61(2 + i++); // Compliant because maybe bar61 needs the unincremented value
71+
return i;
72+
}
73+
74+
void foo71() {
75+
int counter = 0;
76+
int a = 2 + counter++; // Compliant because we probably want to increment counter
77+
// then to add it to 2 to initialize a
78+
}
4879
}

src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
import org.sonar.check.Rule;
2424
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
25+
import org.sonar.plugins.java.api.tree.Arguments;
26+
import org.sonar.plugins.java.api.tree.BinaryExpressionTree;
2527
import org.sonar.plugins.java.api.tree.Tree;
28+
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
2629
import org.sonar.plugins.java.api.tree.Tree.Kind;
2730
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
2831

@@ -40,6 +43,13 @@ public List<Kind> nodesToVisit() {
4043

4144
@Override
4245
public void visitNode(Tree tree) {
46+
UnaryExpressionTree unaryExprTree = (UnaryExpressionTree) tree;
47+
48+
if (unaryExprTree.parent() instanceof BinaryExpressionTree
49+
|| unaryExprTree.parent() instanceof Arguments
50+
|| unaryExprTree.expression().is(Tree.Kind.MEMBER_SELECT)) {
51+
return ;
52+
}
4353
reportIssue(tree, MESSAGERULE);
4454
}
4555
}

src/test/files/IncrementCheck.java

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,92 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
class MyClass {
19-
MyClass(MyClass mc) {
18+
package org.greencodeinitiative.creedengo.java.checks;
19+
20+
private class Foo {
21+
public int i; //NOSONAR
22+
}
23+
24+
class IncrementCheck {
25+
26+
IncrementCheck(IncrementCheck mc) {
2027
}
2128

2229
int foo1() {
2330
int counter = 0;
2431
return counter++; // Noncompliant {{Use ++i instead of i++}}
2532
}
2633

34+
private int j = 0;
35+
int foo10() {
36+
return this.j++; // Compliant because maybe the use case needs to return j AND increment it
37+
}
38+
2739
int foo11() {
2840
int counter = 0;
2941
return ++counter;
3042
}
3143

32-
void foo2(int value) {
44+
int foo12() {
45+
Foo f;
46+
return f.i++; // Compliant because maybe the use case needs to return j AND increment it
47+
}
48+
49+
int foo2() {
3350
int counter = 0;
3451
counter++; // Noncompliant {{Use ++i instead of i++}}
52+
return counter;
3553
}
3654

37-
void foo22(int value) {
55+
int foo22() {
3856
int counter = 0;
3957
++counter;
58+
return counter;
4059
}
4160

42-
void foo3(int value) {
61+
int foo3() {
4362
int counter = 0;
4463
counter = counter + 197845 ;
64+
return counter;
4565
}
4666

47-
void foo4(int value) {
48-
int counter =0;
67+
int foo4() {
68+
int counter = 0;
4969
counter = counter + 35 + 78 ;
70+
return counter;
5071
}
5172

52-
void foo50(int value) {
73+
void foo50() {
5374
for (int i=0; i < 10; i++) { // Noncompliant {{Use ++i instead of i++}}
54-
System.out.println(i);
75+
System.out.println(i); //NOSONAR
5576
}
5677
}
5778

58-
void foo51(int value) {
79+
void foo51() {
5980
for (int i=0; i < 10; ++i) {
60-
System.out.println(i);
81+
System.out.println(i); //NOSONAR
6182
}
6283
}
84+
85+
void bar61(int value) {
86+
// For test purpose
87+
}
88+
89+
int foo61() {
90+
int i = 0;
91+
bar61(i++); // Compliant because maybe bar61 needs the unincremented value
92+
return i;
93+
}
94+
95+
int foo62() {
96+
int i = 0;
97+
bar61(2 + i++); // Compliant because maybe bar61 needs the unincremented value
98+
return i;
99+
}
100+
101+
void foo71() {
102+
int counter = 0;
103+
int a = 2 + counter++; // Compliant because we probably want to increment counter
104+
// then to add it to 2 to initialize a
105+
}
63106
}

0 commit comments

Comments
 (0)