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 diff --git a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java index 00c3094..4ef2f88 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java @@ -21,9 +21,12 @@ 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; 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 +42,12 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { + var unaryExpressionTree = (UnaryExpressionTree) tree; + + if (unaryExpressionTree.parent() instanceof BinaryExpressionTree + || unaryExpressionTree.parent() instanceof ArgumentListTreeImpl) { + return; + } reportIssue(tree, MESSAGERULE); } } diff --git a/src/test/files/IncrementCheckBinaryExpression.java b/src/test/files/IncrementCheckBinaryExpression.java new file mode 100644 index 0000000..f0b81da --- /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 IncrementCheckBinaryExpression { + IncrementCheckBinaryExpression(IncrementCheckBinaryExpression 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/files/IncrementCheckMethod.java b/src/test/files/IncrementCheckMethod.java new file mode 100644 index 0000000..e352db5 --- /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 IncrementCheckMethod { + IncrementCheckMethod(IncrementCheckMethod 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 e9d5b98..49b0c10 100644 --- a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java +++ b/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java @@ -30,4 +30,19 @@ void test() { .verifyIssues(); } + @Test + void incrementCheck_unaryExpressionWithinBinaryExpression_noIssue() { + CheckVerifier.newVerifier() + .onFile("src/test/files/IncrementCheckBinaryExpression.java") + .withCheck(new IncrementCheck()) + .verifyNoIssues(); + } + + @Test + void incrementCheck_unaryExpressionWithinMethod_noIssue() { + CheckVerifier.newVerifier() + .onFile("src/test/files/IncrementCheckMethod.java") + .withCheck(new IncrementCheck()) + .verifyNoIssues(); + } } \ No newline at end of file