2222import org .sonar .check .Rule ;
2323import org .sonar .plugins .python .api .PythonSubscriptionCheck ;
2424import org .sonar .plugins .python .api .SubscriptionContext ;
25- import org .sonar .plugins .python .api .symbols .Symbol ;
2625import org .sonar .plugins .python .api .tree .Argument ;
2726import org .sonar .plugins .python .api .tree .CallExpression ;
2827import org .sonar .plugins .python .api .tree .Expression ;
29- import org .sonar .plugins .python .api .tree .HasSymbol ;
3028import org .sonar .plugins .python .api .tree .Name ;
3129import org .sonar .plugins .python .api .tree .RegularArgument ;
3230import org .sonar .plugins .python .api .tree .Tree ;
31+ import org .sonar .plugins .python .api .types .v2 .matchers .TypeMatcher ;
32+ import org .sonar .plugins .python .api .types .v2 .matchers .TypeMatchers ;
3333import org .sonar .python .tree .TreeUtils ;
3434
3535@ Rule (key = "S6714" )
3636public class NumpyListOverGeneratorCheck extends PythonSubscriptionCheck {
3737
3838 public static final String MESSAGE = "Pass a list to \" np.array\" instead of passing a generator." ;
3939
40+ private static final TypeMatcher IS_NUMPY_ARRAY = TypeMatchers .withFQN ("numpy.array" );
41+ private static final TypeMatcher IS_OBJECT_TYPE = TypeMatchers .isType ("builtins.object" );
42+
4043 @ Override
4144 public void initialize (Context context ) {
42- context .registerSyntaxNodeConsumer (Tree .Kind .CALL_EXPR , this ::checkNumpyArrayCall );
45+ context .registerSyntaxNodeConsumer (Tree .Kind .CALL_EXPR , NumpyListOverGeneratorCheck ::checkNumpyArrayCall );
4346 }
4447
45- private void checkNumpyArrayCall (SubscriptionContext ctx ) {
48+ private static void checkNumpyArrayCall (SubscriptionContext ctx ) {
4649 CallExpression call = (CallExpression ) ctx .syntaxNode ();
47- Optional .ofNullable (call .calleeSymbol ())
48- .map (Symbol ::fullyQualifiedName )
49- .filter ("numpy.array" ::equals )
50- .ifPresent (fqn -> checkGeneratorCallee (call , ctx ));
50+ if (IS_NUMPY_ARRAY .isTrueFor (call .callee (), ctx )) {
51+ checkGeneratorCallee (call , ctx );
52+ }
5153 }
5254
5355 private static void checkGeneratorCallee (CallExpression call , SubscriptionContext ctx ) {
@@ -65,13 +67,8 @@ private static void checkGeneratorCallee(CallExpression call, SubscriptionContex
6567 return ;
6668 }
6769
68- if (Optional .ofNullable (TreeUtils .nthArgumentOrKeyword (1 , "dtype" , argList ))
69- .filter (regArg -> regArg .expression ().is (Tree .Kind .NAME ))
70- .map (regArg -> (Name ) regArg .expression ())
71- .map (HasSymbol ::symbol )
72- .map (Symbol ::fullyQualifiedName )
73- .filter ("object" ::equals )
74- .isEmpty ()) {
70+ RegularArgument dtypeArg = TreeUtils .nthArgumentOrKeyword (1 , "dtype" , argList );
71+ if (dtypeArg == null || !IS_OBJECT_TYPE .isTrueFor (dtypeArg .expression (), ctx )) {
7572 ctx .addIssue (call , MESSAGE );
7673 }
7774 }
0 commit comments