Skip to content

fix(GCI82): fix rule to handle record types and adjust test cases (#104) #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library)
- upgrade JDK from 11 to 17
- [#4](https://github.com/green-code-initiative/creedengo-java/issues/4) Improvement: "++i" statement is not so bad
- [#105](https://github.com/green-code-initiative/creedengo-java/pull/105) fix GCI82 rule to handle record types and adjust test cases

### Deleted

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ void testGCI82() {
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java";
String ruleId = "creedengo-java:GCI82";
String ruleMsg = "The variable is never reassigned and can be 'final'";
int[] startLines = new int[]{7, 12, 13, 45};
int[] endLines = new int[]{7, 12, 13, 45};
int[] startLines = new int[]{7, 17, 18 ,50};
int[] endLines = new int[]{7, 17, 18 ,50};

checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class MakeNonReassignedVariablesConstants {
private Object myNonFinalAndReassignedObject = new Object(); // Compliant
private final Object myFinalAndNotReassignedObject = new Object(); // Compliant

private record myRecord(
String myImplicitlyFinalStringField, // Compliant
Integer myImplicitlyFinalIntField) // Compliant
{ }

private static final String CONSTANT = "toto"; // Compliant
private String varDefinedInClassNotReassigned = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
private String varDefinedInClassNotUsed = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
Expand Down Expand Up @@ -66,4 +71,4 @@ void classVariableReassignedBis() {
logger.info(myFinalAndNotReassignedObject.toString());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void visitNode(@Nonnull Tree tree) {
LOGGER.debug(" => usages = " + variableTree.symbol().usages().size());
LOGGER.debug(" => isNotReassigned = " + isNotReassigned(variableTree));

if (isNotFinalAndNotStatic(variableTree) && isNotReassigned(variableTree)) {
if (isNotFromRecord(variableTree) && isNotFinalAndNotStatic(variableTree) && isNotReassigned(variableTree)) {
reportIssue(tree, MESSAGE_RULE);
} else {
super.visitNode(tree);
Expand Down Expand Up @@ -80,6 +80,13 @@ private static boolean isNotFinalAndNotStatic(VariableTree variableTree) {
return hasNoneOf(variableTree.modifiers(), Modifier.FINAL, Modifier.STATIC);
}

private static boolean isNotFromRecord(VariableTree variableTree) {
Tree parent = variableTree.parent();
if (parent == null) return false;

return !parent.is(Kind.RECORD);
}

private static boolean hasNoneOf(ModifiersTree modifiersTree, Modifier... unexpectedModifiers) {
return !hasAnyOf(modifiersTree, unexpectedModifiers);
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/files/MakeNonReassignedVariablesConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class MakeNonReassignedVariablesConstants {
private Object myNonFinalAndReassignedObject = new Object(); // Compliant
private final Object myFinalAndNotReassignedObject = new Object(); // Compliant

private record myRecord(
String myImplicitlyFinalStringField, // Compliant
Integer myImplicitlyFinalIntField) // Compliant
{ }

private static final String CONSTANT = "toto"; // Compliant
private String varDefinedInClassNotReassigned = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
private String varDefinedInClassNotUsed = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
Expand Down Expand Up @@ -66,4 +71,4 @@ void classVariableReassignedBis() {
logger.info(myFinalAndNotReassignedObject.toString());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ void test() {
.withCheck(new MakeNonReassignedVariablesConstants())
.verifyIssues();
}

}