From 0dccbb9349a06f9803957f7502a7e9501b961cf1 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Wed, 8 Jan 2025 14:16:46 +0100 Subject: [PATCH] Rust: Add two more AST consistency checks --- rust/ql/lib/codeql/rust/AstConsistency.qll | 27 +++++++++++++++++++ .../diagnostics/AstConsistencyCounts.expected | 2 ++ 2 files changed, 29 insertions(+) diff --git a/rust/ql/lib/codeql/rust/AstConsistency.qll b/rust/ql/lib/codeql/rust/AstConsistency.qll index 2358bddb3b9d..0c9359d6569c 100644 --- a/rust/ql/lib/codeql/rust/AstConsistency.qll +++ b/rust/ql/lib/codeql/rust/AstConsistency.qll @@ -52,6 +52,27 @@ query predicate multipleParents(Element child, string childClass, Element parent parentClass = parent.getPrimaryQlClasses() } +/** Holds if `parent` has multiple children at the same index. */ +query predicate multipleChildren(Element parent, int index, Element child1, Element child2) { + child1 = getChildAndAccessor(parent, index, _) and + child2 = getChildAndAccessor(parent, index, _) and + child1 != child2 +} + +/** + * Holds if `child` has multiple positions amongst the `accessor` children + * of `parent`. + * + * Children are allowed to have multiple positions for _different_ accessors, + * for example in an array repeat expression `[1; 10]`, `1` has positions for + * both `getRepeatOperand()` and `getExpr()`. + */ +query predicate multiplePositions(Element parent, int pos1, int pos2, string accessor, Element child) { + child = getChildAndAccessor(parent, pos1, accessor) and + child = getChildAndAccessor(parent, pos2, accessor) and + pos1 != pos2 +} + /** * Gets counts of abstract syntax tree inconsistencies of each type. */ @@ -71,4 +92,10 @@ int getAstInconsistencyCounts(string type) { or type = "Multiple parents" and result = count(Element e | multipleParents(e) | e) + or + type = "Multiple children" and + result = count(Element e | multipleChildren(_, _, e, _) | e) + or + type = "Multiple positions" and + result = count(Element e | multiplePositions(_, _, _, _, e) | e) } diff --git a/rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected b/rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected index 72147226e42b..7f8d388fdc50 100644 --- a/rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected +++ b/rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected @@ -1,5 +1,7 @@ +| Multiple children | 0 | | Multiple locations | 0 | | Multiple parents | 0 | +| Multiple positions | 0 | | Multiple primary QL classes | 0 | | Multiple toStrings | 0 | | No location | 0 |