Skip to content

compiletest: fix a couple clippy lint findings #75789

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
Aug 23, 2020
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
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn parse_expected(

// If we find `//~ ERROR foo` or something like that, skip the first word.
let kind = first_word.parse::<ErrorKind>().ok();
if let Some(_) = kind {
if kind.is_some() {
msg = &msg.trim_start().split_at(first_word.len()).1;
}

Expand Down
18 changes: 6 additions & 12 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,8 @@ impl EarlyProps {
// Ignore if actual version is smaller the minimum required
// version
actual_version < min_version
} else if line.starts_with("rust-lldb") && !config.lldb_native_rust {
true
} else {
false
line.starts_with("rust-lldb") && !config.lldb_native_rust
}
} else {
false
Expand Down Expand Up @@ -657,7 +655,6 @@ fn iter_header<R: Read>(testfile: &Path, cfg: Option<&str>, rdr: R, it: &mut dyn
it(ln[comment.len()..].trim_start());
}
}
return;
}

impl Config {
Expand Down Expand Up @@ -819,7 +816,7 @@ impl Config {
let name = line[prefix.len() + 1..].split(&[':', ' '][..]).next().unwrap();

let is_match = name == "test" ||
&self.target == name || // triple
self.target == name || // triple
util::matches_os(&self.target, name) || // target
util::matches_env(&self.target, name) || // env
self.target.ends_with(name) || // target and env
Expand Down Expand Up @@ -857,10 +854,7 @@ impl Config {
// Ensure the directive is a whole word. Do not match "ignore-x86" when
// the line says "ignore-x86_64".
line.starts_with(directive)
&& match line.as_bytes().get(directive.len()) {
None | Some(&b' ') | Some(&b':') => true,
_ => false,
}
&& matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
}

pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
Expand Down Expand Up @@ -901,9 +895,9 @@ impl Config {
}

fn expand_variables(mut value: String, config: &Config) -> String {
const CWD: &'static str = "{{cwd}}";
const SRC_BASE: &'static str = "{{src-base}}";
const BUILD_BASE: &'static str = "{{build-base}}";
const CWD: &str = "{{cwd}}";
const SRC_BASE: &str = "{{src-base}}";
const BUILD_BASE: &str = "{{build-base}}";

if value.contains(CWD) {
let cwd = env::current_dir().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn extract_rendered(output: &str) -> String {
if line.starts_with('{') {
if let Ok(diagnostic) = serde_json::from_str::<Diagnostic>(line) {
diagnostic.rendered
} else if let Ok(_) = serde_json::from_str::<ArtifactNotification>(line) {
} else if serde_json::from_str::<ArtifactNotification>(line).is_ok() {
// Ignore the notification.
None
} else {
Expand Down
40 changes: 17 additions & 23 deletions src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
cc: matches.opt_str("cc").unwrap(),
cxx: matches.opt_str("cxx").unwrap(),
cflags: matches.opt_str("cflags").unwrap(),
ar: matches.opt_str("ar").unwrap_or("ar".into()),
ar: matches.opt_str("ar").unwrap_or_else(|| String::from("ar")),
linker: matches.opt_str("linker"),
llvm_components: matches.opt_str("llvm-components").unwrap(),
nodejs: matches.opt_str("nodejs"),
Expand Down Expand Up @@ -361,17 +361,13 @@ pub fn run_tests(config: Config) {
}

fn configure_cdb(config: &Config) -> Option<Config> {
if config.cdb.is_none() {
return None;
}
config.cdb.as_ref()?;

Some(Config { debugger: Some(Debugger::Cdb), ..config.clone() })
}

fn configure_gdb(config: &Config) -> Option<Config> {
if config.gdb_version.is_none() {
return None;
}
config.gdb_version?;

if util::matches_env(&config.target, "msvc") {
return None;
Expand Down Expand Up @@ -405,9 +401,7 @@ fn configure_gdb(config: &Config) -> Option<Config> {
}

fn configure_lldb(config: &Config) -> Option<Config> {
if config.lldb_python_dir.is_none() {
return None;
}
config.lldb_python_dir.as_ref()?;

if let Some(350) = config.lldb_version {
println!(
Expand Down Expand Up @@ -455,7 +449,7 @@ pub fn make_tests(config: &Config, tests: &mut Vec<test::TestDescAndFn>) {
debug!("making tests from {:?}", config.src_base.display());
let inputs = common_inputs_stamp(config);
collect_tests_from_dir(config, &config.src_base, &PathBuf::new(), &inputs, tests)
.expect(&format!("Could not read tests from {}", config.src_base.display()));
.unwrap_or_else(|_| panic!("Could not read tests from {}", config.src_base.display()));
}

/// Returns a stamp constructed from input files common to all test cases.
Expand Down Expand Up @@ -588,7 +582,7 @@ fn make_test(config: &Config, testpaths: &TestPaths, inputs: &Stamp) -> Vec<test
let revisions = if early_props.revisions.is_empty() || config.mode == Mode::Incremental {
vec![None]
} else {
early_props.revisions.iter().map(|r| Some(r)).collect()
early_props.revisions.iter().map(Some).collect()
};
revisions
.into_iter()
Expand Down Expand Up @@ -735,24 +729,24 @@ fn make_test_closure(

/// Returns `true` if the given target is an Android target for the
/// purposes of GDB testing.
fn is_android_gdb_target(target: &String) -> bool {
match &target[..] {
"arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => true,
_ => false,
}
fn is_android_gdb_target(target: &str) -> bool {
matches!(
&target[..],
"arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android"
)
}

/// Returns `true` if the given target is a MSVC target for the purpouses of CDB testing.
fn is_pc_windows_msvc_target(target: &String) -> bool {
fn is_pc_windows_msvc_target(target: &str) -> bool {
target.ends_with("-pc-windows-msvc")
}

fn find_cdb(target: &String) -> Option<OsString> {
fn find_cdb(target: &str) -> Option<OsString> {
if !(cfg!(windows) && is_pc_windows_msvc_target(target)) {
return None;
}

let pf86 = env::var_os("ProgramFiles(x86)").or(env::var_os("ProgramFiles"))?;
let pf86 = env::var_os("ProgramFiles(x86)").or_else(|| env::var_os("ProgramFiles"))?;
let cdb_arch = if cfg!(target_arch = "x86") {
"x86"
} else if cfg!(target_arch = "x86_64") {
Expand All @@ -779,14 +773,14 @@ fn find_cdb(target: &String) -> Option<OsString> {
}

/// Returns Path to CDB
fn analyze_cdb(cdb: Option<String>, target: &String) -> Option<OsString> {
cdb.map(|s| OsString::from(s)).or(find_cdb(target))
fn analyze_cdb(cdb: Option<String>, target: &str) -> Option<OsString> {
cdb.map(OsString::from).or_else(|| find_cdb(target))
}

/// Returns (Path to GDB, GDB Version, GDB has Rust Support)
fn analyze_gdb(
gdb: Option<String>,
target: &String,
target: &str,
android_cross_path: &PathBuf,
) -> (Option<String>, Option<u32>, bool) {
#[cfg(not(windows))]
Expand Down
51 changes: 25 additions & 26 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub struct Mismatch {

impl Mismatch {
fn new(line_number: u32) -> Mismatch {
Mismatch { line_number: line_number, lines: Vec::new() }
Mismatch { line_number, lines: Vec::new() }
}
}

Expand Down Expand Up @@ -199,7 +199,7 @@ fn write_diff(expected: &str, actual: &str, context_size: usize) -> String {
}
}
}
writeln!(output, "").unwrap();
writeln!(output).unwrap();
}
output
}
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
debug!("running {:?}", testpaths.file.display());
let props = TestProps::from_file(&testpaths.file, revision, &config);

let cx = TestCx { config: &config, props: &props, testpaths, revision: revision };
let cx = TestCx { config: &config, props: &props, testpaths, revision };
create_dir_all(&cx.output_base_dir()).unwrap();

if config.mode == Incremental {
Expand Down Expand Up @@ -578,8 +578,8 @@ impl<'test> TestCx<'test> {
if self.props.pp_exact.is_some() {
// Now we have to care about line endings
let cr = "\r".to_owned();
actual = actual.replace(&cr, "").to_owned();
expected = expected.replace(&cr, "").to_owned();
actual = actual.replace(&cr, "");
expected = expected.replace(&cr, "");
}

self.compare_source(&expected, &actual);
Expand Down Expand Up @@ -740,7 +740,7 @@ impl<'test> TestCx<'test> {
let exe_file = self.make_exe_name();

let prefixes = {
static PREFIXES: &'static [&'static str] = &["cdb", "cdbg"];
static PREFIXES: &[&str] = &["cdb", "cdbg"];
// No "native rust support" variation for CDB yet.
PREFIXES
};
Expand Down Expand Up @@ -811,12 +811,12 @@ impl<'test> TestCx<'test> {
fn run_debuginfo_gdb_test_no_opt(&self) {
let prefixes = if self.config.gdb_native_rust {
// GDB with Rust
static PREFIXES: &'static [&'static str] = &["gdb", "gdbr"];
static PREFIXES: &[&str] = &["gdb", "gdbr"];
println!("NOTE: compiletest thinks it is using GDB with native rust support");
PREFIXES
} else {
// Generic GDB
static PREFIXES: &'static [&'static str] = &["gdb", "gdbg"];
static PREFIXES: &[&str] = &["gdb", "gdbg"];
println!("NOTE: compiletest thinks it is using GDB without native rust support");
PREFIXES
};
Expand Down Expand Up @@ -875,12 +875,12 @@ impl<'test> TestCx<'test> {
.arg(&exe_file)
.arg(&self.config.adb_test_dir)
.status()
.expect(&format!("failed to exec `{:?}`", adb_path));
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", adb_path));

Command::new(adb_path)
.args(&["forward", "tcp:5039", "tcp:5039"])
.status()
.expect(&format!("failed to exec `{:?}`", adb_path));
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", adb_path));

let adb_arg = format!(
"export LD_LIBRARY_PATH={}; \
Expand All @@ -897,7 +897,7 @@ impl<'test> TestCx<'test> {
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.spawn()
.expect(&format!("failed to exec `{:?}`", adb_path));
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", adb_path));

// Wait for the gdbserver to print out "Listening on port ..."
// at which point we know that it's started and then we can
Expand All @@ -922,7 +922,7 @@ impl<'test> TestCx<'test> {
let Output { status, stdout, stderr } = Command::new(&gdb_path)
.args(debugger_opts)
.output()
.expect(&format!("failed to exec `{:?}`", gdb_path));
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", gdb_path));
let cmdline = {
let mut gdb = Command::new(&format!("{}-gdb", self.config.target));
gdb.args(debugger_opts);
Expand Down Expand Up @@ -1063,11 +1063,11 @@ impl<'test> TestCx<'test> {
}

let prefixes = if self.config.lldb_native_rust {
static PREFIXES: &'static [&'static str] = &["lldb", "lldbr"];
static PREFIXES: &[&str] = &["lldb", "lldbr"];
println!("NOTE: compiletest thinks it is using LLDB with native rust support");
PREFIXES
} else {
static PREFIXES: &'static [&'static str] = &["lldb", "lldbg"];
static PREFIXES: &[&str] = &["lldb", "lldbg"];
println!("NOTE: compiletest thinks it is using LLDB without native rust support");
PREFIXES
};
Expand Down Expand Up @@ -1842,8 +1842,8 @@ impl<'test> TestCx<'test> {

// Need to be sure to put both the lib_path and the aux path in the dylib
// search path for the child.
let mut path = env::split_paths(&env::var_os(dylib_env_var()).unwrap_or(OsString::new()))
.collect::<Vec<_>>();
let mut path =
env::split_paths(&env::var_os(dylib_env_var()).unwrap_or_default()).collect::<Vec<_>>();
if let Some(p) = aux_path {
path.insert(0, PathBuf::from(p))
}
Expand All @@ -1854,7 +1854,7 @@ impl<'test> TestCx<'test> {
command.env(dylib_env_var(), newpath);

let mut child = disable_error_reporting(|| command.spawn())
.expect(&format!("failed to exec `{:?}`", &command));
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", &command));
if let Some(input) = input {
child.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
}
Expand Down Expand Up @@ -2446,8 +2446,8 @@ impl<'test> TestCx<'test> {

self.check_no_compiler_crash(&proc_res, self.props.should_ice);

const PREFIX: &'static str = "MONO_ITEM ";
const CGU_MARKER: &'static str = "@@";
const PREFIX: &str = "MONO_ITEM ";
const CGU_MARKER: &str = "@@";

let actual: Vec<MonoItem> = proc_res
.stdout
Expand Down Expand Up @@ -2976,7 +2976,7 @@ impl<'test> TestCx<'test> {
Filter::MachineApplicableOnly,
)
.unwrap_or_default();
if suggestions.len() > 0
if !suggestions.is_empty()
&& !self.props.run_rustfix
&& !self.props.rustfix_only_machine_applicable
{
Expand All @@ -2990,7 +2990,7 @@ impl<'test> TestCx<'test> {
.open(coverage_file_path.as_path())
.expect("could not create or open file");

if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
if writeln!(file, "{}", self.testpaths.file.display()).is_err() {
panic!("couldn't write to {}", coverage_file_path.display());
}
}
Expand All @@ -3007,10 +3007,9 @@ impl<'test> TestCx<'test> {
},
)
.unwrap();
let fixed_code = apply_suggestions(&unfixed_code, &suggestions).expect(&format!(
"failed to apply suggestions for {:?} with rustfix",
self.testpaths.file
));
let fixed_code = apply_suggestions(&unfixed_code, &suggestions).unwrap_or_else(|_| {
panic!("failed to apply suggestions for {:?} with rustfix", self.testpaths.file)
});

errors += self.compare_output("fixed", &fixed_code, &expected_fixed);
} else if !expected_fixed.is_empty() {
Expand Down Expand Up @@ -3519,7 +3518,7 @@ impl<'test> TestCx<'test> {
let examined_content =
self.load_expected_output_from_path(&examined_path).unwrap_or_else(|_| String::new());

if canon_content == &examined_content {
if canon_content == examined_content {
self.delete_file(&examined_path);
}
}
Expand Down
Loading