Skip to content

Commit 8d1a264

Browse files
authored
Ignore build.rs since that code doesn't end up in the target binary (#193)
* Ignore build.rs since that code doesn't end up in the target binary * remove stats in doc-comment coming from build.rs
1 parent 8c863f7 commit 8d1a264

File tree

10 files changed

+60
-5
lines changed

10 files changed

+60
-5
lines changed

cargo-geiger/src/scan.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct ScanResult {
5353
#[derive(Default)]
5454
pub struct GeigerContext {
5555
pub package_id_to_metrics: HashMap<PackageId, PackageMetrics>,
56+
pub ignored_paths: HashSet<PathBuf>,
5657
}
5758

5859
#[derive(Clone, Debug, Default)]
@@ -184,7 +185,10 @@ fn list_files_used_but_not_scanned(
184185
rs_files_used
185186
.iter()
186187
.cloned()
187-
.filter(|p| !scanned_files.contains(p))
188+
.filter(|p| {
189+
!scanned_files.contains(p)
190+
&& !geiger_context.ignored_paths.contains(p)
191+
})
188192
.collect()
189193
}
190194

@@ -449,6 +453,7 @@ mod scan_tests {
449453
.iter()
450454
.cloned()
451455
.collect(),
456+
ignored_paths: HashSet::new(),
452457
};
453458

454459
let rs_files_used = input_rs_files_used_vec.iter().cloned().collect();

cargo-geiger/src/scan/find.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use cargo::{CliError, Config};
1313
use cargo_metadata::PackageId;
1414
use geiger::find::find_unsafe_in_file;
1515
use geiger::{IncludeTests, RsFileMetrics, ScanFileError};
16-
use std::collections::HashMap;
16+
use std::collections::{HashMap, HashSet};
1717
use std::path::Path;
1818
use std::path::PathBuf;
1919
use walkdir::WalkDir;
@@ -48,13 +48,18 @@ where
4848
F: FnMut(usize, usize) -> CargoResult<()>,
4949
{
5050
let mut package_id_to_metrics = HashMap::new();
51+
let mut ignored = HashSet::new();
5152
let packages = cargo_metadata_parameters.metadata.packages.to_vec();
5253
let package_code_files: Vec<_> =
5354
find_rs_files_in_packages(&packages).collect();
5455
let package_code_file_count = package_code_files.len();
5556
for (i, (package_id, rs_code_file)) in
5657
package_code_files.into_iter().enumerate()
5758
{
59+
if let RsFile::CustomBuildRoot(path_buf) = rs_code_file {
60+
ignored.insert(path_buf);
61+
continue;
62+
}
5863
let (is_entry_point, path_buf) =
5964
into_is_entry_point_and_path_buf(rs_code_file);
6065
if let (false, ScanMode::EntryPointsOnly) = (is_entry_point, &mode) {
@@ -90,6 +95,7 @@ where
9095

9196
GeigerContext {
9297
package_id_to_metrics: cargo_core_package_metrics,
98+
ignored_paths: ignored,
9399
}
94100
}
95101

cargo-geiger/tests/external_package_reports/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ pub fn doc_comment_safety_report() -> SafetyReport {
9696
unsafety: UnsafeInfo {
9797
used: CounterBlock {
9898
functions: Count {
99-
safe: 1,
99+
safe: 0,
100100
unsafe_: 0,
101101
},
102102
exprs: Count {
103-
safe: 37,
103+
safe: 0,
104104
unsafe_: 0,
105105
},
106106
..Default::default()

cargo-geiger/tests/integration_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use std::process::Output;
1919
case("test4_workspace_with_top_level_package"),
2020
case("test5_workspace_with_virtual_manifest"),
2121
case("test6_cargo_lock_out_of_date"),
22-
case("test7_package_with_patched_dep")
22+
case("test7_package_with_patched_dep"),
23+
case("test8_package_with_build_rs_no_deps")
2324
)]
2425
fn test_package(name: &str) {
2526
better_panic::install();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: cargo-geiger/tests/integration_tests.rs
3+
expression: stdout
4+
---
5+
6+
Metric output format: x/y
7+
x = unsafe code used by the build
8+
y = total unsafe code found in the crate
9+
10+
Symbols:
11+
:) = No `unsafe` usage found, declares #![forbid(unsafe_code)]
12+
? = No `unsafe` usage found, missing #![forbid(unsafe_code)]
13+
! = `unsafe` usage found
14+
15+
Functions Expressions Impls Traits Methods Dependency
16+
17+
0/0 0/0 0/0 0/0 0/0 ? test8_package_with_build_rs_no_deps 0.1.0
18+
19+
0/0 0/0 0/0 0/0 0/0
20+
21+

test_crates/test8_package_with_build_rs_no_deps/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test8_package_with_build_rs_no_deps"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
[workspace]

test_crates/test8_package_with_build_rs_no_deps/README.md

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub unsafe fn f() {
2+
unimplemented!()
3+
}
4+
5+
fn main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn g() {
2+
println!("Hello");
3+
}
4+

0 commit comments

Comments
 (0)