Files
codeql/csharp/ql/src/Complexity/ComplexCondition.ql
Anders Schack-Mulligen 726a873c3e C#: Autoformat.
2020-01-29 13:15:00 +01:00

30 lines
956 B
Plaintext

/**
* @name Complex condition
* @description Boolean expressions should not be too deeply nested. Naming intermediate results as local variables will make the logic easier to read and understand.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id cs/complex-condition
* @tags testability
* readability
*/
import csharp
predicate nontrivialLogicalOperator(BinaryLogicalOperation e) {
not exists(BinaryLogicalOperation parent |
parent = e.getParent() and
parent.getOperator() = e.getOperator()
)
}
predicate logicalParent(LogicalOperation op, LogicalOperation parent) { parent = op.getParent() }
from Expr e, int operators
where
not e.getParent() instanceof LogicalOperation and
operators =
count(BinaryLogicalOperation op | logicalParent*(op, e) and nontrivialLogicalOperator(op)) and
operators > 3
select e.getLocation(), "Complex condition: too many logical operations in this expression."