Skip to content

Commit aa303f3

Browse files
committed
Rust: Add more consistency checks
1 parent 09f4e4d commit aa303f3

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ query predicate multiplePathResolutions(Path p, ItemNode i) {
8787
strictcount(resolvePath(p)) > 1
8888
}
8989

90+
/** Holds if `call` has multiple static call targets including `target`. */
91+
query predicate multipleStaticCallTargets(CallExprBase call, Callable target) {
92+
target = call.getStaticTarget() and
93+
strictcount(call.getStaticTarget()) > 1
94+
}
95+
96+
/** Holds if `fe` resolves to multiple record fields including `field`. */
97+
query predicate multipleRecordFields(FieldExpr fe, RecordField field) {
98+
field = fe.getRecordField() and
99+
strictcount(fe.getRecordField()) > 1
100+
}
101+
102+
/** Holds if `fe` resolves to multiple tuple fields including `field`. */
103+
query predicate multipleTupleFields(FieldExpr fe, TupleField field) {
104+
field = fe.getTupleField() and
105+
strictcount(fe.getTupleField()) > 1
106+
}
107+
108+
import codeql.rust.elements.internal.TypeInference::Consistency
109+
90110
/**
91111
* Gets counts of abstract syntax tree inconsistencies of each type.
92112
*/
@@ -115,4 +135,13 @@ int getAstInconsistencyCounts(string type) {
115135
or
116136
type = "Multiple path resolutions" and
117137
result = count(Path p | multiplePathResolutions(p, _) | p)
138+
or
139+
type = "Multiple static call targets" and
140+
result = count(CallExprBase call | multipleStaticCallTargets(call, _) | call)
141+
or
142+
type = "Multiple record fields" and
143+
result = count(FieldExpr fe | multipleRecordFields(fe, _) | fe)
144+
or
145+
type = "Multiple tuple fields" and
146+
result = count(FieldExpr fe | multipleTupleFields(fe, _) | fe)
118147
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
multiplePathResolutions
22
| main.rs:118:9:118:9 | f | main.rs:104:5:106:5 | fn f |
33
| main.rs:118:9:118:9 | f | main.rs:110:5:112:5 | fn f |
4+
multipleStaticCallTargets
5+
| main.rs:118:9:118:11 | f(...) | main.rs:104:5:106:5 | fn f |
6+
| main.rs:118:9:118:11 | f(...) | main.rs:110:5:112:5 | fn f |

rust/ql/test/query-tests/diagnostics/AstConsistencyCounts.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
| Multiple path resolutions | 0 |
55
| Multiple positions | 0 |
66
| Multiple primary QL classes | 0 |
7+
| Multiple record fields | 0 |
8+
| Multiple static call targets | 0 |
79
| Multiple toStrings | 0 |
10+
| Multiple tuple fields | 0 |
811
| No location | 0 |

0 commit comments

Comments
 (0)