|
5 | 5 | *
|
6 | 6 | * The following shows some examples which can be simplified:
|
7 | 7 | * - `-x == -y` → `x == y`
|
| 8 | + * - `x - y > 0` → `x > y` |
8 | 9 | * - `x + 1 < 4` → `x < 3`
|
9 | 10 | * - `x - 1 > 0` → `x > 1`
|
10 | 11 | * - `x * 2 > 4` → `x > 2`
|
|
13 | 14 | * However, there can be cases where the verbose expression reported by
|
14 | 15 | * this query is desired to make the intention clearer, for example
|
15 | 16 | * `index + 1` to indicate an increment of `index` in the subsequent lines.
|
| 17 | + * |
| 18 | + * @id todo |
| 19 | + * @kind problem |
16 | 20 | */
|
17 | 21 |
|
18 | 22 | import java
|
| 23 | +import lib.Expressions |
19 | 24 | import lib.Literals
|
20 | 25 |
|
21 | 26 | class ComparisonOrEqualityTest extends BinaryExpr {
|
|
39 | 44 | and simplifiableExpr.getRightOperand() instanceof Negated
|
40 | 45 | and action = "Remove negation from both operands"
|
41 | 46 | )
|
| 47 | + or exists(SubExpr subExpr, int compared, boolean equalOrGreater, string recommendedCmpOp | |
| 48 | + comparesWithConstant(simplifiableExpr, subExpr, compared, equalOrGreater) |
| 49 | + and ( |
| 50 | + recommendedCmpOp = ">=" |
| 51 | + // compares `>= 0` |
| 52 | + and compared = 0 |
| 53 | + and equalOrGreater = true |
| 54 | + or |
| 55 | + recommendedCmpOp = ">" |
| 56 | + // compares `>= 1` |
| 57 | + and compared = 1 |
| 58 | + and equalOrGreater = true |
| 59 | + // ignore floating point values because it can have results between 0 and 1 |
| 60 | + and subExpr.getType() instanceof IntegralType |
| 61 | + or |
| 62 | + recommendedCmpOp = "<" |
| 63 | + // compares `< 0` |
| 64 | + and compared = 0 |
| 65 | + and equalOrGreater = false |
| 66 | + or |
| 67 | + recommendedCmpOp = "<=" |
| 68 | + // compares `< 1` |
| 69 | + and compared = 1 |
| 70 | + and equalOrGreater = false |
| 71 | + // ignore floating point values because it can have results between 0 and 1 |
| 72 | + and subExpr.getType() instanceof IntegralType |
| 73 | + ) |
| 74 | + | |
| 75 | + action = "Remove subtraction and directly compare values: `a " + recommendedCmpOp + " b`" |
| 76 | + ) |
42 | 77 | or exists(Literal literalOperand, BinaryExpr simplifiableOperand, Literal removableLiteral |
|
43 | 78 | literalOperand = simplifiableExpr.getAnOperand()
|
44 | 79 | and simplifiableOperand = simplifiableExpr.getAnOperand()
|
|
0 commit comments