2323import java .util .List ;
2424import java .util .Optional ;
2525import java .util .Set ;
26+ import java .util .stream .Collectors ;
2627import org .sonar .check .Rule ;
2728import org .sonar .plugins .java .api .IssuableSubscriptionVisitor ;
2829import org .sonar .plugins .java .api .JavaFileScannerContext ;
3637import org .sonar .plugins .java .api .tree .TypePatternTree ;
3738import org .sonar .plugins .java .api .tree .VariableTree ;
3839
39-
4040@ Rule (key = "S6878" )
4141public class RecordPatternInsteadOfFieldAccessCheck extends IssuableSubscriptionVisitor implements JavaVersionAwareVisitor {
4242
@@ -97,7 +97,11 @@ private void checkTypePatternVariableUsage(VariableTree patternVariable) {
9797
9898 private static boolean isEveryRecordComponentUsed (Set <MemberSelectExpressionTree > secondaryLocationsTrees , Symbol .TypeSymbol recordSymbol ) {
9999 var recordComponentNames = recordComponentNames (recordSymbol );
100- return !recordComponentNames .isEmpty () && secondaryLocationsTrees .stream ().map (mse -> mse .identifier ().name ()).toList ().containsAll (recordComponentNames );
100+ return !recordComponentNames .isEmpty () &&
101+ secondaryLocationsTrees .stream ()
102+ .map (mse -> mse .identifier ().name ())
103+ .collect (Collectors .toSet ())
104+ .equals (recordComponentNames );
101105 }
102106
103107 private static boolean isNotRecordGetter (MemberSelectExpressionTree mse ) {
@@ -115,14 +119,14 @@ private static boolean isRecordPattern(TypePatternTree typePattern) {
115119 return typePattern .patternVariable ().type ().symbolType ().isSubtypeOf ("java.lang.Record" );
116120 }
117121
118- private static List <String > recordComponentNames (Symbol .TypeSymbol recordSymbol ) {
122+ private static Set <String > recordComponentNames (Symbol .TypeSymbol recordSymbol ) {
119123 return recordSymbol
120124 .memberSymbols ()
121125 .stream ()
122126 .filter (Symbol ::isVariableSymbol )
123127 .map (Symbol .VariableSymbol .class ::cast )
124128 .map (Symbol .VariableSymbol ::name )
125- .toList ( );
129+ .collect ( Collectors . toSet () );
126130 }
127131
128132}
0 commit comments