Skip to content

Commit ca2abaf

Browse files
committed
Auto merge of rust-lang#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
2 parents 8e9089e + 8f457fa commit ca2abaf

File tree

12 files changed

+61
-73
lines changed

12 files changed

+61
-73
lines changed

.cargo/config

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[alias]
22
uitest = "test --test compile-test"
3+
dev = "run --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
34

45
[build]
56
rustflags = ["-Zunstable-options"]

.github/PULL_REQUEST_TEMPLATE.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ checked during review or continuous integration.
1515
- [ ] Followed [lint naming conventions][lint_naming]
1616
- [ ] Added passing UI tests (including committed `.stderr` file)
1717
- [ ] `cargo test` passes locally
18-
- [ ] Executed `./util/dev update_lints`
18+
- [ ] Executed `cargo dev update_lints`
1919
- [ ] Added lint documentation
20-
- [ ] Run `./util/dev fmt`
20+
- [ ] Run `cargo dev fmt`
2121

2222
[lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints
2323

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry) {
105105

106106
The [`plugin::PluginRegistry`][plugin_registry] provides two methods to register lints: [register_early_lint_pass][reg_early_lint_pass] and [register_late_lint_pass][reg_late_lint_pass].
107107
Both take an object that implements an [`EarlyLintPass`][early_lint_pass] or [`LateLintPass`][late_lint_pass] respectively. This is done in every single lint.
108-
It's worth noting that the majority of `clippy_lints/src/lib.rs` is autogenerated by `util/dev update_lints` and you don't have to add anything by hand. When you are writing your own lint, you can use that script to save you some time.
108+
It's worth noting that the majority of `clippy_lints/src/lib.rs` is autogenerated by `cargo dev update_lints` and you don't have to add anything by hand. When you are writing your own lint, you can use that script to save you some time.
109109

110110
```rust
111111
// ./clippy_lints/src/else_if_without_else.rs

ci/base-tests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ cargo test --features deny-warnings
2222
)
2323

2424
# Perform various checks for lint registration
25-
./util/dev update_lints --check
26-
./util/dev --limit-stderr-length
25+
cargo dev update_lints --check
26+
cargo dev --limit-stderr-length
2727

2828
# Check running clippy-driver without cargo
2929
(

clippy_dev/src/fmt.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
use clippy_dev::clippy_project_root;
12
use shell_escape::escape;
23
use std::ffi::OsStr;
34
use std::io;
4-
use std::path::{Path, PathBuf};
5+
use std::path::Path;
56
use std::process::{self, Command};
67
use walkdir::WalkDir;
78

89
#[derive(Debug)]
910
pub enum CliError {
1011
CommandFailed(String),
1112
IoError(io::Error),
12-
ProjectRootNotFound,
1313
RustfmtNotInstalled,
1414
WalkDirError(walkdir::Error),
1515
}
@@ -35,7 +35,7 @@ pub fn run(check: bool, verbose: bool) {
3535
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
3636
let mut success = true;
3737

38-
let project_root = project_root()?;
38+
let project_root = clippy_project_root();
3939

4040
rustfmt_test(context)?;
4141

@@ -69,9 +69,6 @@ pub fn run(check: bool, verbose: bool) {
6969
CliError::IoError(err) => {
7070
eprintln!("error: {}", err);
7171
},
72-
CliError::ProjectRootNotFound => {
73-
eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
74-
},
7572
CliError::RustfmtNotInstalled => {
7673
eprintln!("error: rustfmt nightly is not installed.");
7774
},
@@ -88,7 +85,7 @@ pub fn run(check: bool, verbose: bool) {
8885
Ok(false) => {
8986
eprintln!();
9087
eprintln!("Formatting check failed.");
91-
eprintln!("Run `./util/dev fmt` to update formatting.");
88+
eprintln!("Run `cargo dev fmt` to update formatting.");
9289
1
9390
},
9491
Err(err) => {
@@ -176,22 +173,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
176173
}
177174
Ok(success)
178175
}
179-
180-
fn project_root() -> Result<PathBuf, CliError> {
181-
let current_dir = std::env::current_dir()?;
182-
for path in current_dir.ancestors() {
183-
let result = std::fs::read_to_string(path.join("Cargo.toml"));
184-
if let Err(err) = &result {
185-
if err.kind() == io::ErrorKind::NotFound {
186-
continue;
187-
}
188-
}
189-
190-
let content = result?;
191-
if content.contains("[package]\nname = \"clippy\"") {
192-
return Ok(path.to_path_buf());
193-
}
194-
}
195-
196-
Err(CliError::ProjectRootNotFound)
197-
}

clippy_dev/src/lib.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::collections::HashMap;
77
use std::ffi::OsStr;
88
use std::fs;
99
use std::io::prelude::*;
10+
use std::path::{Path, PathBuf};
1011
use walkdir::WalkDir;
1112

1213
lazy_static! {
@@ -205,7 +206,8 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item = Lint> {
205206
fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
206207
// We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
207208
// Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`.
208-
WalkDir::new("../clippy_lints/src")
209+
let path = clippy_project_root().join("clippy_lints/src");
210+
WalkDir::new(path)
209211
.into_iter()
210212
.filter_map(std::result::Result::ok)
211213
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
@@ -225,7 +227,7 @@ pub struct FileChange {
225227
/// See `replace_region_in_text` for documentation of the other options.
226228
#[allow(clippy::expect_fun_call)]
227229
pub fn replace_region_in_file<F>(
228-
path: &str,
230+
path: &Path,
229231
start: &str,
230232
end: &str,
231233
replace_start: bool,
@@ -235,14 +237,15 @@ pub fn replace_region_in_file<F>(
235237
where
236238
F: Fn() -> Vec<String>,
237239
{
238-
let mut f = fs::File::open(path).expect(&format!("File not found: {}", path));
240+
let path = clippy_project_root().join(path);
241+
let mut f = fs::File::open(&path).expect(&format!("File not found: {}", path.to_string_lossy()));
239242
let mut contents = String::new();
240243
f.read_to_string(&mut contents)
241244
.expect("Something went wrong reading the file");
242245
let file_change = replace_region_in_text(&contents, start, end, replace_start, replacements);
243246

244247
if write_back {
245-
let mut f = fs::File::create(path).expect(&format!("File not found: {}", path));
248+
let mut f = fs::File::create(&path).expect(&format!("File not found: {}", path.to_string_lossy()));
246249
f.write_all(file_change.new_lines.as_bytes())
247250
.expect("Unable to write file");
248251
// Ensure we write the changes with a trailing newline so that
@@ -318,6 +321,26 @@ where
318321
}
319322
}
320323

324+
/// Returns the path to the Clippy project directory
325+
#[must_use]
326+
pub fn clippy_project_root() -> PathBuf {
327+
let current_dir = std::env::current_dir().unwrap();
328+
for path in current_dir.ancestors() {
329+
let result = std::fs::read_to_string(path.join("Cargo.toml"));
330+
if let Err(err) = &result {
331+
if err.kind() == std::io::ErrorKind::NotFound {
332+
continue;
333+
}
334+
}
335+
336+
let content = result.unwrap();
337+
if content.contains("[package]\nname = \"clippy\"") {
338+
return path.to_path_buf();
339+
}
340+
}
341+
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
342+
}
343+
321344
#[test]
322345
fn test_parse_contents() {
323346
let result: Vec<Lint> = parse_contents(

clippy_dev/src/main.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use clap::{App, Arg, SubCommand};
44
use clippy_dev::*;
5+
use std::path::Path;
56

67
mod fmt;
78
mod new_lint;
@@ -49,12 +50,12 @@ fn main() {
4950
.arg(
5051
Arg::with_name("check")
5152
.long("check")
52-
.help("Checks that util/dev update_lints has been run. Used on CI."),
53+
.help("Checks that `cargo dev update_lints` has been run. Used on CI."),
5354
),
5455
)
5556
.subcommand(
5657
SubCommand::with_name("new_lint")
57-
.about("Create new lint and run util/dev update_lints")
58+
.about("Create new lint and run `cargo dev update_lints`")
5859
.arg(
5960
Arg::with_name("pass")
6061
.short("p")
@@ -170,7 +171,7 @@ fn update_lints(update_mode: &UpdateMode) {
170171
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
171172

172173
let mut file_change = replace_region_in_file(
173-
"../src/lintlist/mod.rs",
174+
Path::new("src/lintlist/mod.rs"),
174175
"begin lint list",
175176
"end lint list",
176177
false,
@@ -189,7 +190,7 @@ fn update_lints(update_mode: &UpdateMode) {
189190
.changed;
190191

191192
file_change |= replace_region_in_file(
192-
"../README.md",
193+
Path::new("README.md"),
193194
r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang.github.io/rust-clippy/master/index.html\)"#,
194195
"",
195196
true,
@@ -202,7 +203,7 @@ fn update_lints(update_mode: &UpdateMode) {
202203
).changed;
203204

204205
file_change |= replace_region_in_file(
205-
"../CHANGELOG.md",
206+
Path::new("CHANGELOG.md"),
206207
"<!-- begin autogenerated links to lint list -->",
207208
"<!-- end autogenerated links to lint list -->",
208209
false,
@@ -212,7 +213,7 @@ fn update_lints(update_mode: &UpdateMode) {
212213
.changed;
213214

214215
file_change |= replace_region_in_file(
215-
"../clippy_lints/src/lib.rs",
216+
Path::new("clippy_lints/src/lib.rs"),
216217
"begin deprecated lints",
217218
"end deprecated lints",
218219
false,
@@ -222,7 +223,7 @@ fn update_lints(update_mode: &UpdateMode) {
222223
.changed;
223224

224225
file_change |= replace_region_in_file(
225-
"../clippy_lints/src/lib.rs",
226+
Path::new("clippy_lints/src/lib.rs"),
226227
"begin register lints",
227228
"end register lints",
228229
false,
@@ -232,7 +233,7 @@ fn update_lints(update_mode: &UpdateMode) {
232233
.changed;
233234

234235
file_change |= replace_region_in_file(
235-
"../clippy_lints/src/lib.rs",
236+
Path::new("clippy_lints/src/lib.rs"),
236237
"begin lints modules",
237238
"end lints modules",
238239
false,
@@ -243,7 +244,7 @@ fn update_lints(update_mode: &UpdateMode) {
243244

244245
// Generate lists of lints in the clippy::all lint group
245246
file_change |= replace_region_in_file(
246-
"../clippy_lints/src/lib.rs",
247+
Path::new("clippy_lints/src/lib.rs"),
247248
r#"store.register_group\(true, "clippy::all""#,
248249
r#"\]\);"#,
249250
false,
@@ -266,7 +267,7 @@ fn update_lints(update_mode: &UpdateMode) {
266267
// Generate the list of lints for all other lint groups
267268
for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
268269
file_change |= replace_region_in_file(
269-
"../clippy_lints/src/lib.rs",
270+
Path::new("clippy_lints/src/lib.rs"),
270271
&format!("store.register_group\\(true, \"clippy::{}\"", lint_group),
271272
r#"\]\);"#,
272273
false,
@@ -279,7 +280,7 @@ fn update_lints(update_mode: &UpdateMode) {
279280
if update_mode == &UpdateMode::Check && file_change {
280281
println!(
281282
"Not all lints defined properly. \
282-
Please run `util/dev update_lints` to make sure all lints are defined properly."
283+
Please run `cargo dev update_lints` to make sure all lints are defined properly."
283284
);
284285
std::process::exit(1);
285286
}

clippy_dev/src/new_lint.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use clippy_dev::clippy_project_root;
12
use std::fs::{File, OpenOptions};
23
use std::io;
34
use std::io::prelude::*;
45
use std::io::ErrorKind;
5-
use std::path::{Path, PathBuf};
6+
use std::path::Path;
67

78
pub fn create(pass: Option<&str>, lint_name: Option<&str>, category: Option<&str>) -> Result<(), io::Error> {
89
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
5556
}
5657

5758
fn open_files(lint_name: &str) -> Result<(File, File), io::Error> {
58-
let project_root = project_root()?;
59+
let project_root = clippy_project_root();
5960

6061
let test_file_path = project_root.join("tests").join("ui").join(format!("{}.rs", lint_name));
6162
let lint_file_path = project_root
@@ -82,24 +83,6 @@ fn open_files(lint_name: &str) -> Result<(File, File), io::Error> {
8283
Ok((test_file, lint_file))
8384
}
8485

85-
fn project_root() -> Result<PathBuf, io::Error> {
86-
let current_dir = std::env::current_dir()?;
87-
for path in current_dir.ancestors() {
88-
let result = std::fs::read_to_string(path.join("Cargo.toml"));
89-
if let Err(err) = &result {
90-
if err.kind() == io::ErrorKind::NotFound {
91-
continue;
92-
}
93-
}
94-
95-
let content = result?;
96-
if content.contains("[package]\nname = \"clippy\"") {
97-
return Ok(path.to_path_buf());
98-
}
99-
}
100-
Err(io::Error::new(ErrorKind::Other, "Unable to find project root"))
101-
}
102-
10386
fn to_camel_case(name: &str) -> String {
10487
name.split('_')
10588
.map(|s| {

doc/adding_lints.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ lint. Fortunately, you can use the clippy dev tools to handle this for you. We
3939
are naming our new lint `foo_functions` (lints are generally written in snake
4040
case), and we don't need type information so it will have an early pass type
4141
(more on this later on). To get started on this lint you can run
42-
`./util/dev new_lint --name=foo_functions --pass=early --category=pedantic`
42+
`cargo dev new_lint --name=foo_functions --pass=early --category=pedantic`
4343
(category will default to nursery if not provided). This command will create
4444
two files: `tests/ui/foo_functions.rs` and `clippy_lints/src/foo_functions.rs`,
45-
as well as run `./util/dev update_lints` to register the new lint. Next, we'll
45+
as well as run `cargo dev update_lints` to register the new lint. Next, we'll
4646
open up these files and add our lint!
4747

4848
### Testing
@@ -386,7 +386,7 @@ It can be installed via `rustup`:
386386
rustup component add rustfmt --toolchain=nightly
387387
```
388388

389-
Use `./util/dev fmt` to format the whole codebase. Make sure that `rustfmt` is
389+
Use `cargo dev fmt` to format the whole codebase. Make sure that `rustfmt` is
390390
installed for the nightly toolchain.
391391

392392
### Debugging
@@ -404,9 +404,9 @@ Before submitting your PR make sure you followed all of the basic requirements:
404404
- [ ] Followed [lint naming conventions][lint_naming]
405405
- [ ] Added passing UI tests (including committed `.stderr` file)
406406
- [ ] `cargo test` passes locally
407-
- [ ] Executed `./util/dev update_lints`
407+
- [ ] Executed `cargo dev update_lints`
408408
- [ ] Added lint documentation
409-
- [ ] Run `./util/dev fmt`
409+
- [ ] Run `cargo dev fmt`
410410

411411
### Cheatsheet
412412

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! This file is managed by `util/dev update_lints`. Do not edit.
1+
//! This file is managed by `cargo dev update_lints`. Do not edit.
22
33
pub mod lint;
44
pub use lint::Level;

tests/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ fn fmt() {
3434

3535
assert!(
3636
output.status.success(),
37-
"Formatting check failed. Run `./util/dev fmt` to update formatting."
37+
"Formatting check failed. Run `cargo dev fmt` to update formatting."
3838
);
3939
}

util/dev

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
CARGO_TARGET_DIR=$(pwd)/target/
33
export CARGO_TARGET_DIR
44

5+
echo 'Deprecated! `util/dev` usage is deprecated, please use `cargo dev` instead.'
6+
57
cd clippy_dev && cargo run -- "$@"

0 commit comments

Comments
 (0)