Skip to content

Commit 5c08cd6

Browse files
committed
Rust: Add inline expectations test for type inference
1 parent e6cf737 commit 5c08cd6

File tree

26 files changed

+1405
-1406
lines changed

26 files changed

+1405
-1406
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.Function
8+
private import codeql.rust.elements.Comment
89

910
/**
1011
* INTERNAL: This module contains the customizable definition of `Function` and should not
@@ -26,5 +27,22 @@ module Impl {
2627
*/
2728
class Function extends Generated::Function {
2829
override string toStringImpl() { result = "fn " + this.getName().getText() }
30+
31+
/**
32+
* Gets a comment preceding this function.
33+
*
34+
* A comment is considered preceding if it occurs immediately before this
35+
* function or if only other comments occur between the comment and this
36+
* function.
37+
*/
38+
Comment getPrecedingComment() {
39+
result.getLocation().getFile() = this.getLocation().getFile() and
40+
// When a function is preceded by comments its start line is the line of
41+
// the first comment. Hence all relevant comments are found by including
42+
// comments from the start line and up to the line with the function
43+
// name.
44+
this.getLocation().getStartLine() <= result.getLocation().getStartLine() and
45+
result.getLocation().getStartLine() <= this.getName().getLocation().getStartLine()
46+
}
2947
}
3048
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ module Impl {
4343
}
4444

4545
override string toStringImpl() {
46-
exists(string base, string separator |
46+
exists(string base, string separator, string parenthesis |
4747
base = this.getReceiver().toAbbreviatedString() and
4848
(if base = "..." then separator = " ." else separator = ".") and
49-
result = base + separator + this.getIdentifier().toStringImpl() + "(...)"
49+
(
50+
if this.getArgList().getNumberOfArgs() = 0
51+
then parenthesis = "()"
52+
else parenthesis = "(...)"
53+
) and
54+
result = base + separator + this.getIdentifier().toStringImpl() + parenthesis
5055
)
5156
}
5257
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class StructType extends StructOrEnumType, TStruct {
104104
result = TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
105105
}
106106

107-
override string toString() { result = struct.toString() }
107+
override string toString() { result = struct.getName().getText() }
108108

109109
override Location getLocation() { result = struct.getLocation() }
110110
}
@@ -125,7 +125,7 @@ class EnumType extends StructOrEnumType, TEnum {
125125
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
126126
}
127127

128-
override string toString() { result = enum.toString() }
128+
override string toString() { result = enum.getName().getText() }
129129

130130
override Location getLocation() { result = enum.getLocation() }
131131
}

rust/ql/lib/utils/test/InlineMadTest.qll

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ private module InlineMadTestLang implements InlineMadTestLangSig {
55
class Callable = R::Function;
66

77
string getComment(R::Function callable) {
8-
exists(R::Comment comment |
9-
result = comment.getCommentText() and
10-
comment.getLocation().getFile() = callable.getLocation().getFile() and
11-
// When a function is preceded by comments its start line is the line of
12-
// the first comment. Hence all relevant comments are found by including
13-
// comments from the start line and up to the line with the function
14-
// name.
15-
callable.getLocation().getStartLine() <= comment.getLocation().getStartLine() and
16-
comment.getLocation().getStartLine() <= callable.getName().getLocation().getStartLine()
17-
)
8+
result = callable.getPrecedingComment().getCommentText()
189
}
1910
}
2011

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
multipleStaticCallTargets
2-
| regular.rs:29:5:29:9 | s.g(...) | anonymous.rs:15:9:15:22 | fn g |
3-
| regular.rs:29:5:29:9 | s.g(...) | regular.rs:13:5:13:18 | fn g |
2+
| regular.rs:29:5:29:9 | s.g() | anonymous.rs:15:9:15:22 | fn g |
3+
| regular.rs:29:5:29:9 | s.g() | regular.rs:13:5:13:18 | fn g |

rust/ql/test/extractor-tests/canonical_path/canonical_paths.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ canonicalPaths
3838
resolvedPaths
3939
| anonymous.rs:27:17:27:30 | OtherStruct {...} | None | None |
4040
| anonymous.rs:28:9:28:9 | s | None | None |
41-
| anonymous.rs:28:9:28:13 | s.f(...) | None | None |
41+
| anonymous.rs:28:9:28:13 | s.f() | None | None |
4242
| anonymous.rs:29:9:29:9 | s | None | None |
43-
| anonymous.rs:29:9:29:13 | s.g(...) | None | None |
43+
| anonymous.rs:29:9:29:13 | s.g() | None | None |
4444
| anonymous.rs:30:9:30:14 | nested | None | None |
4545
| regular.rs:27:13:27:21 | Struct {...} | repo::test | crate::regular::Struct |
4646
| regular.rs:28:5:28:5 | s | None | None |
47-
| regular.rs:28:5:28:9 | s.f(...) | repo::test | <crate::regular::Struct as crate::regular::Trait>::f |
47+
| regular.rs:28:5:28:9 | s.f() | repo::test | <crate::regular::Struct as crate::regular::Trait>::f |
4848
| regular.rs:29:5:29:5 | s | None | None |
49-
| regular.rs:29:5:29:9 | s.g(...) | repo::test | <crate::regular::Struct>::g |
49+
| regular.rs:29:5:29:9 | s.g() | repo::test | <crate::regular::Struct>::g |
5050
| regular.rs:30:5:30:5 | s | None | None |
51-
| regular.rs:30:5:30:9 | s.h(...) | repo::test | <_ as crate::regular::TraitWithBlanketImpl>::h |
51+
| regular.rs:30:5:30:9 | s.h() | repo::test | <_ as crate::regular::TraitWithBlanketImpl>::h |
5252
| regular.rs:31:5:31:8 | free | repo::test | crate::regular::free |
5353
| regular.rs:41:9:41:26 | ...::None::<...> | lang:core | crate::option::Option::None |
5454
| regular.rs:42:9:42:20 | ...::Some | lang:core | crate::option::Option::Some |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
multipleStaticCallTargets
2-
| regular.rs:32:5:32:9 | s.g(...) | anonymous.rs:18:9:18:22 | fn g |
3-
| regular.rs:32:5:32:9 | s.g(...) | regular.rs:16:5:16:18 | fn g |
2+
| regular.rs:32:5:32:9 | s.g() | anonymous.rs:18:9:18:22 | fn g |
3+
| regular.rs:32:5:32:9 | s.g() | regular.rs:16:5:16:18 | fn g |

rust/ql/test/extractor-tests/canonical_path_disabled/canonical_paths.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ canonicalPaths
3838
resolvedPaths
3939
| anonymous.rs:30:17:30:30 | OtherStruct {...} | None | None |
4040
| anonymous.rs:31:9:31:9 | s | None | None |
41-
| anonymous.rs:31:9:31:13 | s.f(...) | None | None |
41+
| anonymous.rs:31:9:31:13 | s.f() | None | None |
4242
| anonymous.rs:32:9:32:9 | s | None | None |
43-
| anonymous.rs:32:9:32:13 | s.g(...) | None | None |
43+
| anonymous.rs:32:9:32:13 | s.g() | None | None |
4444
| anonymous.rs:33:9:33:14 | nested | None | None |
4545
| regular.rs:30:13:30:21 | Struct {...} | None | None |
4646
| regular.rs:31:5:31:5 | s | None | None |
47-
| regular.rs:31:5:31:9 | s.f(...) | None | None |
47+
| regular.rs:31:5:31:9 | s.f() | None | None |
4848
| regular.rs:32:5:32:5 | s | None | None |
49-
| regular.rs:32:5:32:9 | s.g(...) | None | None |
49+
| regular.rs:32:5:32:9 | s.g() | None | None |
5050
| regular.rs:33:5:33:5 | s | None | None |
51-
| regular.rs:33:5:33:9 | s.h(...) | None | None |
51+
| regular.rs:33:5:33:9 | s.h() | None | None |
5252
| regular.rs:34:5:34:8 | free | None | None |
5353
| regular.rs:44:9:44:26 | ...::None::<...> | None | None |
5454
| regular.rs:45:9:45:20 | ...::Some | None | None |

rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/AstConsistency.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
noLocation
2-
| file://:0:0:0:0 | ... .unwrap(...) |
2+
| file://:0:0:0:0 | ... .unwrap() |
33
| file://:0:0:0:0 | ...: ... |
44
| file://:0:0:0:0 | ...::Path |
55
| file://:0:0:0:0 | ...::Path |
@@ -32,7 +32,7 @@ noLocation
3232
| file://:0:0:0:0 | path |
3333
| file://:0:0:0:0 | path |
3434
| file://:0:0:0:0 | path |
35-
| file://:0:0:0:0 | path.parent(...) |
35+
| file://:0:0:0:0 | path.parent() |
3636
| file://:0:0:0:0 | std |
3737
| file://:0:0:0:0 | std |
3838
| file://:0:0:0:0 | std |

rust/ql/test/library-tests/controlflow/Cfg.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ edges
205205
| test.rs:99:19:99:25 | Some(...) | test.rs:99:24:99:24 | x | match |
206206
| test.rs:99:24:99:24 | x | test.rs:99:24:99:24 | x | |
207207
| test.rs:99:24:99:24 | x | test.rs:100:17:100:17 | x | match |
208-
| test.rs:99:29:99:32 | iter | test.rs:99:29:99:39 | iter.next(...) | |
209-
| test.rs:99:29:99:39 | iter.next(...) | test.rs:99:19:99:25 | Some(...) | |
208+
| test.rs:99:29:99:32 | iter | test.rs:99:29:99:39 | iter.next() | |
209+
| test.rs:99:29:99:39 | iter.next() | test.rs:99:19:99:25 | Some(...) | |
210210
| test.rs:99:41:103:9 | { ... } | test.rs:99:15:99:39 | let ... = ... | |
211211
| test.rs:100:13:102:13 | if ... {...} | test.rs:99:41:103:9 | { ... } | |
212212
| test.rs:100:17:100:17 | x | test.rs:100:22:100:22 | 5 | |
@@ -760,8 +760,8 @@ edges
760760
| test.rs:311:87:313:5 | { ... } | test.rs:311:5:313:5 | exit fn test_question_mark_operator_1 (normal) | |
761761
| test.rs:312:9:312:10 | Ok | test.rs:312:12:312:12 | s | |
762762
| test.rs:312:9:312:33 | Ok(...) | test.rs:311:87:313:5 | { ... } | |
763-
| test.rs:312:12:312:12 | s | test.rs:312:12:312:27 | s.parse(...) | |
764-
| test.rs:312:12:312:27 | s.parse(...) | test.rs:312:12:312:28 | TryExpr | |
763+
| test.rs:312:12:312:12 | s | test.rs:312:12:312:27 | s.parse() | |
764+
| test.rs:312:12:312:27 | s.parse() | test.rs:312:12:312:28 | TryExpr | |
765765
| test.rs:312:12:312:28 | TryExpr | test.rs:311:5:313:5 | exit fn test_question_mark_operator_1 (normal) | return |
766766
| test.rs:312:12:312:28 | TryExpr | test.rs:312:32:312:32 | 4 | match |
767767
| test.rs:312:12:312:32 | ... + ... | test.rs:312:9:312:33 | Ok(...) | |

0 commit comments

Comments
 (0)