diff --git a/java-checks-test-sources/default/src/main/java/checks/RecordPatternInsteadOfFieldAccessCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/RecordPatternInsteadOfFieldAccessCheckSample.java index 03d151dc825..8bb4936af93 100644 --- a/java-checks-test-sources/default/src/main/java/checks/RecordPatternInsteadOfFieldAccessCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/RecordPatternInsteadOfFieldAccessCheckSample.java @@ -28,7 +28,7 @@ int notAComponentAccessTwice(Object obj){ } int allComponentsPlusAMethod(Object obj){ - if (obj instanceof Point p) { // Noncompliant + if (obj instanceof Point p) { // Compliant, using record pattern would not allow access to p.notAComponent return p.x() + p.y() + p.notAComponent(); } return 0; diff --git a/java-checks/src/main/java/org/sonar/java/checks/RecordPatternInsteadOfFieldAccessCheck.java b/java-checks/src/main/java/org/sonar/java/checks/RecordPatternInsteadOfFieldAccessCheck.java index 30bb4739894..7487b078684 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/RecordPatternInsteadOfFieldAccessCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/RecordPatternInsteadOfFieldAccessCheck.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.JavaFileScannerContext; @@ -36,7 +37,6 @@ import org.sonar.plugins.java.api.tree.TypePatternTree; import org.sonar.plugins.java.api.tree.VariableTree; - @Rule(key = "S6878") public class RecordPatternInsteadOfFieldAccessCheck extends IssuableSubscriptionVisitor implements JavaVersionAwareVisitor { @@ -97,7 +97,11 @@ private void checkTypePatternVariableUsage(VariableTree patternVariable) { private static boolean isEveryRecordComponentUsed(Set secondaryLocationsTrees, Symbol.TypeSymbol recordSymbol) { var recordComponentNames = recordComponentNames(recordSymbol); - return !recordComponentNames.isEmpty() && secondaryLocationsTrees.stream().map(mse -> mse.identifier().name()).toList().containsAll(recordComponentNames); + return !recordComponentNames.isEmpty() && + secondaryLocationsTrees.stream() + .map(mse -> mse.identifier().name()) + .collect(Collectors.toSet()) + .equals(recordComponentNames); } private static boolean isNotRecordGetter(MemberSelectExpressionTree mse) { @@ -115,14 +119,14 @@ private static boolean isRecordPattern(TypePatternTree typePattern) { return typePattern.patternVariable().type().symbolType().isSubtypeOf("java.lang.Record"); } - private static List recordComponentNames(Symbol.TypeSymbol recordSymbol) { + private static Set recordComponentNames(Symbol.TypeSymbol recordSymbol) { return recordSymbol .memberSymbols() .stream() .filter(Symbol::isVariableSymbol) .map(Symbol.VariableSymbol.class::cast) .map(Symbol.VariableSymbol::name) - .toList(); + .collect(Collectors.toSet()); } }