From 00f21321eda498cb6c3570a3e9b12d2be8ec6bc1 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Wed, 24 Jan 2024 23:36:55 +0100 Subject: [PATCH 1/3] [ISSUE 9] correction of no block statement problem --- .../checks/AvoidMultipleIfElseStatement.java | 4 +++ .../AvoidMultipleIfElseStatementNotBlock.java | 27 +++++++++++++++++++ .../AvoidMultipleIfElseStatementTest.java | 14 +++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/test/files/AvoidMultipleIfElseStatementNotBlock.java 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(); } From dab0b93fe8519a6a4ca986201b691579f0b8b0f3 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Wed, 24 Jan 2024 23:40:58 +0100 Subject: [PATCH 2/3] [ISSUE 9] correction of no block statement problem - update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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 From 49dc366b8624973a37115ae4e192307b2040ed58 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Thu, 25 Jan 2024 21:46:08 +0100 Subject: [PATCH 3/3] [ISSUE 9] correction of no block statement problem - update docker-compose --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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"