1 use crate::clippy_project_root;
2 use shell_escape::escape;
6 use std::process::{self, Command};
11 CommandFailed(String),
14 WalkDirError(walkdir::Error),
17 impl From<io::Error> for CliError {
18 fn from(error: io::Error) -> Self {
23 impl From<walkdir::Error> for CliError {
24 fn from(error: walkdir::Error) -> Self {
25 Self::WalkDirError(error)
34 pub fn run(check: bool, verbose: bool) {
35 fn try_run(context: &FmtContext) -> Result<bool, CliError> {
36 let mut success = true;
38 let project_root = clippy_project_root();
40 rustfmt_test(context)?;
42 success &= cargo_fmt(context, project_root.as_path())?;
43 success &= cargo_fmt(context, &project_root.join("clippy_dev"))?;
44 success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
46 for entry in WalkDir::new(project_root.join("tests")) {
48 let path = entry.path();
50 if path.extension() != Some("rs".as_ref())
51 || entry.file_name() == "ice-3891.rs"
52 // Avoid rustfmt bug rust-lang/rustfmt#1873
53 || cfg!(windows) && entry.file_name() == "implicit_hasher.rs"
58 success &= rustfmt(context, &path)?;
64 fn output_err(err: CliError) {
66 CliError::CommandFailed(command) => {
67 eprintln!("error: A command failed! `{}`", command);
69 CliError::IoError(err) => {
70 eprintln!("error: {}", err);
72 CliError::RustfmtNotInstalled => {
73 eprintln!("error: rustfmt nightly is not installed.");
75 CliError::WalkDirError(err) => {
76 eprintln!("error: {}", err);
81 let context = FmtContext { check, verbose };
82 let result = try_run(&context);
83 let code = match result {
87 eprintln!("Formatting check failed.");
88 eprintln!("Run `cargo dev fmt` to update formatting.");
99 fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
100 let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
104 escape(dir.as_ref().to_string_lossy()),
105 escape(program.as_ref().to_string_lossy()),
106 arg_display.join(" ")
111 context: &FmtContext,
112 program: impl AsRef<OsStr>,
113 dir: impl AsRef<Path>,
114 args: &[impl AsRef<OsStr>],
115 ) -> Result<bool, CliError> {
117 println!("{}", format_command(&program, &dir, args));
120 let mut child = Command::new(&program).current_dir(&dir).args(args.iter()).spawn()?;
121 let code = child.wait()?;
122 let success = code.success();
124 if !context.check && !success {
125 return Err(CliError::CommandFailed(format_command(&program, &dir, args)));
131 fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
132 let mut args = vec!["+nightly", "fmt", "--all"];
135 args.push("--check");
137 let success = exec(context, "cargo", path, &args)?;
142 fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
143 let program = "rustfmt";
144 let dir = std::env::current_dir()?;
145 let args = &["+nightly", "--version"];
148 println!("{}", format_command(&program, &dir, args));
151 let output = Command::new(&program).current_dir(&dir).args(args.iter()).output()?;
153 if output.status.success() {
155 } else if std::str::from_utf8(&output.stderr)
157 .starts_with("error: 'rustfmt' is not installed")
159 Err(CliError::RustfmtNotInstalled)
161 Err(CliError::CommandFailed(format_command(&program, &dir, args)))
165 fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
166 let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
168 args.push("--check".as_ref());
170 let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
172 eprintln!("rustfmt failed on {}", path.display());