Skip to content

Commit d2a0f98

Browse files
committed
Rollup merge of rust-lang#53895 - joshtriplett:tidy-tidy, r=nikomatsakis
tidy: Cleanups and clippy warning fixes This eliminates various clippy warnings in the tidy tool, as well as making some related cleanups. These changes should not introduce any functional differences.
2 parents 3c77043 + a5c86fe commit d2a0f98

File tree

9 files changed

+44
-57
lines changed

9 files changed

+44
-57
lines changed

src/tools/tidy/src/cargo.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ fn verify(tomlfile: &Path, libfile: &Path, bad: &mut bool) {
6565
Some(i) => &toml[i+1..],
6666
None => return,
6767
};
68-
let mut lines = deps.lines().peekable();
69-
while let Some(line) = lines.next() {
70-
if line.starts_with("[") {
68+
for line in deps.lines() {
69+
if line.starts_with('[') {
7170
break
7271
}
7372

src/tools/tidy/src/deps.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::process::Command;
1818

1919
use serde_json;
2020

21-
static LICENSES: &'static [&'static str] = &[
21+
const LICENSES: &[&str] = &[
2222
"MIT/Apache-2.0",
2323
"MIT / Apache-2.0",
2424
"Apache-2.0/MIT",
@@ -33,7 +33,7 @@ static LICENSES: &'static [&'static str] = &[
3333
/// should be considered bugs. Exceptions are only allowed in Rust
3434
/// tooling. It is _crucial_ that no exception crates be dependencies
3535
/// of the Rust runtime (std / test).
36-
static EXCEPTIONS: &'static [&'static str] = &[
36+
const EXCEPTIONS: &[&str] = &[
3737
"mdbook", // MPL2, mdbook
3838
"openssl", // BSD+advertising clause, cargo, mdbook
3939
"pest", // MPL2, mdbook via handlebars
@@ -54,13 +54,13 @@ static EXCEPTIONS: &'static [&'static str] = &[
5454
];
5555

5656
/// Which crates to check against the whitelist?
57-
static WHITELIST_CRATES: &'static [CrateVersion] = &[
57+
const WHITELIST_CRATES: &[CrateVersion] = &[
5858
CrateVersion("rustc", "0.0.0"),
5959
CrateVersion("rustc_codegen_llvm", "0.0.0"),
6060
];
6161

6262
/// Whitelist of crates rustc is allowed to depend on. Avoid adding to the list if possible.
63-
static WHITELIST: &'static [Crate] = &[
63+
const WHITELIST: &[Crate] = &[
6464
Crate("aho-corasick"),
6565
Crate("arrayvec"),
6666
Crate("atty"),
@@ -208,12 +208,13 @@ pub fn check(path: &Path, bad: &mut bool) {
208208
let dir = t!(dir);
209209

210210
// skip our exceptions
211-
if EXCEPTIONS.iter().any(|exception| {
211+
let is_exception = EXCEPTIONS.iter().any(|exception| {
212212
dir.path()
213213
.to_str()
214214
.unwrap()
215215
.contains(&format!("src/vendor/{}", exception))
216-
}) {
216+
});
217+
if is_exception {
217218
continue;
218219
}
219220

@@ -242,7 +243,7 @@ pub fn check_whitelist(path: &Path, cargo: &Path, bad: &mut bool) {
242243
unapproved.append(&mut bad);
243244
}
244245

245-
if unapproved.len() > 0 {
246+
if !unapproved.is_empty() {
246247
println!("Dependencies not on the whitelist:");
247248
for dep in unapproved {
248249
println!("* {}", dep.id_str());

src/tools/tidy/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn check(path: &Path, bad: &mut bool) {
5050
}
5151

5252
let mut search = line;
53-
while let Some(i) = search.find("E") {
53+
while let Some(i) = search.find('E') {
5454
search = &search[i + 1..];
5555
let code = if search.len() > 4 {
5656
search[..4].parse::<u32>()

src/tools/tidy/src/extdeps.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::io::Read;
1515
use std::path::Path;
1616

1717
/// List of whitelisted sources for packages
18-
static WHITELISTED_SOURCES: &'static [&'static str] = &[
18+
const WHITELISTED_SOURCES: &[&str] = &[
1919
"\"registry+https://github.com/rust-lang/crates.io-index\"",
2020
];
2121

@@ -29,17 +29,15 @@ pub fn check(path: &Path, bad: &mut bool) {
2929
t!(t!(File::open(path)).read_to_string(&mut cargo_lock));
3030

3131
// process each line
32-
let mut lines = cargo_lock.lines();
33-
while let Some(line) = lines.next() {
32+
for line in cargo_lock.lines() {
3433

3534
// consider only source entries
3635
if ! line.starts_with("source = ") {
3736
continue;
3837
}
3938

4039
// extract source value
41-
let parts: Vec<&str> = line.splitn(2, "=").collect();
42-
let source = parts[1].trim();
40+
let source = line.splitn(2, '=').nth(1).unwrap().trim();
4341

4442
// ensure source is whitelisted
4543
if !WHITELISTED_SOURCES.contains(&&*source) {

src/tools/tidy/src/features.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn check(path: &Path, bad: &mut bool, quiet: bool) {
7575
return;
7676
}
7777

78-
let filen_underscore = filename.replace("-","_").replace(".rs","");
78+
let filen_underscore = filename.replace('-',"_").replace(".rs","");
7979
let filename_is_gate_test = test_filen_gate(&filen_underscore, &mut features);
8080

8181
contents.truncate(0);
@@ -88,13 +88,9 @@ pub fn check(path: &Path, bad: &mut bool, quiet: bool) {
8888

8989
let gate_test_str = "gate-test-";
9090

91-
if !line.contains(gate_test_str) {
92-
continue;
93-
}
94-
9591
let feature_name = match line.find(gate_test_str) {
9692
Some(i) => {
97-
&line[i+gate_test_str.len()..line[i+1..].find(' ').unwrap_or(line.len())]
93+
line[i+gate_test_str.len()..].splitn(2, ' ').next().unwrap()
9894
},
9995
None => continue,
10096
};
@@ -133,7 +129,7 @@ pub fn check(path: &Path, bad: &mut bool, quiet: bool) {
133129
name);
134130
}
135131

136-
if gate_untested.len() > 0 {
132+
if !gate_untested.is_empty() {
137133
tidy_error!(bad, "Found {} features without a gate test.", gate_untested.len());
138134
}
139135

@@ -259,14 +255,11 @@ pub fn collect_lib_features(base_src_path: &Path) -> Features {
259255

260256
map_lib_features(base_src_path,
261257
&mut |res, _, _| {
262-
match res {
263-
Ok((name, feature)) => {
264-
if lib_features.get(name).is_some() {
265-
return;
266-
}
267-
lib_features.insert(name.to_owned(), feature);
268-
},
269-
Err(_) => (),
258+
if let Ok((name, feature)) = res {
259+
if lib_features.contains_key(name) {
260+
return;
261+
}
262+
lib_features.insert(name.to_owned(), feature);
270263
}
271264
});
272265
lib_features
@@ -332,11 +325,11 @@ fn map_lib_features(base_src_path: &Path,
332325
f.tracking_issue = find_attr_val(line, "issue")
333326
.map(|s| s.parse().unwrap());
334327
}
335-
if line.ends_with("]") {
328+
if line.ends_with(']') {
336329
mf(Ok((name, f.clone())), file, i + 1);
337-
} else if !line.ends_with(",") && !line.ends_with("\\") {
330+
} else if !line.ends_with(',') && !line.ends_with('\\') {
338331
// We need to bail here because we might have missed the
339-
// end of a stability attribute above because the "]"
332+
// end of a stability attribute above because the ']'
340333
// might not have been at the end of the line.
341334
// We could then get into the very unfortunate situation that
342335
// we continue parsing the file assuming the current stability
@@ -394,7 +387,7 @@ fn map_lib_features(base_src_path: &Path,
394387
has_gate_test: false,
395388
tracking_issue,
396389
};
397-
if line.contains("]") {
390+
if line.contains(']') {
398391
mf(Ok((feature_name, feature)), file, i + 1);
399392
} else {
400393
becoming_feature = Some((feature_name.to_owned(), feature));

src/tools/tidy/src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ use std::path::PathBuf;
2424
use std::env;
2525

2626
fn main() {
27-
let path = env::args_os().skip(1).next().expect("need path to src");
28-
let path = PathBuf::from(path);
29-
30-
let cargo = env::args_os().skip(2).next().expect("need path to cargo");
31-
let cargo = PathBuf::from(cargo);
27+
let path: PathBuf = env::args_os().nth(1).expect("need path to src").into();
28+
let cargo: PathBuf = env::args_os().nth(2).expect("need path to cargo").into();
3229

3330
let args: Vec<String> = env::args().skip(1).collect();
3431

src/tools/tidy/src/pal.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use std::path::Path;
5050
use std::iter::Iterator;
5151

5252
// Paths that may contain platform-specific code
53-
const EXCEPTION_PATHS: &'static [&'static str] = &[
53+
const EXCEPTION_PATHS: &[&str] = &[
5454
// std crates
5555
"src/liballoc_jemalloc",
5656
"src/liballoc_system",
@@ -88,22 +88,22 @@ const EXCEPTION_PATHS: &'static [&'static str] = &[
8888
];
8989

9090
pub fn check(path: &Path, bad: &mut bool) {
91-
let ref mut contents = String::new();
91+
let mut contents = String::new();
9292
// Sanity check that the complex parsing here works
93-
let ref mut saw_target_arch = false;
94-
let ref mut saw_cfg_bang = false;
93+
let mut saw_target_arch = false;
94+
let mut saw_cfg_bang = false;
9595
super::walk(path, &mut super::filter_dirs, &mut |file| {
9696
let filestr = file.to_string_lossy().replace("\\", "/");
9797
if !filestr.ends_with(".rs") { return }
9898

9999
let is_exception_path = EXCEPTION_PATHS.iter().any(|s| filestr.contains(&**s));
100100
if is_exception_path { return }
101101

102-
check_cfgs(contents, &file, bad, saw_target_arch, saw_cfg_bang);
102+
check_cfgs(&mut contents, &file, bad, &mut saw_target_arch, &mut saw_cfg_bang);
103103
});
104104

105-
assert!(*saw_target_arch);
106-
assert!(*saw_cfg_bang);
105+
assert!(saw_target_arch);
106+
assert!(saw_cfg_bang);
107107
}
108108

109109
fn check_cfgs(contents: &mut String, file: &Path,
@@ -130,7 +130,7 @@ fn check_cfgs(contents: &mut String, file: &Path,
130130
tidy_error!(bad, "{}:{}: platform-specific cfg: {}", file.display(), line, cfg);
131131
};
132132

133-
for (idx, cfg) in cfgs.into_iter() {
133+
for (idx, cfg) in cfgs {
134134
// Sanity check that the parsing here works
135135
if !*saw_target_arch && cfg.contains("target_arch") { *saw_target_arch = true }
136136
if !*saw_cfg_bang && cfg.contains("cfg!") { *saw_cfg_bang = true }
@@ -216,7 +216,7 @@ fn parse_cfgs<'a>(contents: &'a str) -> Vec<(usize, &'a str)> {
216216
b')' => {
217217
depth -= 1;
218218
if depth == 0 {
219-
return (i, &contents_from[.. j + 1]);
219+
return (i, &contents_from[..=j]);
220220
}
221221
}
222222
_ => { }

src/tools/tidy/src/style.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn line_is_url(line: &str) -> bool {
6969
(EXP_COMMENT_START, "//!") => state = EXP_LINK_LABEL_OR_URL,
7070

7171
(EXP_LINK_LABEL_OR_URL, w)
72-
if w.len() >= 4 && w.starts_with("[") && w.ends_with("]:")
72+
if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:")
7373
=> state = EXP_URL,
7474

7575
(EXP_LINK_LABEL_OR_URL, w)
@@ -128,13 +128,13 @@ pub fn check(path: &Path, bad: &mut bool) {
128128
&& !long_line_is_ok(line) {
129129
err(&format!("line longer than {} chars", COLS));
130130
}
131-
if line.contains("\t") && !skip_tab {
131+
if line.contains('\t') && !skip_tab {
132132
err("tab character");
133133
}
134-
if !skip_end_whitespace && (line.ends_with(" ") || line.ends_with("\t")) {
134+
if !skip_end_whitespace && (line.ends_with(' ') || line.ends_with('\t')) {
135135
err("trailing whitespace");
136136
}
137-
if line.contains("\r") && !skip_cr {
137+
if line.contains('\r') && !skip_cr {
138138
err("CR character");
139139
}
140140
if filename != "style.rs" {

src/tools/tidy/src/unstable_book.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ pub fn collect_unstable_feature_names(features: &Features) -> BTreeSet<String> {
5656
pub fn collect_unstable_book_section_file_names(dir: &path::Path) -> BTreeSet<String> {
5757
fs::read_dir(dir)
5858
.expect("could not read directory")
59-
.into_iter()
6059
.map(|entry| entry.expect("could not read directory entry"))
6160
.filter(dir_entry_is_file)
62-
.map(|entry| entry.file_name().into_string().unwrap())
63-
.filter(|n| n.ends_with(".md"))
64-
.map(|n| n.trim_right_matches(".md").to_owned())
61+
.map(|entry| entry.path())
62+
.filter(|path| path.extension().map(|e| e.to_str().unwrap()) == Some("md"))
63+
.map(|path| path.file_stem().unwrap().to_str().unwrap().into())
6564
.collect()
6665
}
6766

0 commit comments

Comments
 (0)