From 8d10d5b3ebf04d2d27a64b604ca286de1f423a56 Mon Sep 17 00:00:00 2001 From: Luc Fouin Date: Wed, 29 May 2024 15:39:40 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=A9=B9=20"++i"=20statement=20is=20?= =?UTF-8?q?not=20so=20bad?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some cases, postfix increment may be intentional. Closes #4 --- .../greencodeinitiative/java/checks/IncrementCheck.java | 9 +++++++-- src/test/files/IncrementCheck.java | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java index 00c3094..f0c2bae 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java @@ -20,6 +20,8 @@ import java.util.Collections; import java.util.List; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.Tree; @@ -31,14 +33,17 @@ public class IncrementCheck extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Use ++i instead of i++"; + private static final Logger LOGGER = Loggers.get(IncrementCheck.class); @Override public List nodesToVisit() { return Collections.singletonList(Kind.POSTFIX_INCREMENT); } - @Override public void visitNode(Tree tree) { - reportIssue(tree, MESSAGERULE); + LOGGER.debug("Parent node is of Kind: " + tree.parent().toString()); + if (!tree.parent().is(Kind.ARGUMENTS)) { + reportIssue(tree, MESSAGERULE); + } } } diff --git a/src/test/files/IncrementCheck.java b/src/test/files/IncrementCheck.java index 6e7e6c2..26a7f9a 100644 --- a/src/test/files/IncrementCheck.java +++ b/src/test/files/IncrementCheck.java @@ -60,4 +60,10 @@ void foo51(int value) { System.out.println(i); } } + + // Compliant because maybe foo51 needs the incremented value + void foo6(int value) { + int i = 0; + foo51(i++); + } } \ No newline at end of file