From 4c60448062fe1b7918bffc9a194f862ffcfefdd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Boss=C3=A9?= Date: Fri, 6 Oct 2023 16:45:29 +0200 Subject: [PATCH 1/2] [EC63] UnnecessarilyAssignValuesToVariables rule on caught exception --- .../checks/UnnecessarilyAssignValuesToVariables.java | 2 +- ...UnnecessarilyAssignValuesToVariablesTestCheck.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java index f5544d2e5..12b25d758 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java @@ -75,7 +75,7 @@ private void reportIfUnknow(String name, Tree tree) { private class GetVariableVisitor extends BaseTreeVisitor { @Override public void visitVariable(VariableTree tree) { - if (!tree.parent().is(Kind.METHOD)) { + if (!tree.parent().is(Kind.METHOD) && !tree.parent().is(Kind.CATCH)) { variableList.put(tree.simpleName().name(), tree); } super.visitVariable(tree); diff --git a/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java b/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java index 17eb069a5..c8ed31060 100644 --- a/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java +++ b/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java @@ -92,4 +92,13 @@ public void testNonCompliantThrow() throws Exception { public void testCompliantThrow() throws Exception { throw new Exception("dummy"); } -} \ No newline at end of file + + public void testCompliantCatchException() throws Exception { + try { + int i = 1; + System.out.println(i); + } catch (Exception e) { + System.out.println("Something went wrong."); + } + } +} From bce05cf2b6396e7c6ef47b09b07fccb42e0298d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Boss=C3=A9?= Date: Fri, 20 Oct 2023 10:30:07 +0200 Subject: [PATCH 2/2] [EC63] UnnecessarilyAssignValuesToVariables rule on caught exception - update changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96d824280..0db25fa90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#225](https://github.com/green-code-initiative/ecoCode/pull/225) Upgrade licence system and licence headers of Java files - [#140](https://github.com/green-code-initiative/ecoCode/issues/140) Upgrade rule EC3 for Python : no implementation possible for python - [#136](https://github.com/green-code-initiative/ecoCode/issues/136) Upgrade rule EC53 for Python : no implementation possible for python +- [#228](https://github.com/green-code-initiative/ecoCode/issues/228) Upgrade rule EC63 for java: Stop reporting caught exception even if unused ### Deleted