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

67 java #44

Open
wants to merge 3 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

- [#4](https://github.com/green-code-initiative/ecoCode-java/issues/4) Improvement: "++i" statement is not so bad

### Deleted

## [1.6.1] - 2024-05-15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public List<Kind> nodesToVisit() {

@Override
public void visitNode(Tree tree) {
reportIssue(tree, MESSAGERULE);
if (tree.parent().is(Kind.EXPRESSION_STATEMENT)) {
reportIssue(tree, MESSAGERULE);
}
}
}
51 changes: 38 additions & 13 deletions src/test/files/IncrementCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,74 @@
* 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 MyClass {
MyClass(MyClass mc) {
class IncrementCheck {
IncrementCheck(IncrementCheck mc) {
}

int foo1() {
int counter = 0;
return counter++; // Noncompliant {{Use ++i instead of i++}}
return counter++; // Compliant but should raise a java:S2123 and a java:S1854
}

private int j = 0;
int foo10() {
return this.j++; // Compliant because maybe the use case needs to return j AND increment it
}

int foo11() {
int counter = 0;
return ++counter;
}

void foo2(int value) {
int foo2() {
int counter = 0;
counter++; // Noncompliant {{Use ++i instead of i++}}
return counter;
}

void foo22(int value) {
int foo22() {
int counter = 0;
++counter;
return counter;
}

void foo3(int value) {
int foo3() {
int counter = 0;
counter = counter + 197845 ;
return counter;
}

void foo4(int value) {
int counter =0;
int foo4() {
int counter = 0;
counter = counter + 35 + 78 ;
return counter;
}

void foo50(int value) {
void foo50() {
for (int i=0; i < 10; i++) { // Noncompliant {{Use ++i instead of i++}}
System.out.println(i);
System.out.println(i); //NOSONAR
}
}

void foo51(int value) {
void foo51() {
for (int i=0; i < 10; ++i) {
System.out.println(i);
System.out.println(i); //NOSONAR
}
}

void bar61(int value) {
// For test purpose
}

int foo61() {
int i = 0;
bar61(i++); // Compliant because maybe bar61 needs the unincremented value
return i;
}

int foo62() {
int i = 0;
bar61(2 + i++); // Compliant because maybe bar61 needs the unincremented value
return i;
}
}
Loading