Skip to content

Commit c8b5e72

Browse files
committed
Auto merge of #4232 - mikerite:dev-fmt-4, r=flip1995
Add dev fmt subcommand changelog: none
2 parents a9f8d3a + 9ba054d commit c8b5e72

File tree

7 files changed

+239
-53
lines changed

7 files changed

+239
-53
lines changed

ci/base-tests.sh

-30
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export CARGO_TARGET_DIR=`pwd`/target/
2424
# Perform various checks for lint registration
2525
./util/dev update_lints --check
2626
./util/dev --limit-stderr-length
27-
cargo +nightly fmt --all -- --check
2827

2928
# Check running clippy-driver without cargo
3029
(
@@ -50,32 +49,3 @@ cargo +nightly fmt --all -- --check
5049

5150
# TODO: CLIPPY_CONF_DIR / CARGO_MANIFEST_DIR
5251
)
53-
54-
# make sure tests are formatted
55-
56-
# some lints are sensitive to formatting, exclude some files
57-
tests_need_reformatting="false"
58-
# switch to nightly
59-
rustup override set nightly
60-
# avoid loop spam and allow cmds with exit status != 0
61-
set +ex
62-
63-
# Excluding `ice-3891.rs` because the code triggers a rustc parse error which
64-
# makes rustfmt fail.
65-
for file in `find tests -not -path "tests/ui/crashes/ice-3891.rs" | grep "\.rs$"` ; do
66-
rustfmt ${file} --check
67-
if [ $? -ne 0 ]; then
68-
echo "${file} needs reformatting!"
69-
tests_need_reformatting="true"
70-
fi
71-
done
72-
73-
set -ex # reset
74-
75-
if [ "${tests_need_reformatting}" == "true" ] ; then
76-
echo "Tests need reformatting!"
77-
exit 2
78-
fi
79-
80-
# switch back to master
81-
rustup override set master

clippy_dev/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ clap = "2.33"
99
itertools = "0.8"
1010
regex = "1"
1111
lazy_static = "1.0"
12+
shell-escape = "0.1"
1213
walkdir = "2"

clippy_dev/src/fmt.rs

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
use shell_escape::escape;
2+
use std::ffi::OsStr;
3+
use std::io;
4+
use std::path::{Path, PathBuf};
5+
use std::process::{self, Command};
6+
use walkdir::WalkDir;
7+
8+
#[derive(Debug)]
9+
pub enum CliError {
10+
CommandFailed(String),
11+
IoError(io::Error),
12+
ProjectRootNotFound,
13+
WalkDirError(walkdir::Error),
14+
}
15+
16+
impl From<io::Error> for CliError {
17+
fn from(error: io::Error) -> Self {
18+
CliError::IoError(error)
19+
}
20+
}
21+
22+
impl From<walkdir::Error> for CliError {
23+
fn from(error: walkdir::Error) -> Self {
24+
CliError::WalkDirError(error)
25+
}
26+
}
27+
28+
struct FmtContext {
29+
check: bool,
30+
verbose: bool,
31+
}
32+
33+
pub fn run(check: bool, verbose: bool) {
34+
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
35+
let mut success = true;
36+
37+
let project_root = project_root()?;
38+
39+
success &= cargo_fmt(context, project_root.as_path())?;
40+
success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
41+
success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
42+
43+
for entry in WalkDir::new(project_root.join("tests")) {
44+
let entry = entry?;
45+
let path = entry.path();
46+
47+
if path.extension() != Some("rs".as_ref()) || entry.file_name() == "ice-3891.rs" {
48+
continue;
49+
}
50+
51+
success &= rustfmt(context, &path)?;
52+
}
53+
54+
Ok(success)
55+
}
56+
57+
fn output_err(err: CliError) {
58+
match err {
59+
CliError::CommandFailed(command) => {
60+
eprintln!("error: A command failed! `{}`", command);
61+
},
62+
CliError::IoError(err) => {
63+
eprintln!("error: {}", err);
64+
},
65+
CliError::ProjectRootNotFound => {
66+
eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
67+
},
68+
CliError::WalkDirError(err) => {
69+
eprintln!("error: {}", err);
70+
},
71+
}
72+
}
73+
74+
let context = FmtContext { check, verbose };
75+
let result = try_run(&context);
76+
let code = match result {
77+
Ok(true) => 0,
78+
Ok(false) => {
79+
eprintln!();
80+
eprintln!("Formatting check failed.");
81+
eprintln!("Run `./util/dev fmt` to update formatting.");
82+
1
83+
},
84+
Err(err) => {
85+
output_err(err);
86+
1
87+
},
88+
};
89+
process::exit(code);
90+
}
91+
92+
fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
93+
let arg_display: Vec<_> = args
94+
.iter()
95+
.map(|a| escape(a.as_ref().to_string_lossy()).to_owned())
96+
.collect();
97+
98+
format!(
99+
"cd {} && {} {}",
100+
escape(dir.as_ref().to_string_lossy()),
101+
escape(program.as_ref().to_string_lossy()),
102+
arg_display.join(" ")
103+
)
104+
}
105+
106+
fn exec(
107+
context: &FmtContext,
108+
program: impl AsRef<OsStr>,
109+
dir: impl AsRef<Path>,
110+
args: &[impl AsRef<OsStr>],
111+
) -> Result<bool, CliError> {
112+
if context.verbose {
113+
println!("{}", format_command(&program, &dir, args));
114+
}
115+
116+
let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
117+
let code = child.wait()?;
118+
let success = code.success();
119+
120+
if !context.check && !success {
121+
return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
122+
}
123+
124+
Ok(success)
125+
}
126+
127+
fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
128+
let mut args = vec!["+nightly", "fmt", "--all"];
129+
if context.check {
130+
args.push("--");
131+
args.push("--check");
132+
}
133+
let success = exec(context, "cargo", path, &args)?;
134+
135+
Ok(success)
136+
}
137+
138+
fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
139+
let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
140+
if context.check {
141+
args.push("--check".as_ref());
142+
}
143+
let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
144+
if !success {
145+
eprintln!("rustfmt failed on {}", path.display());
146+
}
147+
Ok(success)
148+
}
149+
150+
fn project_root() -> Result<PathBuf, CliError> {
151+
let current_dir = std::env::current_dir()?;
152+
for path in current_dir.ancestors() {
153+
let result = std::fs::read_to_string(path.join("Cargo.toml"));
154+
if let Err(err) = &result {
155+
if err.kind() == io::ErrorKind::NotFound {
156+
continue;
157+
}
158+
}
159+
160+
let content = result?;
161+
if content.contains("[package]\nname = \"clippy\"") {
162+
return Ok(path.to_path_buf());
163+
}
164+
}
165+
166+
Err(CliError::ProjectRootNotFound)
167+
}

clippy_dev/src/main.rs

+32-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extern crate regex;
44

55
use clap::{App, Arg, SubCommand};
66
use clippy_dev::*;
7+
8+
mod fmt;
79
mod stderr_length_check;
810

911
#[derive(PartialEq)]
@@ -14,6 +16,21 @@ enum UpdateMode {
1416

1517
fn main() {
1618
let matches = App::new("Clippy developer tooling")
19+
.subcommand(
20+
SubCommand::with_name("fmt")
21+
.about("Run rustfmt on all projects and tests")
22+
.arg(
23+
Arg::with_name("check")
24+
.long("check")
25+
.help("Use the rustfmt --check option"),
26+
)
27+
.arg(
28+
Arg::with_name("verbose")
29+
.short("v")
30+
.long("verbose")
31+
.help("Echo commands run"),
32+
),
33+
)
1734
.subcommand(
1835
SubCommand::with_name("update_lints")
1936
.about("Updates lint registration and information from the source code")
@@ -46,14 +63,21 @@ fn main() {
4663
if matches.is_present("limit-stderr-length") {
4764
stderr_length_check::check();
4865
}
49-
if let Some(matches) = matches.subcommand_matches("update_lints") {
50-
if matches.is_present("print-only") {
51-
print_lints();
52-
} else if matches.is_present("check") {
53-
update_lints(&UpdateMode::Check);
54-
} else {
55-
update_lints(&UpdateMode::Change);
56-
}
66+
67+
match matches.subcommand() {
68+
("fmt", Some(matches)) => {
69+
fmt::run(matches.is_present("check"), matches.is_present("verbose"));
70+
},
71+
("update_lints", Some(matches)) => {
72+
if matches.is_present("print-only") {
73+
print_lints();
74+
} else if matches.is_present("check") {
75+
update_lints(&UpdateMode::Check);
76+
} else {
77+
update_lints(&UpdateMode::Change);
78+
}
79+
},
80+
_ => {},
5781
}
5882
}
5983

clippy_dev/src/stderr_length_check.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ pub fn check() {
2323
}
2424

2525
fn exceeding_stderr_files(files: impl Iterator<Item = walkdir::DirEntry>) -> impl Iterator<Item = String> {
26-
files
27-
.filter_map(|file| {
28-
let path = file.path().to_str().expect("Could not convert path to str").to_string();
29-
let linecount = count_linenumbers(&path);
30-
if linecount > LIMIT {
31-
Some(path)
32-
} else {
33-
None
34-
}
35-
})
26+
files.filter_map(|file| {
27+
let path = file.path().to_str().expect("Could not convert path to str").to_string();
28+
let linecount = count_linenumbers(&path);
29+
if linecount > LIMIT {
30+
Some(path)
31+
} else {
32+
None
33+
}
34+
})
3635
}
3736

3837
fn stderr_files() -> impl Iterator<Item = walkdir::DirEntry> {

doc/adding_lints.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,18 @@ list][lint_list].
345345

346346
### Running rustfmt
347347

348-
[Rustfmt](https://github.com./rust-lang/rustfmt) is a tool for formatting Rust code according
349-
to style guidelines. Your code has to be formatted by `rustfmt` before a PR can be merged.
348+
[Rustfmt](https://github.com./rust-lang/rustfmt) is a tool for formatting Rust
349+
code according to style guidelines. Your code has to be formatted by `rustfmt`
350+
before a PR can be merged. Clippy uses nightly `rustfmt` in the CI.
350351

351352
It can be installed via `rustup`:
352353

353354
```bash
354-
rustup component add rustfmt
355+
rustup component add rustfmt --toolchain=nightly
355356
```
356357

357-
Use `cargo fmt --all` to format the whole codebase.
358+
Use `./util/dev fmt` to format the whole codebase. Make sure that `rustfmt` is
359+
installed for the nightly toolchain.
358360

359361
### Debugging
360362

@@ -371,7 +373,7 @@ Before submitting your PR make sure you followed all of the basic requirements:
371373
- [ ] `cargo test` passes locally
372374
- [ ] Executed `util/dev update_lints`
373375
- [ ] Added lint documentation
374-
- [ ] Run `cargo fmt`
376+
- [ ] Run `./util/dev fmt`
375377

376378
### Cheatsheet
377379

tests/fmt.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[test]
2+
fn fmt() {
3+
if option_env!("RUSTC_TEST_SUITE").is_some() {
4+
return;
5+
}
6+
7+
let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
8+
let dev_dir = root_dir.join("clippy_dev");
9+
let output = std::process::Command::new("cargo")
10+
.current_dir(dev_dir)
11+
.args(&["+nightly", "run", "--", "fmt", "--check"])
12+
.output()
13+
.unwrap();
14+
15+
println!("status: {}", output.status);
16+
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
17+
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
18+
19+
assert!(
20+
output.status.success(),
21+
"Formatting check failed. Run `./util/dev fmt` to update formatting."
22+
);
23+
}

0 commit comments

Comments
 (0)