2121import org .sonar .plugins .python .api .PythonSubscriptionCheck ;
2222import org .sonar .plugins .python .api .SubscriptionContext ;
2323import org .sonar .plugins .python .api .quickfix .PythonQuickFix ;
24- import org .sonar .plugins .python .api .symbols .Symbol ;
2524import org .sonar .plugins .python .api .tree .BinaryExpression ;
2625import org .sonar .plugins .python .api .tree .Expression ;
2726import org .sonar .plugins .python .api .tree .Name ;
2827import org .sonar .plugins .python .api .tree .QualifiedExpression ;
2928import org .sonar .plugins .python .api .tree .Tree ;
29+ import org .sonar .plugins .python .api .types .v2 .matchers .TypeMatcher ;
30+ import org .sonar .plugins .python .api .types .v2 .matchers .TypeMatchers ;
3031import org .sonar .python .quickfix .TextEditUtils ;
3132import org .sonar .python .tree .TreeUtils ;
3233
@@ -36,6 +37,7 @@ public class NumpyIsNanCheck extends PythonSubscriptionCheck {
3637 private static final String MESSAGE = "Don't perform an equality/inequality check against \" numpy.nan\" ." ;
3738 private static final String QUICK_FIX_MESSAGE_EQUALITY = "Replace this equality check with \" numpy.isnan()\" ." ;
3839 private static final String QUICK_FIX_MESSAGE_INEQUALITY = "Replace this inequality check with \" not numpy.isnan()\" ." ;
40+ private static final TypeMatcher NUMPY_NAN_MATCHER = TypeMatchers .withFQN ("numpy.nan" );
3941
4042 @ Override
4143 public void initialize (Context context ) {
@@ -53,13 +55,10 @@ private static void checkForIsNan(SubscriptionContext ctx) {
5355 }
5456
5557 private static void checkOperand (SubscriptionContext ctx , Expression operand , Expression otherOperand , BinaryExpression be ) {
56- TreeUtils .getSymbolFromTree (operand )
57- .map (Symbol ::fullyQualifiedName )
58- .filter ("numpy.nan" ::equals )
59- .ifPresent (fqn -> {
60- PreciseIssue issue = ctx .addIssue (be , MESSAGE );
61- addQuickFix (issue , operand , otherOperand , be );
62- });
58+ if (NUMPY_NAN_MATCHER .isTrueFor (operand , ctx )) {
59+ PreciseIssue issue = ctx .addIssue (be , MESSAGE );
60+ addQuickFix (issue , operand , otherOperand , be );
61+ }
6362 }
6463
6564 private static void addQuickFix (PreciseIssue issue , Expression nanOperand , Expression otherOperand , BinaryExpression be ) {
0 commit comments