22
22
import java .util .Arrays ;
23
23
import java .util .List ;
24
24
import java .util .Set ;
25
-
26
25
import org .sonar .check .Rule ;
27
26
import org .sonar .plugins .python .api .PythonSubscriptionCheck ;
28
27
import org .sonar .plugins .python .api .SubscriptionContext ;
37
36
import org .sonar .python .cfg .fixpoint .ReachingDefinitionsAnalysis ;
38
37
import org .sonar .python .quickfix .TextEditUtils ;
39
38
import org .sonar .python .tree .TreeUtils ;
40
- import org .sonar .python .types .InferredTypes ;
39
+ import org .sonar .python .types .v2 .TriBool ;
40
+ import org .sonar .python .types .v2 .TypeChecker ;
41
41
42
42
@ Rule (key = "S1244" )
43
43
public class FloatingPointEqualityCheck extends PythonSubscriptionCheck {
@@ -60,6 +60,7 @@ public class FloatingPointEqualityCheck extends PythonSubscriptionCheck {
60
60
private String importedModuleForIsClose ;
61
61
private Name importedAlias ;
62
62
private boolean isMathImported = false ;
63
+ private TypeChecker typeChecker ;
63
64
64
65
@ Override
65
66
public void initialize (Context context ) {
@@ -75,6 +76,7 @@ private void initializeAnalysis(SubscriptionContext ctx) {
75
76
reachingDefinitionsAnalysis = new ReachingDefinitionsAnalysis (ctx .pythonFile ());
76
77
importedModuleForIsClose = null ;
77
78
importedAlias = null ;
79
+ typeChecker = ctx .typeChecker ();
78
80
}
79
81
80
82
private void checkFloatingPointEquality (SubscriptionContext ctx ) {
@@ -95,15 +97,16 @@ private boolean isAnyOperandFloatingPoint(BinaryExpression binaryExpression) {
95
97
isBinaryOperationWithFloat (leftOperand ) || isBinaryOperationWithFloat (rightOperand );
96
98
}
97
99
98
- private static boolean isFloat (Expression expression ) {
99
- return expression .is (Tree .Kind .NUMERIC_LITERAL ) && expression .type ().equals (InferredTypes .FLOAT );
100
+ private boolean isFloat (Expression expression ) {
101
+ TriBool isTypeFloat = typeChecker .typeCheckBuilder ().isBuiltinWithName ("float" ).check (expression .typeV2 ());
102
+ return expression .is (Tree .Kind .NUMERIC_LITERAL ) && isTypeFloat == TriBool .TRUE ;
100
103
}
101
104
102
105
private boolean isAssignedFloat (Expression expression ) {
103
106
if (expression .is (Tree .Kind .NAME )) {
104
107
Set <Expression > values = reachingDefinitionsAnalysis .valuesAtLocation ((Name ) expression );
105
108
if (!values .isEmpty ()) {
106
- return values .stream ().allMatch (value -> isFloat ( value ) );
109
+ return values .stream ().allMatch (this :: isFloat );
107
110
}
108
111
}
109
112
return false ;
0 commit comments