Skip to content

Commit ff70054

Browse files
committed
Add floating-point-assertEquals-with-integral-values.ql
1 parent b67e46b commit ff70054

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Finds usage of `assertEquals` and `assertNotEquals` methods with floating point delta
3+
* parameter which are used for integral number arguments.
4+
* For example `assertEquals(double, double, double)`:
5+
* ```java
6+
* assertEquals(expectedCount, obj.getCount(), 0);
7+
* ```
8+
* When the compared arguments are integral numbers the regular `assertEquals` methods should
9+
* be used, such as `assertEquals(int, int)`.
10+
*
11+
* @kind problem
12+
*/
13+
14+
import java
15+
16+
// TODO: Maybe use classes from AssertLib.qll?
17+
from MethodAccess assertCall, Method assertMethod, int argStartIndex, CompileTimeConstantExpr deltaArg
18+
where
19+
assertMethod = assertCall.getMethod()
20+
and assertMethod.hasName(["assertEquals", "assertNotEquals"])
21+
// Use argStartIndex variable to skip over message parameter, in case there is one
22+
and assertMethod.getParameterType(argStartIndex) instanceof FloatingPointType
23+
and assertMethod.getParameterType(argStartIndex + 1) instanceof FloatingPointType
24+
and assertMethod.getParameterType(argStartIndex + 2) instanceof FloatingPointType
25+
and assertCall.getArgument(argStartIndex).getType() instanceof IntegralType
26+
and assertCall.getArgument(argStartIndex + 1).getType() instanceof IntegralType
27+
// Check delta argument to reduce false positives for intentional usage of this assertion method
28+
and deltaArg = assertCall.getArgument(argStartIndex + 2)
29+
and (
30+
deltaArg.getIntValue() = 0
31+
or deltaArg.(LongLiteral).getValue() = "0"
32+
or deltaArg.(FloatLiteral).getFloatValue() < 1
33+
or deltaArg.(DoubleLiteral).getDoubleValue() < 1
34+
)
35+
select assertCall, "Should not use floating point assertion method for integral arguments"

0 commit comments

Comments
 (0)