C#: Update ConstantCondition.ql

This commit is contained in:
Tom Hvitved
2020-11-13 10:09:35 +01:00
parent 94deed39a2
commit 708fca4a2f
3 changed files with 30 additions and 2 deletions

View File

@@ -35,7 +35,23 @@ class ConstantBooleanCondition extends ConstantCondition {
override predicate isWhiteListed() {
// E.g. `x ?? false`
this.(BoolLiteral) = any(NullCoalescingExpr nce).getRightOperand()
this.(BoolLiteral) = any(NullCoalescingExpr nce).getRightOperand() or
// No need to flag logical operations when the operands are constant
isConstantCondition(this.(LogicalNotExpr).getOperand(), _) or
this =
any(LogicalAndExpr lae |
isConstantCondition(lae.getAnOperand(), false)
or
isConstantCondition(lae.getLeftOperand(), true) and
isConstantCondition(lae.getRightOperand(), true)
) or
this =
any(LogicalOrExpr loe |
isConstantCondition(loe.getAnOperand(), true)
or
isConstantCondition(loe.getLeftOperand(), false) and
isConstantCondition(loe.getRightOperand(), false)
)
}
}
@@ -51,7 +67,8 @@ class ConstantIfCondition extends ConstantBooleanCondition {
or
// It is a common pattern to use a local constant/constant field to control
// whether code parts must be executed or not
this instanceof AssignableRead
this instanceof AssignableRead and
not this instanceof ParameterRead
}
}

View File

@@ -106,6 +106,15 @@ class ConstantMatching
_ => o.ToString() // GOOD
};
}
void M6(bool b1, bool b2) {
if (!b1)
return;
if (!b2)
return;
if (b1 && b2) // BAD
return;
}
}
class Assertions

View File

@@ -8,6 +8,8 @@
| ConstantCondition.cs:66:18:66:18 | 3 | Pattern always matches. |
| ConstantCondition.cs:77:18:77:20 | access to type Int32 | Pattern never matches. |
| ConstantCondition.cs:97:13:97:13 | _ | Pattern always matches. |
| ConstantCondition.cs:115:13:115:14 | access to parameter b1 | Condition always evaluates to 'true'. |
| ConstantCondition.cs:115:19:115:20 | access to parameter b2 | Condition always evaluates to 'true'. |
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |