Skip to content

Commit

Permalink
Merge pull request #35 from dtolnay/clippydriver
Browse files Browse the repository at this point in the history
Pass --rustc argument to clippy driver
  • Loading branch information
dtolnay committed Jul 16, 2022
2 parents 1b25bae + 951d351 commit d2b8708
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
70 changes: 42 additions & 28 deletions build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,51 @@ use std::process::{self, Command};

fn main() {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
let output = match Command::new(&rustc).arg("--version").output() {
Ok(output) => output,
Err(e) => {
let rustc = rustc.to_string_lossy();
eprintln!("Error: failed to run `{} --version`: {}", rustc, e);
process::exit(1);
}
};

let string = match String::from_utf8(output.stdout) {
Ok(string) => string,
Err(e) => {
let rustc = rustc.to_string_lossy();
eprintln!(
"Error: failed to parse output of `{} --version`: {}",
rustc, e,
);
process::exit(1);
let mut is_clippy_driver = false;
let version = loop {
let mut command = Command::new(&rustc);
if is_clippy_driver {
command.arg("--rustc");
}
};
command.arg("--version");

let version = match rustc::parse(&string) {
rustc::ParseResult::Success(version) => version,
rustc::ParseResult::Unrecognized => {
eprintln!(
"Error: unexpected output from `rustc --version`: {:?}\n\n\
Please file an issue in https://github.com/dtolnay/rustversion",
string
);
process::exit(1);
}
let output = match command.output() {
Ok(output) => output,
Err(e) => {
let rustc = rustc.to_string_lossy();
eprintln!("Error: failed to run `{} --version`: {}", rustc, e);
process::exit(1);
}
};

let string = match String::from_utf8(output.stdout) {
Ok(string) => string,
Err(e) => {
let rustc = rustc.to_string_lossy();
eprintln!(
"Error: failed to parse output of `{} --version`: {}",
rustc, e,
);
process::exit(1);
}
};

break match rustc::parse(&string) {
rustc::ParseResult::Success(version) => version,
rustc::ParseResult::OopsClippy if !is_clippy_driver => {
is_clippy_driver = true;
continue;
}
rustc::ParseResult::Unrecognized | rustc::ParseResult::OopsClippy => {
eprintln!(
"Error: unexpected output from `rustc --version`: {:?}\n\n\
Please file an issue in https://github.com/dtolnay/rustversion",
string
);
process::exit(1);
}
};
};

if version.minor < 38 {
Expand Down
7 changes: 5 additions & 2 deletions build/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{self, Debug};

pub enum ParseResult {
Success(Version),
OopsClippy,
Unrecognized,
}

Expand Down Expand Up @@ -32,8 +33,10 @@ pub fn parse(string: &str) -> ParseResult {
let last_line = string.lines().last().unwrap_or(string);
let mut words = last_line.trim().split(' ');

if words.next() != Some("rustc") {
return ParseResult::Unrecognized;
match words.next() {
Some("rustc") => {}
Some(word) if word.starts_with("clippy") => return ParseResult::OopsClippy,
Some(_) | None => return ParseResult::Unrecognized,
}

parse_words(&mut words).map_or(ParseResult::Unrecognized, ParseResult::Success)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ fn test_parse() {
for (string, expected) in cases {
match parse(string) {
ParseResult::Success(version) => assert_eq!(version, *expected),
ParseResult::Unrecognized => panic!("unrecognized: {:?}", string),
ParseResult::OopsClippy | ParseResult::Unrecognized => {
panic!("unrecognized: {:?}", string);
}
}
}
}

0 comments on commit d2b8708

Please sign in to comment.