Skip to content

Commit f3dbae6

Browse files
committed
Change cargo dev fmt fallback
1 parent 2667f4a commit f3dbae6

File tree

1 file changed

+12
-45
lines changed

1 file changed

+12
-45
lines changed

clippy_dev/src/fmt.rs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use itertools::Itertools;
33
use rustc_lexer::{TokenKind, tokenize};
44
use shell_escape::escape;
55
use std::ffi::{OsStr, OsString};
6-
use std::io::BufRead;
76
use std::ops::ControlFlow;
87
use std::path::{Path, PathBuf};
98
use std::process::{self, Command, Stdio};
@@ -266,42 +265,6 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
266265
Ok(())
267266
}
268267

269-
fn find_rustfmt() -> Result<String, Error> {
270-
#[cfg(windows)]
271-
const WHICH_BIN: &str = "where";
272-
273-
#[cfg(not(windows))]
274-
const WHICH_BIN: &str = "which";
275-
276-
let output = Command::new("rustup")
277-
.args(["which", "rustfmt"])
278-
.stderr(Stdio::inherit())
279-
.output();
280-
281-
if let Ok(output) = output {
282-
if output.status.success() {
283-
return Ok(String::from_utf8(output.stdout).expect("invalid rustfmt path"));
284-
}
285-
}
286-
287-
// `rustup which` has failed, fallback to platform-specific built-in command
288-
let output = Command::new(WHICH_BIN)
289-
.args(["rustfmt"])
290-
.stderr(Stdio::inherit())
291-
.output();
292-
293-
if let Ok(output) = output {
294-
if output.status.success() {
295-
// Since `where` can display multiple results we want to ignore all but one
296-
if let Some(Ok(line)) = output.stdout.lines().next() {
297-
return Ok(line);
298-
}
299-
}
300-
}
301-
302-
Err(Error::RustfmtNotInstalled)
303-
}
304-
305268
fn run_rustfmt(context: &FmtContext) -> Result<(), Error> {
306269
let project_root = clippy_project_root();
307270

@@ -344,14 +307,18 @@ fn run_rustfmt(context: &FmtContext) -> Result<(), Error> {
344307

345308
// the "main" function of cargo dev fmt
346309
pub fn run(check: bool, verbose: bool) {
347-
let mut rustfmt_path;
348-
match find_rustfmt() {
349-
Ok(path) => rustfmt_path = path,
350-
Err(e) => {
351-
e.display();
352-
process::exit(1);
353-
},
354-
}
310+
let result = Command::new("rustup")
311+
.args(["which", "rustfmt"])
312+
.stderr(Stdio::inherit())
313+
.output();
314+
315+
let mut rustfmt_path = if let Ok(output) = result
316+
&& output.status.success()
317+
{
318+
String::from_utf8(output.stdout).expect("invalid rustfmt path")
319+
} else {
320+
String::from("rustfmt")
321+
};
355322

356323
rustfmt_path.truncate(rustfmt_path.trim_end().len());
357324

0 commit comments

Comments
 (0)