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