Skip to content

Commit 34d47db

Browse files
joke1196sonartech
authored andcommitted
SONARPY-4928 Migrate S6725 to TypeV2
GitOrigin-RevId: cc98e89eacb5ce22bcac1a3c89f517c03e52275b
1 parent 7396b3f commit 34d47db

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

‎python-checks/src/main/java/org/sonar/python/checks/NumpyIsNanCheck.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
2222
import org.sonar.plugins.python.api.SubscriptionContext;
2323
import org.sonar.plugins.python.api.quickfix.PythonQuickFix;
24-
import org.sonar.plugins.python.api.symbols.Symbol;
2524
import org.sonar.plugins.python.api.tree.BinaryExpression;
2625
import org.sonar.plugins.python.api.tree.Expression;
2726
import org.sonar.plugins.python.api.tree.Name;
2827
import org.sonar.plugins.python.api.tree.QualifiedExpression;
2928
import 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;
3031
import org.sonar.python.quickfix.TextEditUtils;
3132
import 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

Comments
 (0)