Rust: Fix bad join

This commit is contained in:
Simon Friis Vindum
2025-04-03 14:33:02 +02:00
parent d5d61dd8b3
commit a53f664e85
3 changed files with 28 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
* INTERNAL: Do not use.
*/
private import codeql.files.FileSystem
private import codeql.rust.elements.internal.generated.Function
private import codeql.rust.elements.Comment
@@ -28,6 +29,30 @@ module Impl {
class Function extends Generated::Function {
override string toStringImpl() { result = "fn " + this.getName().getText() }
pragma[nomagic]
private predicate hasPotentialCommentAt(File f, int line) {
f = this.getLocation().getFile() and
// When a function is preceded by comments its start line is the line of
// the first comment. Hence all relevant comments are found by including
// comments from the start line and up to the line with the function
// name.
line in [this.getLocation().getStartLine() .. this.getName().getLocation().getStartLine()]
}
/**
* Gets a comment preceding this function.
*
* A comment is considered preceding if it occurs immediately before this
* function or if only other comments occur between the comment and this
* function.
*/
Comment getAPrecedingComment() {
exists(File f, int line |
this.hasPotentialCommentAt(f, line) and
result.getLocation().hasLocationFileInfo(f, line, _, _, _)
)
}
/**
* Gets a comment preceding this function.
*

View File

@@ -5,7 +5,7 @@ private module InlineMadTestLang implements InlineMadTestLangSig {
class Callable = R::Function;
string getComment(R::Function callable) {
result = callable.getPrecedingComment().getCommentText()
result = callable.getAPrecedingComment().getCommentText()
}
}

View File

@@ -11,9 +11,9 @@ module ResolveTest implements TestSig {
string getARelevantTag() { result = ["method", "fieldof"] }
private predicate functionHasValue(Function f, string value) {
f.getPrecedingComment().getCommentText() = value
f.getAPrecedingComment().getCommentText() = value
or
not exists(f.getPrecedingComment()) and
not exists(f.getAPrecedingComment()) and
value = f.getName().getText()
}