mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-21 20:23:12 +00:00
Introduce utils
mod in clippy_dev
There was some dependence between the different subcommands of clippy_dev. And this dependence will increased with the introduction of the sync and release subcommands. This moves the common functions to a `utils` module, to decouple the other modules.
This commit is contained in:
parent
0712689aae
commit
9c8d9504ce
8 changed files with 154 additions and 154 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::{clippy_project_root, exit_if_err};
|
||||
use crate::utils::{clippy_project_root, exit_if_err};
|
||||
use std::process::Command;
|
||||
|
||||
/// # Panics
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::clippy_project_root;
|
||||
use crate::utils::clippy_project_root;
|
||||
use itertools::Itertools;
|
||||
use rustc_lexer::{TokenKind, tokenize};
|
||||
use shell_escape::escape;
|
||||
|
|
|
@ -14,10 +14,6 @@
|
|||
extern crate rustc_driver;
|
||||
extern crate rustc_lexer;
|
||||
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{self, ExitStatus};
|
||||
|
||||
pub mod dogfood;
|
||||
pub mod fmt;
|
||||
pub mod lint;
|
||||
|
@ -25,58 +21,4 @@ pub mod new_lint;
|
|||
pub mod serve;
|
||||
pub mod setup;
|
||||
pub mod update_lints;
|
||||
|
||||
#[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
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the path of current executable could not be retrieved.
|
||||
#[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
|
||||
}
|
||||
|
||||
/// Returns the path to the Clippy project directory
|
||||
///
|
||||
/// # 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
|
||||
#[must_use]
|
||||
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 {
|
||||
if err.kind() == io::ErrorKind::NotFound {
|
||||
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.");
|
||||
}
|
||||
|
||||
/// # Panics
|
||||
/// Panics if given command result was failed.
|
||||
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);
|
||||
},
|
||||
}
|
||||
}
|
||||
pub mod utils;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{cargo_clippy_path, exit_if_err};
|
||||
use crate::utils::{cargo_clippy_path, exit_if_err};
|
||||
use std::process::{self, Command};
|
||||
use std::{env, fs};
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#![warn(rust_2018_idioms, unused_lifetimes)]
|
||||
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use clippy_dev::{dogfood, fmt, lint, new_lint, serve, setup, update_lints};
|
||||
use clippy_dev::{dogfood, fmt, lint, new_lint, serve, setup, update_lints, utils};
|
||||
use std::convert::Infallible;
|
||||
|
||||
fn main() {
|
||||
|
@ -23,9 +23,9 @@ fn main() {
|
|||
if print_only {
|
||||
update_lints::print_lints();
|
||||
} else if check {
|
||||
update_lints::update(update_lints::UpdateMode::Check);
|
||||
update_lints::update(utils::UpdateMode::Check);
|
||||
} else {
|
||||
update_lints::update(update_lints::UpdateMode::Change);
|
||||
update_lints::update(utils::UpdateMode::Change);
|
||||
}
|
||||
},
|
||||
DevCommand::NewLint {
|
||||
|
@ -35,7 +35,7 @@ fn main() {
|
|||
r#type,
|
||||
msrv,
|
||||
} => match new_lint::create(&pass, &name, &category, r#type.as_deref(), msrv) {
|
||||
Ok(()) => update_lints::update(update_lints::UpdateMode::Change),
|
||||
Ok(()) => update_lints::update(utils::UpdateMode::Change),
|
||||
Err(e) => eprintln!("Unable to create lint: {e}"),
|
||||
},
|
||||
DevCommand::Setup(SetupCommand { subcommand }) => match subcommand {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::clippy_project_root;
|
||||
use crate::utils::{clippy_project_root, clippy_version};
|
||||
use indoc::{formatdoc, writedoc};
|
||||
use std::fmt;
|
||||
use std::fmt::Write as _;
|
||||
|
@ -186,23 +186,8 @@ fn to_camel_case(name: &str) -> String {
|
|||
}
|
||||
|
||||
pub(crate) fn get_stabilization_version() -> String {
|
||||
fn parse_manifest(contents: &str) -> Option<String> {
|
||||
let version = contents
|
||||
.lines()
|
||||
.filter_map(|l| l.split_once('='))
|
||||
.find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
|
||||
let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
|
||||
return None;
|
||||
};
|
||||
let (minor, patch) = version.split_once('.')?;
|
||||
Some(format!(
|
||||
"{}.{}.0",
|
||||
minor.parse::<u32>().ok()?,
|
||||
patch.parse::<u32>().ok()?
|
||||
))
|
||||
}
|
||||
let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
|
||||
parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
|
||||
let (minor, patch) = clippy_version();
|
||||
format!("{minor}.{patch}.0")
|
||||
}
|
||||
|
||||
fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::clippy_project_root;
|
||||
use crate::utils::{UpdateMode, clippy_project_root, exit_with_failure, replace_region_in_file};
|
||||
use aho_corasick::AhoCorasickBuilder;
|
||||
use itertools::Itertools;
|
||||
use rustc_lexer::{LiteralKind, TokenKind, tokenize, unescape};
|
||||
|
@ -17,12 +17,6 @@ const GENERATED_FILE_COMMENT: &str = "// This file was generated by `cargo dev u
|
|||
|
||||
const DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.html";
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum UpdateMode {
|
||||
Check,
|
||||
Change,
|
||||
}
|
||||
|
||||
/// Runs the `update_lints` command.
|
||||
///
|
||||
/// This updates various generated values from the lint source code.
|
||||
|
@ -511,14 +505,6 @@ fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str)
|
|||
}
|
||||
}
|
||||
|
||||
fn exit_with_failure() {
|
||||
println!(
|
||||
"Not all lints defined properly. \
|
||||
Please run `cargo dev update_lints` to make sure all lints are defined properly."
|
||||
);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
/// Lint data parsed from the Clippy source code.
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
struct Lint {
|
||||
|
@ -851,61 +837,6 @@ fn remove_line_splices(s: &str) -> String {
|
|||
});
|
||||
res
|
||||
}
|
||||
|
||||
/// Replaces a region in a file delimited by two lines matching regexes.
|
||||
///
|
||||
/// `path` is the relative path to the file on which you want to perform the replacement.
|
||||
///
|
||||
/// See `replace_region_in_text` for documentation of the other options.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the path could not read or then written
|
||||
fn replace_region_in_file(
|
||||
update_mode: UpdateMode,
|
||||
path: &Path,
|
||||
start: &str,
|
||||
end: &str,
|
||||
write_replacement: impl FnMut(&mut String),
|
||||
) {
|
||||
let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {e}", path.display()));
|
||||
let new_contents = match replace_region_in_text(&contents, start, end, write_replacement) {
|
||||
Ok(x) => x,
|
||||
Err(delim) => panic!("Couldn't find `{delim}` in file `{}`", path.display()),
|
||||
};
|
||||
|
||||
match update_mode {
|
||||
UpdateMode::Check if contents != new_contents => exit_with_failure(),
|
||||
UpdateMode::Check => (),
|
||||
UpdateMode::Change => {
|
||||
if let Err(e) = fs::write(path, new_contents.as_bytes()) {
|
||||
panic!("Cannot write to `{}`: {e}", path.display());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
|
||||
/// were found, or the missing delimiter if not.
|
||||
fn replace_region_in_text<'a>(
|
||||
text: &str,
|
||||
start: &'a str,
|
||||
end: &'a str,
|
||||
mut write_replacement: impl FnMut(&mut String),
|
||||
) -> Result<String, &'a str> {
|
||||
let (text_start, rest) = text.split_once(start).ok_or(start)?;
|
||||
let (_, text_end) = rest.split_once(end).ok_or(end)?;
|
||||
|
||||
let mut res = String::with_capacity(text.len() + 4096);
|
||||
res.push_str(text_start);
|
||||
res.push_str(start);
|
||||
write_replacement(&mut res);
|
||||
res.push_str(end);
|
||||
res.push_str(text_end);
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn try_rename_file(old_name: &Path, new_name: &Path) -> bool {
|
||||
match OpenOptions::new().create_new(true).write(true).open(new_name) {
|
||||
Ok(file) => drop(file),
|
||||
|
|
142
clippy_dev/src/utils.rs
Normal file
142
clippy_dev/src/utils.rs
Normal file
|
@ -0,0 +1,142 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::process::{self, ExitStatus};
|
||||
use std::{fs, io};
|
||||
|
||||
#[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
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the path of current executable could not be retrieved.
|
||||
#[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
|
||||
}
|
||||
|
||||
/// Returns the path to the Clippy project directory
|
||||
///
|
||||
/// # 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
|
||||
#[must_use]
|
||||
pub fn clippy_project_root() -> PathBuf {
|
||||
let current_dir = std::env::current_dir().unwrap();
|
||||
for path in current_dir.ancestors() {
|
||||
let result = fs::read_to_string(path.join("Cargo.toml"));
|
||||
if let Err(err) = &result {
|
||||
if err.kind() == io::ErrorKind::NotFound {
|
||||
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.");
|
||||
}
|
||||
|
||||
/// # Panics
|
||||
/// Panics if given command result was failed.
|
||||
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);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn clippy_version() -> (u32, u32) {
|
||||
fn parse_manifest(contents: &str) -> Option<(u32, u32)> {
|
||||
let version = contents
|
||||
.lines()
|
||||
.filter_map(|l| l.split_once('='))
|
||||
.find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
|
||||
let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
|
||||
return None;
|
||||
};
|
||||
let (minor, patch) = version.split_once('.')?;
|
||||
Some((minor.parse().ok()?, patch.parse().ok()?))
|
||||
}
|
||||
let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
|
||||
parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum UpdateMode {
|
||||
Check,
|
||||
Change,
|
||||
}
|
||||
|
||||
pub(crate) fn exit_with_failure() {
|
||||
println!(
|
||||
"Not all lints defined properly. \
|
||||
Please run `cargo dev update_lints` to make sure all lints are defined properly."
|
||||
);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
/// Replaces a region in a file delimited by two lines matching regexes.
|
||||
///
|
||||
/// `path` is the relative path to the file on which you want to perform the replacement.
|
||||
///
|
||||
/// See `replace_region_in_text` for documentation of the other options.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the path could not read or then written
|
||||
pub(crate) fn replace_region_in_file(
|
||||
update_mode: UpdateMode,
|
||||
path: &Path,
|
||||
start: &str,
|
||||
end: &str,
|
||||
write_replacement: impl FnMut(&mut String),
|
||||
) {
|
||||
let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {e}", path.display()));
|
||||
let new_contents = match replace_region_in_text(&contents, start, end, write_replacement) {
|
||||
Ok(x) => x,
|
||||
Err(delim) => panic!("Couldn't find `{delim}` in file `{}`", path.display()),
|
||||
};
|
||||
|
||||
match update_mode {
|
||||
UpdateMode::Check if contents != new_contents => exit_with_failure(),
|
||||
UpdateMode::Check => (),
|
||||
UpdateMode::Change => {
|
||||
if let Err(e) = fs::write(path, new_contents.as_bytes()) {
|
||||
panic!("Cannot write to `{}`: {e}", path.display());
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
|
||||
/// were found, or the missing delimiter if not.
|
||||
pub(crate) fn replace_region_in_text<'a>(
|
||||
text: &str,
|
||||
start: &'a str,
|
||||
end: &'a str,
|
||||
mut write_replacement: impl FnMut(&mut String),
|
||||
) -> Result<String, &'a str> {
|
||||
let (text_start, rest) = text.split_once(start).ok_or(start)?;
|
||||
let (_, text_end) = rest.split_once(end).ok_or(end)?;
|
||||
|
||||
let mut res = String::with_capacity(text.len() + 4096);
|
||||
res.push_str(text_start);
|
||||
res.push_str(start);
|
||||
write_replacement(&mut res);
|
||||
res.push_str(end);
|
||||
res.push_str(text_end);
|
||||
|
||||
Ok(res)
|
||||
}
|
Loading…
Reference in a new issue