Skip to content

Commit a53f664

Browse files
committed
Rust: Fix bad join
1 parent d5d61dd commit a53f664

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* INTERNAL: Do not use.
55
*/
66

7+
private import codeql.files.FileSystem
78
private import codeql.rust.elements.internal.generated.Function
89
private import codeql.rust.elements.Comment
910

@@ -28,6 +29,30 @@ module Impl {
2829
class Function extends Generated::Function {
2930
override string toStringImpl() { result = "fn " + this.getName().getText() }
3031

32+
pragma[nomagic]
33+
private predicate hasPotentialCommentAt(File f, int line) {
34+
f = this.getLocation().getFile() and
35+
// When a function is preceded by comments its start line is the line of
36+
// the first comment. Hence all relevant comments are found by including
37+
// comments from the start line and up to the line with the function
38+
// name.
39+
line in [this.getLocation().getStartLine() .. this.getName().getLocation().getStartLine()]
40+
}
41+
42+
/**
43+
* Gets a comment preceding this function.
44+
*
45+
* A comment is considered preceding if it occurs immediately before this
46+
* function or if only other comments occur between the comment and this
47+
* function.
48+
*/
49+
Comment getAPrecedingComment() {
50+
exists(File f, int line |
51+
this.hasPotentialCommentAt(f, line) and
52+
result.getLocation().hasLocationFileInfo(f, line, _, _, _)
53+
)
54+
}
55+
3156
/**
3257
* Gets a comment preceding this function.
3358
*

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

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

77
string getComment(R::Function callable) {
8-
result = callable.getPrecedingComment().getCommentText()
8+
result = callable.getAPrecedingComment().getCommentText()
99
}
1010
}
1111

rust/ql/test/library-tests/type-inference/type-inference.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ module ResolveTest implements TestSig {
1111
string getARelevantTag() { result = ["method", "fieldof"] }
1212

1313
private predicate functionHasValue(Function f, string value) {
14-
f.getPrecedingComment().getCommentText() = value
14+
f.getAPrecedingComment().getCommentText() = value
1515
or
16-
not exists(f.getPrecedingComment()) and
16+
not exists(f.getAPrecedingComment()) and
1717
value = f.getName().getText()
1818
}
1919

0 commit comments

Comments
 (0)