-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathComplexCondition.ql
More file actions
49 lines (39 loc) · 1.52 KB
/
ComplexCondition.ql
File metadata and controls
49 lines (39 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @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 maintainability
* readability
* testability
*/
import csharp
abstract class RelevantBinaryOperations extends Operation { }
private class AddBinaryLogicalOperationRelevantBinaryOperations extends RelevantBinaryOperations,
BinaryLogicalOperation
{ }
private class AddAssignCoalesceExprRelevantBinaryOperations extends RelevantBinaryOperations,
AssignCoalesceExpr
{ }
abstract class RelevantOperations extends Operation { }
private class AddLogicalOperationRelevantOperations extends RelevantOperations, LogicalOperation { }
private class AddAssignCoalesceExprRelevantOperations extends RelevantOperations, AssignCoalesceExpr
{ }
predicate nontrivialLogicalOperator(RelevantBinaryOperations e) {
not exists(RelevantBinaryOperations parent |
parent = e.getParent() and
parent.getOperator() = e.getOperator()
)
}
predicate logicalParent(RelevantOperations op, RelevantOperations parent) {
parent = op.getParent()
}
from Expr e, int operators
where
not e.getParent() instanceof RelevantOperations and
operators =
count(RelevantBinaryOperations op | logicalParent*(op, e) and nontrivialLogicalOperator(op)) and
operators > 3
select e, "Complex condition: too many logical operations in this expression."