Skip to content

Commit

Permalink
Merge pull request #10 from green-code-initiative/ISSUE_9
Browse files Browse the repository at this point in the history
[ISSUE 9] correction of no block statement problem
  • Loading branch information
dedece35 authored Jan 25, 2024
2 parents 1bbe866 + 49dc366 commit a368a19
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
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

- [#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
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
27 changes: 27 additions & 0 deletions src/test/files/AvoidMultipleIfElseStatementNotBlock.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;

class AvoidMultipleIfElseStatementNotBlock {

public boolean equals(Object obj) {
if (this == obj)
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit a368a19

Please sign in to comment.