]>
git.proxmox.com Git - rustc.git/blob - src/tools/tidy/src/cargo.rs
1 //! Tidy check to ensure that `[dependencies]` and `extern crate` are in sync.
3 //! This tidy check ensures that all crates listed in the `[dependencies]`
4 //! section of a `Cargo.toml` are present in the corresponding `lib.rs` as
5 //! `extern crate` declarations. This should help us keep the DAG correctly
6 //! structured through various refactorings to prune out unnecessary edges.
11 pub fn check(path
: &Path
, bad
: &mut bool
) {
12 if !super::filter_dirs(path
) {
15 for entry
in t
!(path
.read_dir(), path
).map(|e
| t
!(e
)) {
16 // Look for `Cargo.toml` with a sibling `src/lib.rs` or `lib.rs`.
17 if entry
.file_name().to_str() == Some("Cargo.toml") {
18 if path
.join("src/lib.rs").is_file() {
19 verify(&entry
.path(), &path
.join("src/lib.rs"), bad
)
21 if path
.join("lib.rs").is_file() {
22 verify(&entry
.path(), &path
.join("lib.rs"), bad
)
24 } else if t
!(entry
.file_type()).is_dir() {
25 check(&entry
.path(), bad
);
30 /// Verifies that the dependencies in Cargo.toml at `tomlfile` are synced with
31 /// the `extern crate` annotations in the lib.rs at `libfile`.
32 fn verify(tomlfile
: &Path
, libfile
: &Path
, bad
: &mut bool
) {
33 let toml
= t
!(fs
::read_to_string(&tomlfile
));
34 let librs
= t
!(fs
::read_to_string(&libfile
));
36 if toml
.contains("name = \"bootstrap\"") {
40 // "Poor man's TOML parser" -- just assume we use one syntax for now.
51 // If we encounter a line starting with `[` then we assume it's the end of
52 // the dependency section and bail out.
53 let deps
= match toml
.find("[dependencies]") {
54 Some(i
) => &toml
[i
+ 1..],
57 for line
in deps
.lines() {
58 if line
.starts_with('
['
) {
62 let krate
= match line
.split_once('
='
) {
64 Some((krate
, _
)) => krate
.trim(),
67 // Don't worry about depending on core/std while not writing `extern crate
68 // core/std` -- that's intentional.
69 if krate
== "core" || krate
== "std" {
73 // This is intentional -- this dependency just makes the crate available
74 // for others later on.
75 let allowed
= krate
.starts_with("panic");
76 if toml
.contains("name = \"std\"") && allowed
{
80 if !librs
.contains(&format
!("extern crate {}", krate
)) {
83 "{} doesn't have `extern crate {}`, but Cargo.toml \