Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ecocode challenge 2024/Hafnium] Improve rule EC67 : Handle case when i++ is located within a binary expression or a method parameter #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -39,6 +42,12 @@ public List<Kind> nodesToVisit() {

@Override
public void visitNode(Tree tree) {
var unaryExpressionTree = (UnaryExpressionTree) tree;

if (unaryExpressionTree.parent() instanceof BinaryExpressionTree
|| unaryExpressionTree.parent() instanceof ArgumentListTreeImpl) {
return;
}
reportIssue(tree, MESSAGERULE);
}
}
31 changes: 31 additions & 0 deletions src/test/files/IncrementCheckBinaryExpression.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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++);
}
}
}
27 changes: 27 additions & 0 deletions src/test/files/IncrementCheckMethod.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
class IncrementCheckMethod {
IncrementCheckMethod(IncrementCheckMethod mc) {
}
void unaryExpressionWithinBinaryExpression() {
var i = 0;
for (int j=0; j < 10; ++j) {
doSomething(i++);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}