diff --git a/CHANGELOG.md b/CHANGELOG.md index da54c1d..d56235d 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 +- [#9](https://github.com/green-code-initiative/ecoCode-java/issues/9) EC2 rule : correction no block statement use case + ### Deleted ## [1.5.1] - 2024-01-23 diff --git a/docker-compose.yml b/docker-compose.yml index 1782b70..bd44653 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,8 +16,8 @@ services: SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: 'true' volumes: - type: bind - source: ./target/ecocode-java-plugin-1.5.1-SNAPSHOT.jar - target: /opt/sonarqube/extensions/plugins/ecocode-java-plugin-1.5.1-SNAPSHOT.jar + source: ./target/ecocode-java-plugin-1.5.2-SNAPSHOT.jar + target: /opt/sonarqube/extensions/plugins/ecocode-java-plugin-1.5.2-SNAPSHOT.jar - "extensions:/opt/sonarqube/extensions" - "logs:/opt/sonarqube/logs" - "data:/opt/sonarqube/data" diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java b/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java index a855a37..70c6c5e 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java @@ -114,6 +114,10 @@ private void visitIfNode(IfStatementTree pIfTree, int pLevel) { // analyze condition variables and raise error if needed computeIfVariables(pIfTree, pLevel); + // return if there is no block + if (!pIfTree.thenStatement().is(Kind.BLOCK)) + return; + // visit the content of if block visitNodeContent(((BlockTree)pIfTree.thenStatement()).body(), pLevel + 1); diff --git a/src/test/files/AvoidMultipleIfElseStatementNotBlock.java b/src/test/files/AvoidMultipleIfElseStatementNotBlock.java new file mode 100644 index 0000000..3e8f613 --- /dev/null +++ b/src/test/files/AvoidMultipleIfElseStatementNotBlock.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 . + */ +package fr.greencodeinitiative.java.checks; + +class AvoidMultipleIfElseStatementNotBlock { + + public boolean equals(Object obj) { + if (this == obj) + return true; + } + +} \ No newline at end of file diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java b/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java index b478ae2..f3f8ecd 100644 --- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java +++ b/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java @@ -27,12 +27,24 @@ void test() { .onFile("src/test/files/AvoidMultipleIfElseStatement.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyIssues(); + CheckVerifier.newVerifier() + .onFile("src/test/files/AvoidMultipleIfElseStatementNoIssue.java") + .withCheck(new AvoidMultipleIfElseStatement()) + .verifyNoIssues(); + } + + @Test + void testInterfaceMethodStatement() { CheckVerifier.newVerifier() .onFile("src/test/files/AvoidMultipleIfElseStatementInterface.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyNoIssues(); + } + + @Test + void testNotBlockStatement() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidMultipleIfElseStatementNoIssue.java") + .onFile("src/test/files/AvoidMultipleIfElseStatementNotBlock.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyNoIssues(); }