Skip to content

Commit b5483a1

Browse files
authored
Merge branch 'main' into fix/challenge25-GCI82-manage-record
2 parents 50ea70a + 4a7435a commit b5483a1

File tree

6 files changed

+116
-36
lines changed

6 files changed

+116
-36
lines changed

.github/workflows/build.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ on:
55
branches:
66
- main
77
paths-ignore:
8-
- '*.md'
9-
- '.github/**/*.yml'
8+
- "*.md"
9+
- ".github/**/*.yml"
1010
tags:
11-
- '[0-9]+.[0-9]+.[0-9]+'
11+
- "[0-9]+.[0-9]+.[0-9]+"
1212
pull_request:
13-
types: [ opened, synchronize, reopened ]
13+
types: [opened, synchronize, reopened]
1414

1515
jobs:
1616
build:
@@ -22,32 +22,27 @@ jobs:
2222
- name: Checkout
2323
uses: actions/checkout@v4
2424
with:
25-
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
25+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
2626

2727
- name: Set up JDK 17
28-
uses: actions/setup-java@v3
28+
uses: actions/setup-java@v4
2929
with:
30-
distribution: 'temurin'
30+
distribution: "temurin"
3131
java-version: 17
32-
33-
- name: Cache Maven packages
34-
uses: actions/cache@v3
35-
with:
36-
path: ~/.m2
37-
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
38-
restore-keys: ${{ runner.os }}-m2
32+
cache: maven
3933

4034
- name: Verify
4135
run: ./mvnw -e -B verify
4236

4337
- name: Cache SonarQube packages
44-
uses: actions/cache@v3
38+
uses: actions/cache@v4
4539
with:
4640
path: ~/.sonar/cache
4741
key: ${{ runner.os }}-sonar
4842
restore-keys: ${{ runner.os }}-sonar
4943

5044
- name: SonarQube Scan
45+
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
5146
env:
5247
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5348
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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
1818
- [#105](https://github.com/green-code-initiative/creedengo-java/pull/105) fix GCI82 rule to handle record types and adjust test cases
19+
- [#4](https://github.com/green-code-initiative/creedengo-java/issues/4) Improvement: "++i" statement is not so bad
1920

2021
### Deleted
2122

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)