From fce5d6b5aed49f860372afebaacbc69352978f0e Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Wed, 18 Sep 2024 19:25:26 +0200 Subject: [PATCH] Rust: lines of code, exclude 'SourceFile' node --- rust/ql/lib/codeql/files/FileSystem.qll | 8 ++++++-- rust/ql/test/query-tests/diagnostics/LinesOfCode.expected | 2 +- .../test/query-tests/diagnostics/LinesOfUserCode.expected | 2 +- .../diagnostics/LinesOfUserCodeInFiles.expected | 2 +- .../ql/test/query-tests/diagnostics/SummaryStats.expected | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/rust/ql/lib/codeql/files/FileSystem.qll b/rust/ql/lib/codeql/files/FileSystem.qll index 5588d2ecc7e50..f6926609d685d 100644 --- a/rust/ql/lib/codeql/files/FileSystem.qll +++ b/rust/ql/lib/codeql/files/FileSystem.qll @@ -2,6 +2,8 @@ private import codeql.Locations private import codeql.util.FileSystem +private import codeql.rust.elements.SourceFile +private import codeql.rust.elements.AstNode private module Input implements InputSig { abstract class ContainerBase extends @container { @@ -41,8 +43,10 @@ class File extends Container, Impl::File { int getNumberOfLinesOfCode() { result = count(int line | - exists(Location loc | - loc.getFile() = this and + exists(AstNode node, Location loc | + not node instanceof SourceFile and loc = node.getLocation() + | + node.getFile() = this and line = [loc.getStartLine(), loc.getEndLine()] and not loc instanceof EmptyLocation ) diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected index 3885b955c3f3f..be186c4c08f2a 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected @@ -1 +1 @@ -| 46 | +| 45 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected index 3885b955c3f3f..be186c4c08f2a 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected @@ -1 +1 @@ -| 46 | +| 45 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected index 77f76dc8dfae3..de89005bc4a4b 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected @@ -1,6 +1,6 @@ | my_struct.rs:0:0:0:0 | my_struct.rs | 21 | | my_macro.rs:0:0:0:0 | my_macro.rs | 8 | -| lib.rs:0:0:0:0 | lib.rs | 7 | | main.rs:0:0:0:0 | main.rs | 7 | +| lib.rs:0:0:0:0 | lib.rs | 6 | | error.rs:0:0:0:0 | error.rs | 3 | | does_not_compile.rs:0:0:0:0 | does_not_compile.rs | 0 | diff --git a/rust/ql/test/query-tests/diagnostics/SummaryStats.expected b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected index 6356e37c40931..d2ee1e81ca7dd 100644 --- a/rust/ql/test/query-tests/diagnostics/SummaryStats.expected +++ b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected @@ -1,5 +1,5 @@ | Elements extracted | 167 | | Elements unextracted | 0 | | Files extracted | 6 | -| Lines of code extracted | 46 | -| Lines of user code extracted | 46 | +| Lines of code extracted | 45 | +| Lines of user code extracted | 45 |