From 1c0f60fa2e485c39e08113b74d9b1539b2dcedf8 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 5588d2ecc7e5..f6926609d685 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 d6a21ebb49cf..035a74975be8 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected @@ -1 +1 @@ -| 49 | +| 48 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected index d6a21ebb49cf..035a74975be8 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected @@ -1 +1 @@ -| 49 | +| 48 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected index 03846c665ae8..2be3f73088ca 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 | | does_not_compile.rs:0:0:0:0 | does_not_compile.rs | 3 | | error.rs:0:0:0:0 | error.rs | 3 | diff --git a/rust/ql/test/query-tests/diagnostics/SummaryStats.expected b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected index 3bfa8561888a..145a18cae05d 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 | 204 | | Elements unextracted | 0 | | Files extracted | 6 | -| Lines of code extracted | 49 | -| Lines of user code extracted | 49 | +| Lines of code extracted | 48 | +| Lines of user code extracted | 48 |