Skip to content

Commit 508e78f

Browse files
committed
Add a tidy check prohibiting mod build
Because we have a .gitignore rule for `build/`, having a module directory named "build" causes various problems for tools that try to skip ignored files. Forbidding this in tidy is trickier than it looks, because tidy itself tries to skip ignored files. So the rule instead looks in non-ignored files for module declarations that would imply the existence of potentially-ignored module directories. This also has the side-effect of prohibiting `build.rs` files that aren't build scripts.
1 parent ccc06c0 commit 508e78f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ no_llvm_build
4646
/inst/
4747
/llvm/
4848
/mingw-build/
49+
# Ignore all "build" directories, in the root and in subdirectories.
50+
# This makes it a bad idea to have Rust modules named "build", so there is a
51+
# tidy rule prohibiting that. Try to avoid adding exceptions to this, because
52+
# some tools don't properly handle negative .gitignore rules.
4953
build/
50-
!/compiler/rustc_mir_build/src/build/
5154
/build-rust-analyzer/
5255
/dist/
5356
/unicode-downloads

src/tools/tidy/src/style.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ pub fn check(path: &Path, bad: &mut bool) {
343343
let filename = file.file_name().unwrap().to_string_lossy();
344344

345345
let is_style_file = filename.ends_with(".css");
346-
let under_rustfmt = filename.ends_with(".rs") &&
346+
let is_rust_file = filename.ends_with(".rs");
347+
let under_rustfmt = is_rust_file &&
347348
// This list should ideally be sourced from rustfmt.toml but we don't want to add a toml
348349
// parser to tidy.
349350
!file.ancestors().any(|a| {
@@ -485,6 +486,21 @@ pub fn check(path: &Path, bad: &mut bool) {
485486
err("Don't use magic numbers that spell things (consider 0x12345678)");
486487
}
487488
}
489+
490+
// Forbid `mod build;` and `mod build {`, because they imply the
491+
// possible existence of a source directory named "build" that would
492+
// be ignored by our blanket .gitignore rule for `build/`.
493+
// (Bear in mind that some tools don't support negated .gitignore rules.)
494+
// This also has the side-effect of forbidding `build.rs` files that
495+
// aren't build scripts.
496+
if is_rust_file
497+
&& !trimmed.starts_with("//")
498+
&& static_regex!(r"\bmod\s+build\s*[;{]").is_match(trimmed)
499+
{
500+
err("modules named `build` can cause problems for git-adjacent tools; \
501+
use a different name (e.g. `builder`)");
502+
}
503+
488504
// for now we just check libcore
489505
if trimmed.contains("unsafe {")
490506
&& !trimmed.starts_with("//")

0 commit comments

Comments
 (0)