Skip to content

Commit

Permalink
fix: 🩹 "++i" statement is not so bad
Browse files Browse the repository at this point in the history
In some cases, postfix increment may be intentional.

Closes #4
  • Loading branch information
pataluc committed May 29, 2024
1 parent ee04ea3 commit 8d10d5b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Kind> 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);
}
}
}
6 changes: 6 additions & 0 deletions src/test/files/IncrementCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++);
}
}

0 comments on commit 8d10d5b

Please sign in to comment.