Skip to content

Commit 422bec1

Browse files
committed
Rust: Add two more AST consistency checks
1 parent 39fd3ab commit 422bec1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

rust/ql/lib/codeql/rust/AstConsistency.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ query predicate multipleParents(Element child, string childClass, Element parent
5252
parentClass = parent.getPrimaryQlClasses()
5353
}
5454

55+
/** Holds if `parent` has multiple children at the same index. */
56+
query predicate multipleChildren(Element parent, int index, Element child1, Element child2) {
57+
child1 = getChildAndAccessor(parent, index, _) and
58+
child2 = getChildAndAccessor(parent, index, _) and
59+
child1 != child2
60+
}
61+
62+
/**
63+
* Holds if `child` has multiple positions amongst the `accessor` children
64+
* of `parent`.
65+
*
66+
* Children are allowed to have multiple positions for _different_ accessors,
67+
* for example in an array repeat expression `[1; 10]`, `1` has positions for
68+
* both `getRepeatOperand()` and `getExpr()`.
69+
*/
70+
query predicate multiplePositions(Element parent, int pos1, int pos2, string accessor, Element child) {
71+
child = getChildAndAccessor(parent, pos1, accessor) and
72+
child = getChildAndAccessor(parent, pos2, accessor) and
73+
pos1 != pos2
74+
}
75+
5576
/**
5677
* Gets counts of abstract syntax tree inconsistencies of each type.
5778
*/
@@ -71,4 +92,10 @@ int getAstInconsistencyCounts(string type) {
7192
or
7293
type = "Multiple parents" and
7394
result = count(Element e | multipleParents(e) | e)
95+
or
96+
type = "Multiple children" and
97+
result = count(Element e | multipleChildren(_, _, e, _) | e)
98+
or
99+
type = "Multiple positions" and
100+
result = count(Element e | multiplePositions(_, _, _, _, e) | e)
74101
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
| Multiple children | 0 |
12
| Multiple locations | 0 |
23
| Multiple parents | 0 |
4+
| Multiple positions | 0 |
35
| Multiple primary QL classes | 0 |
46
| Multiple toStrings | 0 |
57
| No location | 0 |

0 commit comments

Comments
 (0)