Skip to content

Commit a368a19

Browse files
authored
Merge pull request #10 from green-code-initiative/ISSUE_9
[ISSUE 9] correction of no block statement problem
2 parents 1bbe866 + 49dc366 commit a368a19

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
- [#9](https://github.com/green-code-initiative/ecoCode-java/issues/9) EC2 rule : correction no block statement use case
15+
1416
### Deleted
1517

1618
## [1.5.1] - 2024-01-23

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ services:
1616
SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: 'true'
1717
volumes:
1818
- type: bind
19-
source: ./target/ecocode-java-plugin-1.5.1-SNAPSHOT.jar
20-
target: /opt/sonarqube/extensions/plugins/ecocode-java-plugin-1.5.1-SNAPSHOT.jar
19+
source: ./target/ecocode-java-plugin-1.5.2-SNAPSHOT.jar
20+
target: /opt/sonarqube/extensions/plugins/ecocode-java-plugin-1.5.2-SNAPSHOT.jar
2121
- "extensions:/opt/sonarqube/extensions"
2222
- "logs:/opt/sonarqube/logs"
2323
- "data:/opt/sonarqube/data"

src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ private void visitIfNode(IfStatementTree pIfTree, int pLevel) {
114114
// analyze condition variables and raise error if needed
115115
computeIfVariables(pIfTree, pLevel);
116116

117+
// return if there is no block
118+
if (!pIfTree.thenStatement().is(Kind.BLOCK))
119+
return;
120+
117121
// visit the content of if block
118122
visitNodeContent(((BlockTree)pIfTree.thenStatement()).body(), pLevel + 1);
119123

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package fr.greencodeinitiative.java.checks;
19+
20+
class AvoidMultipleIfElseStatementNotBlock {
21+
22+
public boolean equals(Object obj) {
23+
if (this == obj)
24+
return true;
25+
}
26+
27+
}

src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,24 @@ void test() {
2727
.onFile("src/test/files/AvoidMultipleIfElseStatement.java")
2828
.withCheck(new AvoidMultipleIfElseStatement())
2929
.verifyIssues();
30+
CheckVerifier.newVerifier()
31+
.onFile("src/test/files/AvoidMultipleIfElseStatementNoIssue.java")
32+
.withCheck(new AvoidMultipleIfElseStatement())
33+
.verifyNoIssues();
34+
}
35+
36+
@Test
37+
void testInterfaceMethodStatement() {
3038
CheckVerifier.newVerifier()
3139
.onFile("src/test/files/AvoidMultipleIfElseStatementInterface.java")
3240
.withCheck(new AvoidMultipleIfElseStatement())
3341
.verifyNoIssues();
42+
}
43+
44+
@Test
45+
void testNotBlockStatement() {
3446
CheckVerifier.newVerifier()
35-
.onFile("src/test/files/AvoidMultipleIfElseStatementNoIssue.java")
47+
.onFile("src/test/files/AvoidMultipleIfElseStatementNotBlock.java")
3648
.withCheck(new AvoidMultipleIfElseStatement())
3749
.verifyNoIssues();
3850
}

0 commit comments

Comments
 (0)