Skip to content

Commit 50e8d0c

Browse files
committed
Rust: Add isFieldless and isUnitOnly to Enum
1 parent b4ae588 commit 50e8d0c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

rust/ql/lib/codeql/rust/elements/internal/EnumImpl.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,23 @@ module Impl {
3131
result = this.getVariantList().getAVariant() and
3232
result.getName().getText() = name
3333
}
34+
35+
/**
36+
* Holds if this is a field-less enum, that is, an enum where no constructors contain fields.
37+
*
38+
* See: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.fieldless
39+
*/
40+
predicate isFieldless() {
41+
forall(Variant v | v = this.getVariantList().getAVariant() | v.getNumberOfFields() = 0)
42+
}
43+
44+
/**
45+
* Holds if this is a unit-only enum, that is, an enum where all constructors are unit variants.
46+
*
47+
* See: https://doc.rust-lang.org/reference/items/enumerations.html#r-items.enum.unit-only
48+
*/
49+
predicate isUnitOnly() {
50+
forall(Variant v | v = this.getVariantList().getAVariant() | v.isUnit())
51+
}
3452
}
3553
}

rust/ql/lib/codeql/rust/elements/internal/VariantImpl.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ module Impl {
3636
pragma[nomagic]
3737
TupleField getTupleField(int i) { result = this.getFieldList().(TupleFieldList).getField(i) }
3838

39+
int getNumberOfFields() {
40+
not this.hasFieldList() and
41+
result = 0
42+
or
43+
result = this.getFieldList().(StructFieldList).getNumberOfFields()
44+
or
45+
result = this.getFieldList().(TupleFieldList).getNumberOfFields()
46+
}
47+
3948
/** Holds if this variant uses tuple fields. */
4049
pragma[nomagic]
4150
predicate isTuple() { this.getFieldList() instanceof TupleFieldList }

0 commit comments

Comments
 (0)