Skip to content
Open
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- [#119](https://github.com/green-code-initiative/creedengo-java/issues/119) GCI94 - reduce false positives: rule no longer flags `orElse()` when argument is a constant, literal, static field or null; detection extended to Optional variables (semantic type check) and to computed arguments nested inside concatenation, ternary or object instantiation
- [#122](https://github.com/green-code-initiative/creedengo-java/issues/122) resolve bug : final keyword is not recognized when using instanceof pattern
- [#69](https://github.com/green-code-initiative/creedengo-java/issues/69) correction of NullPointer in GCI79 rule + technical refactoring of GCI79
- update integration tests system to use the new component "creedengo-integration-test"
- compatibility updates for SonarQube up to 26.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ void testGCI82() {
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI82/MakeNonReassignedVariablesConstants.java";
String ruleId = "creedengo-java:GCI82";
String ruleMsg = "The variable is never reassigned and can be 'final'";
int[] startLines = new int[]{9, 14, 15, 20, 26, 29, 48, 75, 108, 121, 146};
int[] endLines = new int[]{9, 14, 15, 20, 26, 29, 48, 75, 108, 121, 146};
int[] startLines = new int[]{9, 14, 15, 20, 26, 29, 48, 75, 108, 121, 136, 179};
int[] endLines = new int[]{9, 14, 15, 20, 26, 29, 48, 75, 108, 121, 136, 179};

checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,39 @@ void reassignedInConstructor(){
o = new notReassignedInConstructorNotFinal(this.varDefinedInClassNotReassignedInConstructor);
}


public String nonReasignedVariableWithPatternInstanceOfShouldBeNonCompliant() {
final Object o = "NON-COMPLIANT";

if (o instanceof String var) { // Noncompliant {{The variable is never reassigned and can be 'final'}}
return var;
}

return "";
}


public String nonReasignedVariableWithPatternInstanceOfWithFinalShouldBeCompliant() {
final Object o = "COMPLIANT";

if (o instanceof final String var) { // Compliant : here final keyword should be recognized and not trigger the rule
return var;
}

return "";
}

public String reasignedVariableWithPatternInstanceOfShouldBeCompliant() {
final Object o = "COMPLIANT";

if (o instanceof String var) { // Compliant : Variable is reassigned
var = "REASSIGN";
return var;
}

return "";
}

}

class reassignedInConstructor{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void visitNode(@Nonnull Tree tree) {
LOGGER.debug(" => isNotReassigned = {}", isNotReassigned(variableTree));
LOGGER.debug(" => isPassedAsNonFinalParameter = {}", isPassedAsNonFinalParameter(variableTree));
}

if (isNotFinalAndNotStatic(variableTree) && isNotReassigned(variableTree)) {
reportIssue(tree, MESSAGE_RULE);
} else {
Expand Down Expand Up @@ -112,20 +113,7 @@ private static boolean parentIsKind(Tree tree, Kind... orKind) {
}

private static boolean isNotFinalAndNotStatic(VariableTree variableTree) {
return hasNoneOf(variableTree.modifiers(), Modifier.FINAL, Modifier.STATIC);
}

private static boolean hasNoneOf(ModifiersTree modifiersTree, Modifier... unexpectedModifiers) {
return !hasAnyOf(modifiersTree, unexpectedModifiers);
}

private static boolean hasAnyOf(ModifiersTree modifiersTree, Modifier... expectedModifiers) {
for(Modifier expectedModifier : expectedModifiers) {
if (hasModifier(modifiersTree, expectedModifier)) {
return true;
}
}
return false;
return !variableTree.symbol().isFinal() && !variableTree.symbol().isStatic(); // use symbol instead of modifiers since in case of type_pattern modifiers is always empty
}

public static boolean hasModifier(ModifiersTree modifiersTree, Modifier expectedModifier) {
Expand Down