Skip to content

Commit b623c94

Browse files
author
Morille, Amelie (ITDV XF) - AF
committed
fix(GCI82): update GCI82 rule to handle record types and adjust test cases
1 parent 7c576c4 commit b623c94

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- upgrade libraries versions
1616
- correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library)
1717
- upgrade JDK from 11 to 17
18+
- fix GCI82 rule to handle record types and adjust test cases
1819

1920
### Deleted
2021

src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ void testGCI82() {
440440
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java";
441441
String ruleId = "creedengo-java:GCI82";
442442
String ruleMsg = "The variable is never reassigned and can be 'final'";
443-
int[] startLines = new int[]{7, 12, 13, 45};
444-
int[] endLines = new int[]{7, 12, 13, 45};
443+
int[] startLines = new int[]{7, 17, 18 ,50};
444+
int[] endLines = new int[]{7, 17, 18 ,50};
445445

446446
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines);
447447
}

src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class MakeNonReassignedVariablesConstants {
88
private Object myNonFinalAndReassignedObject = new Object(); // Compliant
99
private final Object myFinalAndNotReassignedObject = new Object(); // Compliant
1010

11+
private record myRecord(
12+
String myImplicitlyFinalStringField, // Compliant
13+
Integer myImplicitlyFinalIntField) // Compliant
14+
{ }
15+
1116
private static final String CONSTANT = "toto"; // Compliant
1217
private String varDefinedInClassNotReassigned = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
1318
private String varDefinedInClassNotUsed = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
@@ -66,4 +71,4 @@ void classVariableReassignedBis() {
6671
logger.info(myFinalAndNotReassignedObject.toString());
6772
}
6873

69-
}
74+
}

src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void visitNode(@Nonnull Tree tree) {
3030
LOGGER.debug(" => usages = " + variableTree.symbol().usages().size());
3131
LOGGER.debug(" => isNotReassigned = " + isNotReassigned(variableTree));
3232

33-
if (isNotFinalAndNotStatic(variableTree) && isNotReassigned(variableTree)) {
33+
if (isNotFromRecord(variableTree) && isNotFinalAndNotStatic(variableTree) && isNotReassigned(variableTree)) {
3434
reportIssue(tree, MESSAGE_RULE);
3535
} else {
3636
super.visitNode(tree);
@@ -80,6 +80,13 @@ private static boolean isNotFinalAndNotStatic(VariableTree variableTree) {
8080
return hasNoneOf(variableTree.modifiers(), Modifier.FINAL, Modifier.STATIC);
8181
}
8282

83+
private static boolean isNotFromRecord(VariableTree variableTree) {
84+
Tree parent = variableTree.parent();
85+
if (parent == null) return false;
86+
87+
return !parent.is(Kind.RECORD);
88+
}
89+
8390
private static boolean hasNoneOf(ModifiersTree modifiersTree, Modifier... unexpectedModifiers) {
8491
return !hasAnyOf(modifiersTree, unexpectedModifiers);
8592
}

src/test/files/MakeNonReassignedVariablesConstants.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class MakeNonReassignedVariablesConstants {
88
private Object myNonFinalAndReassignedObject = new Object(); // Compliant
99
private final Object myFinalAndNotReassignedObject = new Object(); // Compliant
1010

11+
private record myRecord(
12+
String myImplicitlyFinalStringField, // Compliant
13+
Integer myImplicitlyFinalIntField) // Compliant
14+
{ }
15+
1116
private static final String CONSTANT = "toto"; // Compliant
1217
private String varDefinedInClassNotReassigned = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
1318
private String varDefinedInClassNotUsed = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}}
@@ -66,4 +71,4 @@ void classVariableReassignedBis() {
6671
logger.info(myFinalAndNotReassignedObject.toString());
6772
}
6873

69-
}
74+
}

src/test/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstantsTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@ void test() {
2929
.withCheck(new MakeNonReassignedVariablesConstants())
3030
.verifyIssues();
3131
}
32-
3332
}

0 commit comments

Comments
 (0)