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