From f95b8c072a0e65010d0016c9aa3d11f502ddd930 Mon Sep 17 00:00:00 2001 From: hocine hacherouf Date: Wed, 29 May 2024 22:18:28 +0200 Subject: [PATCH 1/4] Improve rule EC67 #4 Ignore rule EC67 when i++ is located within a binary expression --- .../java/checks/IncrementCheck.java | 7 +++++ .../files/IncrementCheckBinaryExpression.java | 31 +++++++++++++++++++ .../java/checks/IncrementCheckTest.java | 8 +++++ 3 files changed, 46 insertions(+) create mode 100644 src/test/files/IncrementCheckBinaryExpression.java diff --git a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java index 00c3094..f6a8afc 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java @@ -22,8 +22,10 @@ import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.tree.BinaryExpressionTree; import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; +import org.sonar.plugins.java.api.tree.UnaryExpressionTree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; @Rule(key = "EC67") @@ -39,6 +41,11 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { + var unaryExpressionTree = (UnaryExpressionTree) tree; + + if (unaryExpressionTree.parent() instanceof BinaryExpressionTree) { + return; + } reportIssue(tree, MESSAGERULE); } } diff --git a/src/test/files/IncrementCheckBinaryExpression.java b/src/test/files/IncrementCheckBinaryExpression.java new file mode 100644 index 0000000..0156303 --- /dev/null +++ b/src/test/files/IncrementCheckBinaryExpression.java @@ -0,0 +1,31 @@ +/* + * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) + * + * 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 . + */ +class MyClass2 { + MyClass(MyClass mc) { + } + void unaryExpressionWithinBinaryExpression() { + var i = 0; + for (int j=0; j < 10; ++j) { + System.out.println("test" + i++); + System.out.println(2 + i++); + System.out.println(2 - i++); + System.out.println(2 * i++); + System.out.println(2 / i++); + } + } +} \ No newline at end of file diff --git a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java b/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java index e9d5b98..edd73cc 100644 --- a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java +++ b/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java @@ -30,4 +30,12 @@ void test() { .verifyIssues(); } + @Test + void incrementCheck_unaryExpressionWithinBinaryExpression_noIssue() { + CheckVerifier.newVerifier() + .onFile("src/test/files/IncrementCheckBinaryExpression.java") + .withCheck(new IncrementCheck()) + .verifyNoIssues(); + } + } \ No newline at end of file From c77a06ff45976cfa1fa662cd322533671754728a Mon Sep 17 00:00:00 2001 From: Hocine Hacherouf Date: Thu, 30 May 2024 14:32:58 +0200 Subject: [PATCH 2/4] Add support of i++ used as a method parameter --- .../java/checks/IncrementCheck.java | 4 ++- src/test/files/IncrementCheckMethod.java | 27 +++++++++++++++++++ .../java/checks/IncrementCheckTest.java | 7 +++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/test/files/IncrementCheckMethod.java diff --git a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java index f6a8afc..4ef2f88 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java @@ -21,6 +21,7 @@ import java.util.List; import org.sonar.check.Rule; +import org.sonar.java.ast.parser.ArgumentListTreeImpl; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.BinaryExpressionTree; import org.sonar.plugins.java.api.tree.Tree; @@ -43,7 +44,8 @@ public List nodesToVisit() { public void visitNode(Tree tree) { var unaryExpressionTree = (UnaryExpressionTree) tree; - if (unaryExpressionTree.parent() instanceof BinaryExpressionTree) { + if (unaryExpressionTree.parent() instanceof BinaryExpressionTree + || unaryExpressionTree.parent() instanceof ArgumentListTreeImpl) { return; } reportIssue(tree, MESSAGERULE); diff --git a/src/test/files/IncrementCheckMethod.java b/src/test/files/IncrementCheckMethod.java new file mode 100644 index 0000000..42e7cb5 --- /dev/null +++ b/src/test/files/IncrementCheckMethod.java @@ -0,0 +1,27 @@ +/* + * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) + * + * 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 . + */ +class MyClass2 { + MyClass(MyClass mc) { + } + void unaryExpressionWithinBinaryExpression() { + var i = 0; + for (int j=0; j < 10; ++j) { + doSomething(i++); + } + } +} \ No newline at end of file diff --git a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java b/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java index edd73cc..49b0c10 100644 --- a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java +++ b/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java @@ -38,4 +38,11 @@ void incrementCheck_unaryExpressionWithinBinaryExpression_noIssue() { .verifyNoIssues(); } + @Test + void incrementCheck_unaryExpressionWithinMethod_noIssue() { + CheckVerifier.newVerifier() + .onFile("src/test/files/IncrementCheckMethod.java") + .withCheck(new IncrementCheck()) + .verifyNoIssues(); + } } \ No newline at end of file From b59cd541a4a71b1658b06b85ef46c44542032b72 Mon Sep 17 00:00:00 2001 From: Hocine Hacherouf Date: Sun, 9 Jun 2024 20:09:45 +0200 Subject: [PATCH 3/4] Update java classes files used in unit tests for EC67 --- src/test/files/IncrementCheckBinaryExpression.java | 4 ++-- src/test/files/IncrementCheckMethod.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/files/IncrementCheckBinaryExpression.java b/src/test/files/IncrementCheckBinaryExpression.java index 0156303..f0b81da 100644 --- a/src/test/files/IncrementCheckBinaryExpression.java +++ b/src/test/files/IncrementCheckBinaryExpression.java @@ -15,8 +15,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class MyClass2 { - MyClass(MyClass mc) { +class IncrementCheckBinaryExpression { + IncrementCheckBinaryExpression(IncrementCheckBinaryExpression mc) { } void unaryExpressionWithinBinaryExpression() { var i = 0; diff --git a/src/test/files/IncrementCheckMethod.java b/src/test/files/IncrementCheckMethod.java index 42e7cb5..e352db5 100644 --- a/src/test/files/IncrementCheckMethod.java +++ b/src/test/files/IncrementCheckMethod.java @@ -15,8 +15,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class MyClass2 { - MyClass(MyClass mc) { +class IncrementCheckMethod { + IncrementCheckMethod(IncrementCheckMethod mc) { } void unaryExpressionWithinBinaryExpression() { var i = 0; From 65a35f7dfbc9b99025e1663cb8319114170a4a28 Mon Sep 17 00:00:00 2001 From: Hocine Hacherouf Date: Sun, 9 Jun 2024 20:10:47 +0200 Subject: [PATCH 4/4] Add entry on CHANGELOG.md for changes on EC67 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce62a8..a3bc1a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- EC67: Ignore rule EC67 when i++ is located within a binary expression or as a method parameter + ### Deleted ## [1.6.1] - 2024-05-15