Skip to content

Pass --rustc argument to clippy driver #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}