Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
C#: Check that both operands are booleans when suggesting re-writes f…
…or equality, logical and conditional operations.
  • Loading branch information
michaelnebel committed Sep 30, 2026
commit cbf1c4ff5e3ce192eec5d995006f4d5036aba83f
50 changes: 29 additions & 21 deletions csharp/ql/src/Language Abuse/SimplifyBoolExpr.ql
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,39 @@ predicate conditionalThenAndElseLiteral(ConditionalExpr cond, boolean thenValue,
}

predicate rewriteBinaryExpr(BinaryOperation op, boolean value, string oldPattern) {
binaryLiteralLeft(op, value) and oldPattern = value + " " + op.getOperator() + " A"
or
binaryLiteralRight(op, value) and oldPattern = "A " + op.getOperator() + " " + value
op.getLeftOperand().getType() instanceof BoolType and
op.getRightOperand().getType() instanceof BoolType and
(
Comment thread
michaelnebel marked this conversation as resolved.
binaryLiteralLeft(op, value) and oldPattern = value + " " + op.getOperator() + " A"
or
binaryLiteralRight(op, value) and oldPattern = "A " + op.getOperator() + " " + value
)
}

predicate rewriteConditionalExpr(ConditionalExpr cond, string oldPattern, string newPattern) {
conditionalThenLiteral(cond, false) and oldPattern = "A ? false : B" and newPattern = "!A && B"
or
conditionalThenLiteral(cond, true) and oldPattern = "A ? true : B" and newPattern = "A || B"
or
conditionalElseLiteral(cond, false) and oldPattern = "A ? B : false" and newPattern = "A && B"
or
conditionalElseLiteral(cond, true) and oldPattern = "A ? B : true" and newPattern = "!A || B"
or
exists(boolean b | conditionalThenAndElseLiteral(cond, b, b) |
oldPattern = "A ? " + b + " : " + b and newPattern = b.toString()
cond.getThen().getType() instanceof BoolType and
cond.getElse().getType() instanceof BoolType and
Comment thread
michaelnebel marked this conversation as resolved.
Comment thread
michaelnebel marked this conversation as resolved.
(
conditionalThenLiteral(cond, false) and oldPattern = "A ? false : B" and newPattern = "!A && B"
or
conditionalThenLiteral(cond, true) and oldPattern = "A ? true : B" and newPattern = "A || B"
or
conditionalElseLiteral(cond, false) and oldPattern = "A ? B : false" and newPattern = "A && B"
or
conditionalElseLiteral(cond, true) and oldPattern = "A ? B : true" and newPattern = "!A || B"
or
exists(boolean b | conditionalThenAndElseLiteral(cond, b, b) |
oldPattern = "A ? " + b + " : " + b and newPattern = b.toString()
)
or
conditionalThenAndElseLiteral(cond, true, false) and
oldPattern = "A ? true : false" and
newPattern = "A"
or
conditionalThenAndElseLiteral(cond, false, true) and
oldPattern = "A ? false : true" and
newPattern = "!A"
)
or
conditionalThenAndElseLiteral(cond, true, false) and
oldPattern = "A ? true : false" and
newPattern = "A"
or
conditionalThenAndElseLiteral(cond, false, true) and
oldPattern = "A ? false : true" and
newPattern = "!A"
}

predicate negatedOperators(string op, string negated) {
Expand Down