Skip to content

Commit d88cfe9

Browse files
committed
Rust: Implement toString for struct fields and visibility
1 parent 8b32679 commit d88cfe9

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,17 @@ module Impl {
2929

3030
/** Holds if this record field is named `name` and belongs to the struct `s`. */
3131
predicate isStructField(Struct s, string name) { this = s.getStructField(name) }
32+
33+
override string toStringImpl() { result = concat(int i | | this.toStringPart(i) order by i) }
34+
35+
private string toStringPart(int index) {
36+
index = 0 and result = this.getVisibility().toAbbreviatedString() + " "
37+
or
38+
index = 1 and result = this.getName().getText()
39+
or
40+
index = 2 and result = ": "
41+
or
42+
index = 3 and result = this.getTypeRepr().toAbbreviatedString()
43+
}
3244
}
3345
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `Visibility`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.Visibility
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* A visibility modifier.
1717
*
@@ -21,5 +21,13 @@ module Impl {
2121
* //^^^
2222
* ```
2323
*/
24-
class Visibility extends Generated::Visibility { }
24+
class Visibility extends Generated::Visibility {
25+
override string toStringImpl() { result = this.toAbbreviatedString() }
26+
27+
override string toAbbreviatedString() {
28+
result = "pub(" + this.getPath().toAbbreviatedString() + ")"
29+
or
30+
not this.hasPath() and result = "pub"
31+
}
32+
}
2533
}

0 commit comments

Comments
 (0)