//! Mandatory test 6 (the part a test can check; `cargo deny` and `cargo audit` are run separately, //! see docs/01_arcanum.md). The compiler enforces `#![forbid(unsafe_code)]`; this guardrail makes //! sure the attribute itself is never removed and no source file opts back in. use std::fs; use std::path::Path; fn rust_files(dir: &Path, out: &mut Vec) { for entry in fs::read_dir(dir).unwrap() { let path = entry.unwrap().path(); if path.is_dir() { rust_files(&path, out); } else if path.extension().is_some_and(|e| e == "rs") { out.push(path); } } } #[test] fn the_crate_forbids_unsafe_code() { let lib = fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("src/lib.rs")).unwrap(); assert!(lib.lines().any(|l| l.trim() == "#![forbid(unsafe_code)]"), "src/lib.rs must forbid unsafe code"); } #[test] fn no_source_file_contains_unsafe() { let mut files = Vec::new(); rust_files(&Path::new(env!("CARGO_MANIFEST_DIR")).join("src"), &mut files); assert!(!files.is_empty()); for f in files { let text = fs::read_to_string(&f).unwrap(); for (n, line) in text.lines().enumerate() { let code = line.split("//").next().unwrap_or(""); let opts_in = code.contains("unsafe") && code.trim() != "#![forbid(unsafe_code)]"; assert!(!opts_in, "{}:{}: {line}", f.display(), n + 1); } } }