Skip to content

Commit cbf1c4f

Browse files
committed
C#: Check that both operands are booleans when suggesting re-writes for equality, logical and conditional operations.
1 parent fd116f2 commit cbf1c4f

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

‎csharp/ql/src/Language Abuse/SimplifyBoolExpr.ql‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,39 @@ predicate conditionalThenAndElseLiteral(ConditionalExpr cond, boolean thenValue,
6161
}
6262

6363
predicate rewriteBinaryExpr(BinaryOperation op, boolean value, string oldPattern) {
64-
binaryLiteralLeft(op, value) and oldPattern = value + " " + op.getOperator() + " A"
65-
or
66-
binaryLiteralRight(op, value) and oldPattern = "A " + op.getOperator() + " " + value
64+
op.getLeftOperand().getType() instanceof BoolType and
65+
op.getRightOperand().getType() instanceof BoolType and
66+
(
67+
binaryLiteralLeft(op, value) and oldPattern = value + " " + op.getOperator() + " A"
68+
or
69+
binaryLiteralRight(op, value) and oldPattern = "A " + op.getOperator() + " " + value
70+
)
6771
}
6872

6973
predicate rewriteConditionalExpr(ConditionalExpr cond, string oldPattern, string newPattern) {
70-
conditionalThenLiteral(cond, false) and oldPattern = "A ? false : B" and newPattern = "!A && B"
71-
or
72-
conditionalThenLiteral(cond, true) and oldPattern = "A ? true : B" and newPattern = "A || B"
73-
or
74-
conditionalElseLiteral(cond, false) and oldPattern = "A ? B : false" and newPattern = "A && B"
75-
or
76-
conditionalElseLiteral(cond, true) and oldPattern = "A ? B : true" and newPattern = "!A || B"
77-
or
78-
exists(boolean b | conditionalThenAndElseLiteral(cond, b, b) |
79-
oldPattern = "A ? " + b + " : " + b and newPattern = b.toString()
74+
cond.getThen().getType() instanceof BoolType and
75+
cond.getElse().getType() instanceof BoolType and
76+
(
77+
conditionalThenLiteral(cond, false) and oldPattern = "A ? false : B" and newPattern = "!A && B"
78+
or
79+
conditionalThenLiteral(cond, true) and oldPattern = "A ? true : B" and newPattern = "A || B"
80+
or
81+
conditionalElseLiteral(cond, false) and oldPattern = "A ? B : false" and newPattern = "A && B"
82+
or
83+
conditionalElseLiteral(cond, true) and oldPattern = "A ? B : true" and newPattern = "!A || B"
84+
or
85+
exists(boolean b | conditionalThenAndElseLiteral(cond, b, b) |
86+
oldPattern = "A ? " + b + " : " + b and newPattern = b.toString()
87+
)
88+
or
89+
conditionalThenAndElseLiteral(cond, true, false) and
90+
oldPattern = "A ? true : false" and
91+
newPattern = "A"
92+
or
93+
conditionalThenAndElseLiteral(cond, false, true) and
94+
oldPattern = "A ? false : true" and
95+
newPattern = "!A"
8096
)
81-
or
82-
conditionalThenAndElseLiteral(cond, true, false) and
83-
oldPattern = "A ? true : false" and
84-
newPattern = "A"
85-
or
86-
conditionalThenAndElseLiteral(cond, false, true) and
87-
oldPattern = "A ? false : true" and
88-
newPattern = "!A"
8997
}
9098

9199
predicate negatedOperators(string op, string negated) {

0 commit comments

Comments
 (0)