2022-08-20 20:40:08 +02:00
|
|
|
#![feature(let_chains)]
|
2022-04-07 18:39:59 +01:00
|
|
|
#![feature(rustc_private)]
|
2021-05-20 12:30:31 +02:00
|
|
|
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
|
2024-04-04 19:52:55 +02:00
|
|
|
#![warn(
|
|
|
|
trivial_casts,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
rust_2018_idioms,
|
|
|
|
unused_lifetimes,
|
|
|
|
unused_qualifications
|
|
|
|
)]
|
2024-04-18 17:48:52 +02:00
|
|
|
#![allow(clippy::missing_panics_doc)]
|
2019-11-25 17:23:48 +01:00
|
|
|
|
2023-01-12 19:48:13 +01:00
|
|
|
// The `rustc_driver` crate seems to be required in order to use the `rust_lexer` crate.
|
|
|
|
#[allow(unused_extern_crates)]
|
|
|
|
extern crate rustc_driver;
|
2022-04-07 18:39:59 +01:00
|
|
|
extern crate rustc_lexer;
|
|
|
|
|
2023-05-05 17:45:49 +02:00
|
|
|
use std::io;
|
2021-10-07 11:21:30 +02:00
|
|
|
use std::path::PathBuf;
|
2023-05-05 17:45:49 +02:00
|
|
|
use std::process::{self, ExitStatus};
|
2018-07-17 22:50:17 +02:00
|
|
|
|
2022-07-18 09:39:37 +02:00
|
|
|
pub mod dogfood;
|
2020-03-31 15:09:11 +02:00
|
|
|
pub mod fmt;
|
2021-12-06 12:33:31 +01:00
|
|
|
pub mod lint;
|
2020-03-31 15:09:11 +02:00
|
|
|
pub mod new_lint;
|
2020-10-09 12:45:29 +02:00
|
|
|
pub mod serve;
|
2021-07-01 18:17:38 +02:00
|
|
|
pub mod setup;
|
2020-03-31 15:09:11 +02:00
|
|
|
pub mod update_lints;
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
|
|
|
|
#[cfg(windows)]
|
|
|
|
static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
|
|
|
|
|
|
|
|
/// Returns the path to the `cargo-clippy` binary
|
2023-07-02 14:35:19 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the path of current executable could not be retrieved.
|
2022-04-07 18:39:59 +01:00
|
|
|
#[must_use]
|
|
|
|
pub fn cargo_clippy_path() -> PathBuf {
|
|
|
|
let mut path = std::env::current_exe().expect("failed to get current executable name");
|
|
|
|
path.set_file_name(CARGO_CLIPPY_EXE);
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2020-01-30 08:33:48 +01:00
|
|
|
/// Returns the path to the Clippy project directory
|
2021-02-02 20:43:30 -08:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the current directory could not be retrieved, there was an error reading any of the
|
|
|
|
/// Cargo.toml files or ancestor directory is the clippy root directory
|
2020-01-31 07:32:53 +01:00
|
|
|
#[must_use]
|
2020-01-30 21:29:21 +01:00
|
|
|
pub fn clippy_project_root() -> PathBuf {
|
|
|
|
let current_dir = std::env::current_dir().unwrap();
|
|
|
|
for path in current_dir.ancestors() {
|
|
|
|
let result = std::fs::read_to_string(path.join("Cargo.toml"));
|
|
|
|
if let Err(err) = &result {
|
2023-07-31 23:53:53 +02:00
|
|
|
if err.kind() == io::ErrorKind::NotFound {
|
2020-01-30 21:29:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = result.unwrap();
|
|
|
|
if content.contains("[package]\nname = \"clippy\"") {
|
|
|
|
return path.to_path_buf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
|
2020-01-30 08:33:48 +01:00
|
|
|
}
|
2023-05-05 17:45:49 +02:00
|
|
|
|
2023-07-02 14:35:19 +02:00
|
|
|
/// # Panics
|
|
|
|
/// Panics if given command result was failed.
|
2023-05-05 17:45:49 +02:00
|
|
|
pub fn exit_if_err(status: io::Result<ExitStatus>) {
|
|
|
|
match status.expect("failed to run command").code() {
|
|
|
|
Some(0) => {},
|
|
|
|
Some(n) => process::exit(n),
|
|
|
|
None => {
|
|
|
|
eprintln!("Killed by signal");
|
|
|
|
process::exit(1);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|