Skip to content

Commit e814133

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 e814133

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
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

+16-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::ffi::OsStr;
2121
use std::path::Path;
2222
use std::sync::LazyLock;
2323

24-
use regex::RegexSetBuilder;
24+
use regex::{Regex, RegexSetBuilder};
2525
use rustc_hash::FxHashMap;
2626

2727
use crate::walk::{filter_dirs, walk};
@@ -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,19 @@ pub fn check(path: &Path, bad: &mut bool) {
485486
err("Don't use magic numbers that spell things (consider 0x12345678)");
486487
}
487488
}
489+
490+
// Our .gitignore contains a blanket rule ignoring `build/` directories,
491+
// so avoid the existence of files like `build/mod.rs` or `build/submod.rs`.
492+
// (Bear in mind that some tools don't support negated .gitignore rules.)
493+
// This also has the side-effect of forbidding `build.rs` files that
494+
// aren't build scripts.
495+
static MOD_BUILD_RE: LazyLock<Regex> =
496+
LazyLock::new(|| Regex::new(r"\bmod\s+build\s*[;{]").unwrap());
497+
if is_rust_file && !trimmed.starts_with("//") && MOD_BUILD_RE.is_match(trimmed) {
498+
err("modules named `build` can cause problems for git-adjacent tools; \
499+
use a different name (e.g. `builder`)");
500+
}
501+
488502
// for now we just check libcore
489503
if trimmed.contains("unsafe {")
490504
&& !trimmed.starts_with("//")

0 commit comments

Comments
 (0)