Fixed #3954 missing reporting of issue on records.

This commit is contained in:
Per Abich
2022-05-08 12:45:40 +02:00
parent 5657559026
commit 418fe220b9
2 changed files with 21 additions and 1 deletions

View File

@ -115,7 +115,13 @@ public class UseCollectionIsEmptyRule extends AbstractInefficientZeroCheck {
classOrEnumBody = expr.getFirstParentOfType(ASTEnumBody.class);
}
if (classOrEnumBody == null) {
classOrEnumBody = expr.getFirstParentOfType(ASTRecordBody.class);
classOrEnumBody = expr.getFirstParentOfType(ASTRecordDeclaration.class);
List<ASTVariableDeclaratorId> descendantsOfType = classOrEnumBody.findDescendantsOfType(ASTVariableDeclaratorId.class);
for (ASTVariableDeclaratorId variableDeclaratorId : descendantsOfType) {
if (variableDeclaratorId.getName().equals(varName)) {
return variableDeclaratorId.getTypeNode().getTypeDefinition();
}
}
}
List<ASTVariableDeclarator> varDeclarators = classOrEnumBody.findDescendantsOfType(ASTVariableDeclarator.class);
for (ASTVariableDeclarator varDeclarator : varDeclarators) {

View File

@ -500,6 +500,20 @@ public record Record(Set<String> stringSet) {
public boolean hasMoreThanOneItem() {
return this.stringSet.size() > 1;
}
} ]]></code>
</test-code>
<test-code>
<description>With records and size check</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<code><![CDATA[
import java.util.Set;
public record Record(Set<String> stringSet) {
public boolean hasMoreThanOneItem() {
return this.stringSet.size() == 0;
}
} ]]></code>
</test-code>
</test-data>