-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forbid certain macros in the codebase.
- Loading branch information
Showing
4 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//! Tests for internal code checks. | ||
use std::fs; | ||
|
||
#[test] | ||
fn check_forbidden_code() { | ||
// Do not use certain macros, functions, etc. | ||
if !cargo::util::is_ci() { | ||
// Only check these on CI, otherwise it could be annoying. | ||
return; | ||
} | ||
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); | ||
for entry in walkdir::WalkDir::new(path) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
{ | ||
let path = entry.path(); | ||
if !entry | ||
.file_name() | ||
.to_str() | ||
.map(|s| s.ends_with(".rs")) | ||
.unwrap_or(false) | ||
{ | ||
continue; | ||
} | ||
let c = fs::read_to_string(path).unwrap(); | ||
for (line_index, line) in c.lines().enumerate() { | ||
if line_has_print(line) { | ||
if entry.file_name().to_str().unwrap() == "cargo_new.rs" && line.contains("Hello") { | ||
// An exception. | ||
continue; | ||
} | ||
panic!( | ||
"found print macro in {}:{}\n\n{}\n\n\ | ||
print! macros should not be used in Cargo because they can panic.\n\ | ||
Use one of the drop_print macros instead.\n\ | ||
", | ||
path.display(), | ||
line_index, | ||
line | ||
); | ||
} | ||
if line_has_macro(line, "dbg") { | ||
panic!( | ||
"found dbg! macro in {}:{}\n\n{}\n\n\ | ||
dbg! should not be used outside of debugging.", | ||
path.display(), | ||
line_index, | ||
line | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn line_has_print(line: &str) -> bool { | ||
line_has_macro(line, "print") | ||
|| line_has_macro(line, "eprint") | ||
|| line_has_macro(line, "println") | ||
|| line_has_macro(line, "eprintln") | ||
} | ||
|
||
#[test] | ||
fn line_has_print_works() { | ||
assert!(line_has_print("print!")); | ||
assert!(line_has_print("println!")); | ||
assert!(line_has_print("eprint!")); | ||
assert!(line_has_print("eprintln!")); | ||
assert!(line_has_print("(print!(\"hi!\"))")); | ||
assert!(!line_has_print("print")); | ||
assert!(!line_has_print("i like to print things")); | ||
assert!(!line_has_print("drop_print!")); | ||
assert!(!line_has_print("drop_println!")); | ||
assert!(!line_has_print("drop_eprint!")); | ||
assert!(!line_has_print("drop_eprintln!")); | ||
} | ||
|
||
fn line_has_macro(line: &str, mac: &str) -> bool { | ||
for (i, _) in line.match_indices(mac) { | ||
if line.get(i + mac.len()..i + mac.len() + 1) != Some("!") { | ||
continue; | ||
} | ||
if i == 0 { | ||
return true; | ||
} | ||
// Check for identifier boundary start. | ||
let prev1 = line.get(i - 1..i).unwrap().chars().next().unwrap(); | ||
if prev1.is_alphanumeric() || prev1 == '_' { | ||
continue; | ||
} | ||
return true; | ||
} | ||
false | ||
} |