diff options
| author | bors <bors@rust-lang.org> | 2020-01-31 07:18:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-01-31 07:18:06 +0000 |
| commit | ca2abaf3667771ed5d7ab602792d472b032ca445 (patch) | |
| tree | cc833ad4c8a38a38dd0bfe4eb4dd3f7ee69263a1 /clippy_dev/src | |
| parent | 8e9089e454b04b8bad5ed6678050f1cc290a74b3 (diff) | |
| parent | 8f457fa4c99c39ec5c2428922d6f7cc3fec84f59 (diff) | |
| download | rust-ca2abaf3667771ed5d7ab602792d472b032ca445.tar.gz rust-ca2abaf3667771ed5d7ab602792d472b032ca445.zip | |
Auto merge of #5109 - phansch:ciao_util_dev, r=flip1995
Deprecate util/dev in favor of cargo alias This means one less shell script and a bit more cross-platform support for contributors. If you've been using `./util/dev` before, this now becomes `cargo dev`. The key part of this change is found in `.cargo/config` where an alias for calling the `clippy_dev` binary is defined. changelog: none
Diffstat (limited to 'clippy_dev/src')
| -rw-r--r-- | clippy_dev/src/fmt.rs | 30 | ||||
| -rw-r--r-- | clippy_dev/src/lib.rs | 31 | ||||
| -rw-r--r-- | clippy_dev/src/main.rs | 23 | ||||
| -rw-r--r-- | clippy_dev/src/new_lint.rs | 23 |
4 files changed, 46 insertions, 61 deletions
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs index 69cd046a855..a6043c4be0d 100644 --- a/clippy_dev/src/fmt.rs +++ b/clippy_dev/src/fmt.rs @@ -1,7 +1,8 @@ +use clippy_dev::clippy_project_root; use shell_escape::escape; use std::ffi::OsStr; use std::io; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::{self, Command}; use walkdir::WalkDir; @@ -9,7 +10,6 @@ use walkdir::WalkDir; pub enum CliError { CommandFailed(String), IoError(io::Error), - ProjectRootNotFound, RustfmtNotInstalled, WalkDirError(walkdir::Error), } @@ -35,7 +35,7 @@ pub fn run(check: bool, verbose: bool) { fn try_run(context: &FmtContext) -> Result<bool, CliError> { let mut success = true; - let project_root = project_root()?; + let project_root = clippy_project_root(); rustfmt_test(context)?; @@ -69,9 +69,6 @@ pub fn run(check: bool, verbose: bool) { CliError::IoError(err) => { eprintln!("error: {}", err); }, - CliError::ProjectRootNotFound => { - eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir."); - }, CliError::RustfmtNotInstalled => { eprintln!("error: rustfmt nightly is not installed."); }, @@ -88,7 +85,7 @@ pub fn run(check: bool, verbose: bool) { Ok(false) => { eprintln!(); eprintln!("Formatting check failed."); - eprintln!("Run `./util/dev fmt` to update formatting."); + eprintln!("Run `cargo dev fmt` to update formatting."); 1 }, Err(err) => { @@ -176,22 +173,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> { } Ok(success) } - -fn project_root() -> Result<PathBuf, CliError> { - let current_dir = std::env::current_dir()?; - 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?; - if content.contains("[package]\nname = \"clippy\"") { - return Ok(path.to_path_buf()); - } - } - - Err(CliError::ProjectRootNotFound) -} diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 3aae3e53317..c58b80e4318 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -7,6 +7,7 @@ use std::collections::HashMap; use std::ffi::OsStr; use std::fs; use std::io::prelude::*; +use std::path::{Path, PathBuf}; use walkdir::WalkDir; lazy_static! { @@ -205,7 +206,8 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item = Lint> { fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> { // We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories. // Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`. - WalkDir::new("../clippy_lints/src") + let path = clippy_project_root().join("clippy_lints/src"); + WalkDir::new(path) .into_iter() .filter_map(std::result::Result::ok) .filter(|f| f.path().extension() == Some(OsStr::new("rs"))) @@ -225,7 +227,7 @@ pub struct FileChange { /// See `replace_region_in_text` for documentation of the other options. #[allow(clippy::expect_fun_call)] pub fn replace_region_in_file<F>( - path: &str, + path: &Path, start: &str, end: &str, replace_start: bool, @@ -235,14 +237,15 @@ pub fn replace_region_in_file<F>( where F: Fn() -> Vec<String>, { - let mut f = fs::File::open(path).expect(&format!("File not found: {}", path)); + let path = clippy_project_root().join(path); + let mut f = fs::File::open(&path).expect(&format!("File not found: {}", path.to_string_lossy())); let mut contents = String::new(); f.read_to_string(&mut contents) .expect("Something went wrong reading the file"); let file_change = replace_region_in_text(&contents, start, end, replace_start, replacements); if write_back { - let mut f = fs::File::create(path).expect(&format!("File not found: {}", path)); + let mut f = fs::File::create(&path).expect(&format!("File not found: {}", path.to_string_lossy())); f.write_all(file_change.new_lines.as_bytes()) .expect("Unable to write file"); // Ensure we write the changes with a trailing newline so that @@ -318,6 +321,26 @@ where } } +/// Returns the path to the Clippy project 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() == std::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."); +} + #[test] fn test_parse_contents() { let result: Vec<Lint> = parse_contents( diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index 58b4f87e872..7369db1f078 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -2,6 +2,7 @@ use clap::{App, Arg, SubCommand}; use clippy_dev::*; +use std::path::Path; mod fmt; mod new_lint; @@ -49,12 +50,12 @@ fn main() { .arg( Arg::with_name("check") .long("check") - .help("Checks that util/dev update_lints has been run. Used on CI."), + .help("Checks that `cargo dev update_lints` has been run. Used on CI."), ), ) .subcommand( SubCommand::with_name("new_lint") - .about("Create new lint and run util/dev update_lints") + .about("Create new lint and run `cargo dev update_lints`") .arg( Arg::with_name("pass") .short("p") @@ -170,7 +171,7 @@ fn update_lints(update_mode: &UpdateMode) { sorted_usable_lints.sort_by_key(|lint| lint.name.clone()); let mut file_change = replace_region_in_file( - "../src/lintlist/mod.rs", + Path::new("src/lintlist/mod.rs"), "begin lint list", "end lint list", false, @@ -189,7 +190,7 @@ fn update_lints(update_mode: &UpdateMode) { .changed; file_change |= replace_region_in_file( - "../README.md", + Path::new("README.md"), r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#, "", true, @@ -202,7 +203,7 @@ fn update_lints(update_mode: &UpdateMode) { ).changed; file_change |= replace_region_in_file( - "../CHANGELOG.md", + Path::new("CHANGELOG.md"), "<!-- begin autogenerated links to lint list -->", "<!-- end autogenerated links to lint list -->", false, @@ -212,7 +213,7 @@ fn update_lints(update_mode: &UpdateMode) { .changed; file_change |= replace_region_in_file( - "../clippy_lints/src/lib.rs", + Path::new("clippy_lints/src/lib.rs"), "begin deprecated lints", "end deprecated lints", false, @@ -222,7 +223,7 @@ fn update_lints(update_mode: &UpdateMode) { .changed; file_change |= replace_region_in_file( - "../clippy_lints/src/lib.rs", + Path::new("clippy_lints/src/lib.rs"), "begin register lints", "end register lints", false, @@ -232,7 +233,7 @@ fn update_lints(update_mode: &UpdateMode) { .changed; file_change |= replace_region_in_file( - "../clippy_lints/src/lib.rs", + Path::new("clippy_lints/src/lib.rs"), "begin lints modules", "end lints modules", false, @@ -243,7 +244,7 @@ fn update_lints(update_mode: &UpdateMode) { // Generate lists of lints in the clippy::all lint group file_change |= replace_region_in_file( - "../clippy_lints/src/lib.rs", + Path::new("clippy_lints/src/lib.rs"), r#"store.register_group\(true, "clippy::all""#, r#"\]\);"#, false, @@ -266,7 +267,7 @@ fn update_lints(update_mode: &UpdateMode) { // Generate the list of lints for all other lint groups for (lint_group, lints) in Lint::by_lint_group(&usable_lints) { file_change |= replace_region_in_file( - "../clippy_lints/src/lib.rs", + Path::new("clippy_lints/src/lib.rs"), &format!("store.register_group\\(true, \"clippy::{}\"", lint_group), r#"\]\);"#, false, @@ -279,7 +280,7 @@ fn update_lints(update_mode: &UpdateMode) { if update_mode == &UpdateMode::Check && file_change { println!( "Not all lints defined properly. \ - Please run `util/dev update_lints` to make sure all lints are defined properly." + Please run `cargo dev update_lints` to make sure all lints are defined properly." ); std::process::exit(1); } diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs index 76b8dd60d43..9d1f19f954b 100644 --- a/clippy_dev/src/new_lint.rs +++ b/clippy_dev/src/new_lint.rs @@ -1,8 +1,9 @@ +use clippy_dev::clippy_project_root; use std::fs::{File, OpenOptions}; use std::io; use std::io::prelude::*; use std::io::ErrorKind; -use std::path::{Path, PathBuf}; +use std::path::Path; pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>) -> Result<(), io::Error> { let pass = pass.expect("`pass` argument is validated by clap"); @@ -55,7 +56,7 @@ pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str } fn open_files(lint_name: &str) -> Result<(File, File), io::Error> { - let project_root = project_root()?; + let project_root = clippy_project_root(); let test_file_path = project_root.join("tests").join("ui").join(format!("{}.rs", lint_name)); let lint_file_path = project_root @@ -82,24 +83,6 @@ fn open_files(lint_name: &str) -> Result<(File, File), io::Error> { Ok((test_file, lint_file)) } -fn project_root() -> Result<PathBuf, io::Error> { - let current_dir = std::env::current_dir()?; - 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?; - if content.contains("[package]\nname = \"clippy\"") { - return Ok(path.to_path_buf()); - } - } - Err(io::Error::new(ErrorKind::Other, "Unable to find project root")) -} - fn to_camel_case(name: &str) -> String { name.split('_') .map(|s| { |
