Skip to content

Commit 7e3eabc

Browse files
committed
Auto merge of #28152 - llogiq:master, r=Manishearth
This changes a lot of `.to_string()` to `.to_owned()`, removes a few redundant closures, and changes some `match`es to `if let`s. I'm currently in the process of trying out clippy and acting on its suggestions. I started with compiletest, because we use it to test clippy, too. If this finds positive reception, I may continue refactoring other parts of the rust codebase.
2 parents 2e9a74e + 99383f8 commit 7e3eabc

File tree

6 files changed

+176
-191
lines changed

6 files changed

+176
-191
lines changed

src/compiletest/compiletest.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn log_config(config: &Config) {
178178
logv(c, format!("filter: {}",
179179
opt_str(&config.filter
180180
.as_ref()
181-
.map(|re| re.to_string()))));
181+
.map(|re| re.to_owned()))));
182182
logv(c, format!("runtool: {}", opt_str(&config.runtool)));
183183
logv(c, format!("host-rustcflags: {}",
184184
opt_str(&config.host_rustcflags)));
@@ -205,19 +205,16 @@ pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'a str {
205205

206206
pub fn opt_str2(maybestr: Option<String>) -> String {
207207
match maybestr {
208-
None => "(none)".to_string(),
208+
None => "(none)".to_owned(),
209209
Some(s) => s,
210210
}
211211
}
212212

213213
pub fn run_tests(config: &Config) {
214214
if config.target.contains("android") {
215-
match config.mode {
216-
DebugInfoGdb => {
217-
println!("{} debug-info test uses tcp 5039 port.\
218-
please reserve it", config.target);
219-
}
220-
_ =>{}
215+
if let DebugInfoGdb = config.mode {
216+
println!("{} debug-info test uses tcp 5039 port.\
217+
please reserve it", config.target);
221218
}
222219

223220
// android debug-info test uses remote debugger
@@ -289,10 +286,10 @@ pub fn is_test(config: &Config, testfile: &Path) -> bool {
289286
// Pretty-printer does not work with .rc files yet
290287
let valid_extensions =
291288
match config.mode {
292-
Pretty => vec!(".rs".to_string()),
293-
_ => vec!(".rc".to_string(), ".rs".to_string())
289+
Pretty => vec!(".rs".to_owned()),
290+
_ => vec!(".rc".to_owned(), ".rs".to_owned())
294291
};
295-
let invalid_prefixes = vec!(".".to_string(), "#".to_string(), "~".to_string());
292+
let invalid_prefixes = vec!(".".to_owned(), "#".to_owned(), "~".to_owned());
296293
let name = testfile.file_name().unwrap().to_str().unwrap();
297294

298295
let mut valid = false;
@@ -364,7 +361,7 @@ fn extract_gdb_version(full_version_line: Option<String>) -> Option<String> {
364361
full_version_line.char_at(pos + 3).is_digit(10) {
365362
continue
366363
}
367-
return Some(full_version_line[pos..pos+3].to_string());
364+
return Some(full_version_line[pos..pos+3].to_owned());
368365
}
369366
println!("Could not extract GDB version from line '{}'",
370367
full_version_line);
@@ -386,9 +383,8 @@ fn extract_lldb_version(full_version_line: Option<String>) -> Option<String> {
386383
// We are only interested in the major version number, so this function
387384
// will return `Some("179")` and `Some("300")` respectively.
388385

389-
match full_version_line {
390-
Some(ref full_version_line)
391-
if !full_version_line.trim().is_empty() => {
386+
if let Some(ref full_version_line) = full_version_line {
387+
if !full_version_line.trim().is_empty() {
392388
let full_version_line = full_version_line.trim();
393389

394390
for (pos, l) in full_version_line.char_indices() {
@@ -410,8 +406,7 @@ fn extract_lldb_version(full_version_line: Option<String>) -> Option<String> {
410406
}
411407
println!("Could not extract LLDB version from line '{}'",
412408
full_version_line);
413-
None
414-
},
415-
_ => None
409+
}
416410
}
411+
None
417412
}

src/compiletest/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn parse_expected(last_nonfollow_error: Option<usize>,
7676
let letters = line[kind_start..].chars();
7777
let msg = letters.skip_while(|c| c.is_whitespace())
7878
.skip_while(|c| !c.is_whitespace())
79-
.collect::<String>().trim().to_string();
79+
.collect::<String>().trim().to_owned();
8080

8181
let (which, line) = if follow {
8282
assert!(adjusts == 0, "use either //~| or //~^, not both.");

src/compiletest/header.rs

+56-65
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ pub fn load_props(testfile: &Path) -> TestProps {
6767
let mut pretty_compare_only = false;
6868
let mut forbid_output = Vec::new();
6969
iter_header(testfile, &mut |ln| {
70-
match parse_error_pattern(ln) {
71-
Some(ep) => error_patterns.push(ep),
72-
None => ()
73-
};
70+
if let Some(ep) = parse_error_pattern(ln) {
71+
error_patterns.push(ep);
72+
}
7473

7574
if compile_flags.is_none() {
7675
compile_flags = parse_compile_flags(ln);
@@ -108,24 +107,20 @@ pub fn load_props(testfile: &Path) -> TestProps {
108107
pretty_compare_only = parse_pretty_compare_only(ln);
109108
}
110109

111-
match parse_aux_build(ln) {
112-
Some(ab) => { aux_builds.push(ab); }
113-
None => {}
110+
if let Some(ab) = parse_aux_build(ln) {
111+
aux_builds.push(ab);
114112
}
115113

116-
match parse_exec_env(ln) {
117-
Some(ee) => { exec_env.push(ee); }
118-
None => {}
114+
if let Some(ee) = parse_exec_env(ln) {
115+
exec_env.push(ee);
119116
}
120117

121-
match parse_check_line(ln) {
122-
Some(cl) => check_lines.push(cl),
123-
None => ()
124-
};
118+
if let Some(cl) = parse_check_line(ln) {
119+
check_lines.push(cl);
120+
}
125121

126-
match parse_forbid_output(ln) {
127-
Some(of) => forbid_output.push(of),
128-
None => (),
122+
if let Some(of) = parse_forbid_output(ln) {
123+
forbid_output.push(of);
129124
}
130125

131126
true
@@ -134,8 +129,8 @@ pub fn load_props(testfile: &Path) -> TestProps {
134129
for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
135130
match env::var(key) {
136131
Ok(val) =>
137-
if exec_env.iter().find(|&&(ref x, _)| *x == key.to_string()).is_none() {
138-
exec_env.push((key.to_string(), val))
132+
if exec_env.iter().find(|&&(ref x, _)| *x == key).is_none() {
133+
exec_env.push((key.to_owned(), val))
139134
},
140135
Err(..) => {}
141136
}
@@ -153,7 +148,7 @@ pub fn load_props(testfile: &Path) -> TestProps {
153148
check_stdout: check_stdout,
154149
no_prefer_dynamic: no_prefer_dynamic,
155150
pretty_expanded: pretty_expanded,
156-
pretty_mode: pretty_mode.unwrap_or("normal".to_string()),
151+
pretty_mode: pretty_mode.unwrap_or("normal".to_owned()),
157152
pretty_compare_only: pretty_compare_only,
158153
forbid_output: forbid_output,
159154
}
@@ -182,22 +177,21 @@ pub fn is_test_ignored(config: &Config, testfile: &Path) -> bool {
182177
return true;
183178
}
184179

185-
match config.gdb_version {
186-
Some(ref actual_version) => {
187-
if line.contains("min-gdb-version") {
188-
let min_version = line.trim()
189-
.split(' ')
190-
.last()
191-
.expect("Malformed GDB version directive");
192-
// Ignore if actual version is smaller the minimum required
193-
// version
194-
gdb_version_to_int(actual_version) <
195-
gdb_version_to_int(min_version)
196-
} else {
197-
false
198-
}
180+
if let Some(ref actual_version) = config.gdb_version {
181+
if line.contains("min-gdb-version") {
182+
let min_version = line.trim()
183+
.split(' ')
184+
.last()
185+
.expect("Malformed GDB version directive");
186+
// Ignore if actual version is smaller the minimum required
187+
// version
188+
gdb_version_to_int(actual_version) <
189+
gdb_version_to_int(min_version)
190+
} else {
191+
false
199192
}
200-
None => false
193+
} else {
194+
false
201195
}
202196
}
203197

@@ -210,22 +204,21 @@ pub fn is_test_ignored(config: &Config, testfile: &Path) -> bool {
210204
return true;
211205
}
212206

213-
match config.lldb_version {
214-
Some(ref actual_version) => {
215-
if line.contains("min-lldb-version") {
216-
let min_version = line.trim()
217-
.split(' ')
218-
.last()
219-
.expect("Malformed lldb version directive");
220-
// Ignore if actual version is smaller the minimum required
221-
// version
222-
lldb_version_to_int(actual_version) <
223-
lldb_version_to_int(min_version)
224-
} else {
225-
false
226-
}
207+
if let Some(ref actual_version) = config.lldb_version {
208+
if line.contains("min-lldb-version") {
209+
let min_version = line.trim()
210+
.split(' ')
211+
.last()
212+
.expect("Malformed lldb version directive");
213+
// Ignore if actual version is smaller the minimum required
214+
// version
215+
lldb_version_to_int(actual_version) <
216+
lldb_version_to_int(min_version)
217+
} else {
218+
false
227219
}
228-
None => false
220+
} else {
221+
false
229222
}
230223
}
231224

@@ -316,11 +309,11 @@ fn parse_exec_env(line: &str) -> Option<(String, String)> {
316309
// nv is either FOO or FOO=BAR
317310
let mut strs: Vec<String> = nv
318311
.splitn(2, '=')
319-
.map(|s| s.to_string())
312+
.map(str::to_owned)
320313
.collect();
321314

322315
match strs.len() {
323-
1 => (strs.pop().unwrap(), "".to_string()),
316+
1 => (strs.pop().unwrap(), "".to_owned()),
324317
2 => {
325318
let end = strs.pop().unwrap();
326319
(strs.pop().unwrap(), end)
@@ -331,33 +324,31 @@ fn parse_exec_env(line: &str) -> Option<(String, String)> {
331324
}
332325

333326
fn parse_pp_exact(line: &str, testfile: &Path) -> Option<PathBuf> {
334-
match parse_name_value_directive(line, "pp-exact") {
335-
Some(s) => Some(PathBuf::from(&s)),
336-
None => {
327+
if let Some(s) = parse_name_value_directive(line, "pp-exact") {
328+
Some(PathBuf::from(&s))
329+
} else {
337330
if parse_name_directive(line, "pp-exact") {
338-
testfile.file_name().map(|s| PathBuf::from(s))
331+
testfile.file_name().map(PathBuf::from)
339332
} else {
340333
None
341334
}
342-
}
343335
}
344336
}
345337

346338
fn parse_name_directive(line: &str, directive: &str) -> bool {
347339
// This 'no-' rule is a quick hack to allow pretty-expanded and no-pretty-expanded to coexist
348-
line.contains(directive) && !line.contains(&("no-".to_string() + directive))
340+
line.contains(directive) && !line.contains(&("no-".to_owned() + directive))
349341
}
350342

351343
pub fn parse_name_value_directive(line: &str, directive: &str)
352344
-> Option<String> {
353345
let keycolon = format!("{}:", directive);
354-
match line.find(&keycolon) {
355-
Some(colon) => {
356-
let value = line[(colon + keycolon.len()) .. line.len()].to_string();
357-
debug!("{}: {}", directive, value);
358-
Some(value)
359-
}
360-
None => None
346+
if let Some(colon) = line.find(&keycolon) {
347+
let value = line[(colon + keycolon.len()) .. line.len()].to_owned();
348+
debug!("{}: {}", directive, value);
349+
Some(value)
350+
} else {
351+
None
361352
}
362353
}
363354

src/compiletest/procsrv.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) {
1717
// Need to be sure to put both the lib_path and the aux path in the dylib
1818
// search path for the child.
1919
let mut path = DynamicLibrary::search_path();
20-
match aux_path {
21-
Some(p) => path.insert(0, PathBuf::from(p)),
22-
None => {}
20+
if let Some(p) = aux_path {
21+
path.insert(0, PathBuf::from(p))
2322
}
2423
path.insert(0, PathBuf::from(lib_path));
2524

0 commit comments

Comments
 (0)