]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_dev/src/lib.rs
8536e2429926a6b9b726b4498db635cfd64a2b1d
[rust.git] / src / tools / clippy / clippy_dev / src / lib.rs
1 #![cfg_attr(bootstrap, feature(let_chains))]
2 #![feature(let_else)]
3 #![feature(once_cell)]
4 #![feature(rustc_private)]
5 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
6 // warn on lints, that are included in `rust-lang/rust`s bootstrap
7 #![warn(rust_2018_idioms, unused_lifetimes)]
8
9 extern crate rustc_lexer;
10
11 use std::path::PathBuf;
12
13 pub mod bless;
14 pub mod dogfood;
15 pub mod fmt;
16 pub mod lint;
17 pub mod new_lint;
18 pub mod serve;
19 pub mod setup;
20 pub mod update_lints;
21
22 #[cfg(not(windows))]
23 static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
24 #[cfg(windows)]
25 static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
26
27 /// Returns the path to the `cargo-clippy` binary
28 #[must_use]
29 pub fn cargo_clippy_path() -> PathBuf {
30     let mut path = std::env::current_exe().expect("failed to get current executable name");
31     path.set_file_name(CARGO_CLIPPY_EXE);
32     path
33 }
34
35 /// Returns the path to the Clippy project directory
36 ///
37 /// # Panics
38 ///
39 /// Panics if the current directory could not be retrieved, there was an error reading any of the
40 /// Cargo.toml files or ancestor directory is the clippy root directory
41 #[must_use]
42 pub fn clippy_project_root() -> PathBuf {
43     let current_dir = std::env::current_dir().unwrap();
44     for path in current_dir.ancestors() {
45         let result = std::fs::read_to_string(path.join("Cargo.toml"));
46         if let Err(err) = &result {
47             if err.kind() == std::io::ErrorKind::NotFound {
48                 continue;
49             }
50         }
51
52         let content = result.unwrap();
53         if content.contains("[package]\nname = \"clippy\"") {
54             return path.to_path_buf();
55         }
56     }
57     panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
58 }