Skip to content

Commit 87d6a60

Browse files
authored
Merge pull request #20924 from paldepind/rust/struct-field-tostring
Rust: Implement `toString` for struct fields and visibility
2 parents 6ddb9c7 + 1fe7e2e commit 87d6a60

File tree

13 files changed

+63
-43
lines changed

13 files changed

+63
-43
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,19 @@ 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() {
34+
result = strictconcat(int i | | this.toStringPart(i) order by i)
35+
}
36+
37+
private string toStringPart(int index) {
38+
index = 0 and result = this.getVisibility().toAbbreviatedString() + " "
39+
or
40+
index = 1 and result = this.getName().getText()
41+
or
42+
index = 2 and result = ": "
43+
or
44+
index = 3 and result = this.getTypeRepr().toAbbreviatedString()
45+
}
3246
}
3347
}

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
}

rust/ql/test/extractor-tests/generated/MacroDef/MacroDef.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ getBody
99
getName
1010
| gen_macro_def.rs:4:5:9:5 | MacroDef | gen_macro_def.rs:7:15:7:24 | vec_of_two |
1111
getVisibility
12-
| gen_macro_def.rs:4:5:9:5 | MacroDef | gen_macro_def.rs:7:5:7:7 | Visibility |
12+
| gen_macro_def.rs:4:5:9:5 | MacroDef | gen_macro_def.rs:7:5:7:7 | pub |

rust/ql/test/extractor-tests/generated/Module/Module.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ getName
1111
| gen_module.rs:5:1:7:1 | mod bar | gen_module.rs:5:5:5:7 | bar |
1212
| lib.rs:1:1:1:19 | mod gen_module | lib.rs:1:9:1:18 | gen_module |
1313
getVisibility
14-
| lib.rs:1:1:1:19 | mod gen_module | lib.rs:1:1:1:3 | Visibility |
14+
| lib.rs:1:1:1:19 | mod gen_module | lib.rs:1:1:1:3 | pub |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
instances
2-
| gen_struct_field.rs:7:16:7:21 | StructField | isUnsafe: | no |
2+
| gen_struct_field.rs:7:16:7:21 | x: i32 | isUnsafe: | no |
33
getAttr
44
getDefault
55
getName
6-
| gen_struct_field.rs:7:16:7:21 | StructField | gen_struct_field.rs:7:16:7:16 | x |
6+
| gen_struct_field.rs:7:16:7:21 | x: i32 | gen_struct_field.rs:7:16:7:16 | x |
77
getTypeRepr
8-
| gen_struct_field.rs:7:16:7:21 | StructField | gen_struct_field.rs:7:19:7:21 | i32 |
8+
| gen_struct_field.rs:7:16:7:21 | x: i32 | gen_struct_field.rs:7:19:7:21 | i32 |
99
getVisibility
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
instances
22
| gen_struct_field_list.rs:7:14:7:31 | StructFieldList |
33
getField
4-
| gen_struct_field_list.rs:7:14:7:31 | StructFieldList | 0 | gen_struct_field_list.rs:7:16:7:21 | StructField |
5-
| gen_struct_field_list.rs:7:14:7:31 | StructFieldList | 1 | gen_struct_field_list.rs:7:24:7:29 | StructField |
4+
| gen_struct_field_list.rs:7:14:7:31 | StructFieldList | 0 | gen_struct_field_list.rs:7:16:7:21 | x: i32 |
5+
| gen_struct_field_list.rs:7:14:7:31 | StructFieldList | 1 | gen_struct_field_list.rs:7:24:7:29 | y: i32 |

rust/ql/test/extractor-tests/generated/Trait/Trait.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ getName
1313
| gen_trait.rs:10:1:10:57 | trait Foo | gen_trait.rs:10:11:10:13 | Foo |
1414
getTypeBoundList
1515
getVisibility
16-
| gen_trait.rs:10:1:10:57 | trait Foo | gen_trait.rs:10:1:10:3 | Visibility |
16+
| gen_trait.rs:10:1:10:57 | trait Foo | gen_trait.rs:10:1:10:3 | pub |
1717
getWhereClause
1818
| gen_trait.rs:10:1:10:57 | trait Foo | gen_trait.rs:10:32:10:54 | WhereClause |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
instances
2-
| gen_visibility.rs:7:7:7:9 | Visibility |
3-
| lib.rs:1:1:1:3 | Visibility |
2+
| gen_visibility.rs:7:7:7:9 | pub |
3+
| lib.rs:1:1:1:3 | pub |
44
getPath

0 commit comments

Comments
 (0)