Skip to content

Commit 2342cc3

Browse files
committed
Auto merge of #75789 - matthiaskrgr:clippy_compiletest, r=Dylan-DPC
compiletest: fix a couple clippy lint findings
2 parents d5ba3ef + a2a387c commit 2342cc3

File tree

6 files changed

+59
-72
lines changed

6 files changed

+59
-72
lines changed

src/tools/compiletest/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn parse_expected(
148148

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

src/tools/compiletest/src/header.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,8 @@ impl EarlyProps {
173173
// Ignore if actual version is smaller the minimum required
174174
// version
175175
actual_version < min_version
176-
} else if line.starts_with("rust-lldb") && !config.lldb_native_rust {
177-
true
178176
} else {
179-
false
177+
line.starts_with("rust-lldb") && !config.lldb_native_rust
180178
}
181179
} else {
182180
false
@@ -657,7 +655,6 @@ fn iter_header<R: Read>(testfile: &Path, cfg: Option<&str>, rdr: R, it: &mut dyn
657655
it(ln[comment.len()..].trim_start());
658656
}
659657
}
660-
return;
661658
}
662659

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

821818
let is_match = name == "test" ||
822-
&self.target == name || // triple
819+
self.target == name || // triple
823820
util::matches_os(&self.target, name) || // target
824821
util::matches_env(&self.target, name) || // env
825822
self.target.ends_with(name) || // target and env
@@ -857,10 +854,7 @@ impl Config {
857854
// Ensure the directive is a whole word. Do not match "ignore-x86" when
858855
// the line says "ignore-x86_64".
859856
line.starts_with(directive)
860-
&& match line.as_bytes().get(directive.len()) {
861-
None | Some(&b' ') | Some(&b':') => true,
862-
_ => false,
863-
}
857+
&& matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
864858
}
865859

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

903897
fn expand_variables(mut value: String, config: &Config) -> String {
904-
const CWD: &'static str = "{{cwd}}";
905-
const SRC_BASE: &'static str = "{{src-base}}";
906-
const BUILD_BASE: &'static str = "{{build-base}}";
898+
const CWD: &str = "{{cwd}}";
899+
const SRC_BASE: &str = "{{src-base}}";
900+
const BUILD_BASE: &str = "{{build-base}}";
907901

908902
if value.contains(CWD) {
909903
let cwd = env::current_dir().unwrap();

src/tools/compiletest/src/json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn extract_rendered(output: &str) -> String {
7575
if line.starts_with('{') {
7676
if let Ok(diagnostic) = serde_json::from_str::<Diagnostic>(line) {
7777
diagnostic.rendered
78-
} else if let Ok(_) = serde_json::from_str::<ArtifactNotification>(line) {
78+
} else if serde_json::from_str::<ArtifactNotification>(line).is_ok() {
7979
// Ignore the notification.
8080
None
8181
} else {

src/tools/compiletest/src/main.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
240240
cc: matches.opt_str("cc").unwrap(),
241241
cxx: matches.opt_str("cxx").unwrap(),
242242
cflags: matches.opt_str("cflags").unwrap(),
243-
ar: matches.opt_str("ar").unwrap_or("ar".into()),
243+
ar: matches.opt_str("ar").unwrap_or_else(|| String::from("ar")),
244244
linker: matches.opt_str("linker"),
245245
llvm_components: matches.opt_str("llvm-components").unwrap(),
246246
nodejs: matches.opt_str("nodejs"),
@@ -361,17 +361,13 @@ pub fn run_tests(config: Config) {
361361
}
362362

363363
fn configure_cdb(config: &Config) -> Option<Config> {
364-
if config.cdb.is_none() {
365-
return None;
366-
}
364+
config.cdb.as_ref()?;
367365

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

371369
fn configure_gdb(config: &Config) -> Option<Config> {
372-
if config.gdb_version.is_none() {
373-
return None;
374-
}
370+
config.gdb_version?;
375371

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

407403
fn configure_lldb(config: &Config) -> Option<Config> {
408-
if config.lldb_python_dir.is_none() {
409-
return None;
410-
}
404+
config.lldb_python_dir.as_ref()?;
411405

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

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

736730
/// Returns `true` if the given target is an Android target for the
737731
/// purposes of GDB testing.
738-
fn is_android_gdb_target(target: &String) -> bool {
739-
match &target[..] {
740-
"arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => true,
741-
_ => false,
742-
}
732+
fn is_android_gdb_target(target: &str) -> bool {
733+
matches!(
734+
&target[..],
735+
"arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android"
736+
)
743737
}
744738

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

750-
fn find_cdb(target: &String) -> Option<OsString> {
744+
fn find_cdb(target: &str) -> Option<OsString> {
751745
if !(cfg!(windows) && is_pc_windows_msvc_target(target)) {
752746
return None;
753747
}
754748

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

781775
/// Returns Path to CDB
782-
fn analyze_cdb(cdb: Option<String>, target: &String) -> Option<OsString> {
783-
cdb.map(|s| OsString::from(s)).or(find_cdb(target))
776+
fn analyze_cdb(cdb: Option<String>, target: &str) -> Option<OsString> {
777+
cdb.map(OsString::from).or_else(|| find_cdb(target))
784778
}
785779

786780
/// Returns (Path to GDB, GDB Version, GDB has Rust Support)
787781
fn analyze_gdb(
788782
gdb: Option<String>,
789-
target: &String,
783+
target: &str,
790784
android_cross_path: &PathBuf,
791785
) -> (Option<String>, Option<u32>, bool) {
792786
#[cfg(not(windows))]

src/tools/compiletest/src/runtest.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub struct Mismatch {
114114

115115
impl Mismatch {
116116
fn new(line_number: u32) -> Mismatch {
117-
Mismatch { line_number: line_number, lines: Vec::new() }
117+
Mismatch { line_number, lines: Vec::new() }
118118
}
119119
}
120120

@@ -199,7 +199,7 @@ fn write_diff(expected: &str, actual: &str, context_size: usize) -> String {
199199
}
200200
}
201201
}
202-
writeln!(output, "").unwrap();
202+
writeln!(output).unwrap();
203203
}
204204
output
205205
}
@@ -230,7 +230,7 @@ pub fn run(config: Config, testpaths: &TestPaths, revision: Option<&str>) {
230230
debug!("running {:?}", testpaths.file.display());
231231
let props = TestProps::from_file(&testpaths.file, revision, &config);
232232

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

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

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

742742
let prefixes = {
743-
static PREFIXES: &'static [&'static str] = &["cdb", "cdbg"];
743+
static PREFIXES: &[&str] = &["cdb", "cdbg"];
744744
// No "native rust support" variation for CDB yet.
745745
PREFIXES
746746
};
@@ -811,12 +811,12 @@ impl<'test> TestCx<'test> {
811811
fn run_debuginfo_gdb_test_no_opt(&self) {
812812
let prefixes = if self.config.gdb_native_rust {
813813
// GDB with Rust
814-
static PREFIXES: &'static [&'static str] = &["gdb", "gdbr"];
814+
static PREFIXES: &[&str] = &["gdb", "gdbr"];
815815
println!("NOTE: compiletest thinks it is using GDB with native rust support");
816816
PREFIXES
817817
} else {
818818
// Generic GDB
819-
static PREFIXES: &'static [&'static str] = &["gdb", "gdbg"];
819+
static PREFIXES: &[&str] = &["gdb", "gdbg"];
820820
println!("NOTE: compiletest thinks it is using GDB without native rust support");
821821
PREFIXES
822822
};
@@ -875,12 +875,12 @@ impl<'test> TestCx<'test> {
875875
.arg(&exe_file)
876876
.arg(&self.config.adb_test_dir)
877877
.status()
878-
.expect(&format!("failed to exec `{:?}`", adb_path));
878+
.unwrap_or_else(|_| panic!("failed to exec `{:?}`", adb_path));
879879

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

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

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

10651065
let prefixes = if self.config.lldb_native_rust {
1066-
static PREFIXES: &'static [&'static str] = &["lldb", "lldbr"];
1066+
static PREFIXES: &[&str] = &["lldb", "lldbr"];
10671067
println!("NOTE: compiletest thinks it is using LLDB with native rust support");
10681068
PREFIXES
10691069
} else {
1070-
static PREFIXES: &'static [&'static str] = &["lldb", "lldbg"];
1070+
static PREFIXES: &[&str] = &["lldb", "lldbg"];
10711071
println!("NOTE: compiletest thinks it is using LLDB without native rust support");
10721072
PREFIXES
10731073
};
@@ -1842,8 +1842,8 @@ impl<'test> TestCx<'test> {
18421842

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

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

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

2449-
const PREFIX: &'static str = "MONO_ITEM ";
2450-
const CGU_MARKER: &'static str = "@@";
2449+
const PREFIX: &str = "MONO_ITEM ";
2450+
const CGU_MARKER: &str = "@@";
24512451

24522452
let actual: Vec<MonoItem> = proc_res
24532453
.stdout
@@ -2976,7 +2976,7 @@ impl<'test> TestCx<'test> {
29762976
Filter::MachineApplicableOnly,
29772977
)
29782978
.unwrap_or_default();
2979-
if suggestions.len() > 0
2979+
if !suggestions.is_empty()
29802980
&& !self.props.run_rustfix
29812981
&& !self.props.rustfix_only_machine_applicable
29822982
{
@@ -2990,7 +2990,7 @@ impl<'test> TestCx<'test> {
29902990
.open(coverage_file_path.as_path())
29912991
.expect("could not create or open file");
29922992

2993-
if let Err(_) = writeln!(file, "{}", self.testpaths.file.display()) {
2993+
if writeln!(file, "{}", self.testpaths.file.display()).is_err() {
29942994
panic!("couldn't write to {}", coverage_file_path.display());
29952995
}
29962996
}
@@ -3007,10 +3007,9 @@ impl<'test> TestCx<'test> {
30073007
},
30083008
)
30093009
.unwrap();
3010-
let fixed_code = apply_suggestions(&unfixed_code, &suggestions).expect(&format!(
3011-
"failed to apply suggestions for {:?} with rustfix",
3012-
self.testpaths.file
3013-
));
3010+
let fixed_code = apply_suggestions(&unfixed_code, &suggestions).unwrap_or_else(|_| {
3011+
panic!("failed to apply suggestions for {:?} with rustfix", self.testpaths.file)
3012+
});
30143013

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

3522-
if canon_content == &examined_content {
3521+
if canon_content == examined_content {
35233522
self.delete_file(&examined_path);
35243523
}
35253524
}

0 commit comments

Comments
 (0)