about summary refs log tree commit diff
path: root/src/tools/clippy/tests/no-profile-in-cargo-toml.rs
blob: 1f8c4fae9b31fdff4692c469c0c378159fa62b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Check that we do not have `profile.*` sections in our `Cargo.toml` files,
// as this causes warnings when run from the compiler repository which includes
// Clippy in a workspace.
//
// Those sections can be put into `.cargo/config.toml` which will be read
// when commands are issued from the top-level Clippy directory, outside of
// a workspace.

use std::fs::File;
use std::io::{self, BufRead as _};
use walkdir::WalkDir;

#[test]
fn no_profile_in_cargo_toml() {
    // This check could parse `Cargo.toml` using a TOML deserializer, but in practice
    // profile sections would be added at the beginning of a line as `[profile.*]`, so
    // keep it fast and simple.
    for entry in WalkDir::new(".")
        .into_iter()
        // Do not recurse into `target` as lintcheck might put some sources (and their
        //  `Cargo.toml`) there.
        .filter_entry(|e| e.file_name() != "target")
        .filter_map(Result::ok)
        .filter(|e| e.file_name().to_str() == Some("Cargo.toml"))
    {
        for line in io::BufReader::new(File::open(entry.path()).unwrap())
            .lines()
            .map(Result::unwrap)
        {
            if line.starts_with("[profile.") {
                eprintln!("Profile section `{line}` found in file `{}`.", entry.path().display());
                eprintln!("Use `.cargo/config.toml` for profiles specific to the standalone Clippy repository.");
                panic!("Profile section found in `Cargo.toml`");
            }
        }
    }
}