Skip to content

Commit 7579e71

Browse files
committed
clippy_dev: Inline and simplify read_src_with_module.
1 parent fcfab5f commit 7579e71

File tree

4 files changed

+35
-47
lines changed

4 files changed

+35
-47
lines changed

clippy_dev/src/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn run_rustfmt(update_mode: UpdateMode) {
268268
.expect("invalid rustfmt path");
269269
rustfmt_path.truncate(rustfmt_path.trim_end().len());
270270

271-
let args: Vec<_> = walk_dir_no_dot_or_target()
271+
let args: Vec<_> = walk_dir_no_dot_or_target(".")
272272
.filter_map(|e| {
273273
let e = expect_action(e, ErrAction::Read, ".");
274274
e.path()

clippy_dev/src/parse.rs

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
pub mod cursor;
22

33
use self::cursor::{Capture, Cursor};
4-
use crate::utils::{ErrAction, File, Scoped, expect_action};
4+
use crate::utils::{ErrAction, File, Scoped, expect_action, walk_dir_no_dot_or_target};
55
use core::range::Range;
66
use std::fs;
7-
use std::path::{Path, PathBuf};
8-
use walkdir::{DirEntry, WalkDir};
7+
use std::path::{self, Path, PathBuf};
98

109
pub struct ParseCxImpl;
1110
pub type ParseCx<'cx> = &'cx mut ParseCxImpl;
@@ -43,18 +42,38 @@ impl ParseCxImpl {
4342
let mut contents = String::new();
4443
for e in expect_action(fs::read_dir("."), ErrAction::Read, ".") {
4544
let e = expect_action(e, ErrAction::Read, ".");
46-
if !expect_action(e.file_type(), ErrAction::Read, ".").is_dir() {
47-
continue;
48-
}
49-
let Ok(mut name) = e.file_name().into_string() else {
45+
46+
// Skip if this isn't a lint crate's directory.
47+
let mut crate_path = if expect_action(e.file_type(), ErrAction::Read, ".").is_dir()
48+
&& let Ok(crate_path) = e.file_name().into_string()
49+
&& crate_path.starts_with("clippy_lints")
50+
&& crate_path != "clippy_lints_internal"
51+
{
52+
crate_path
53+
} else {
5054
continue;
5155
};
52-
if name.starts_with("clippy_lints") && name != "clippy_lints_internal" {
53-
name.push_str("/src");
54-
for (file, module) in read_src_with_module(name.as_ref()) {
56+
57+
crate_path.push(path::MAIN_SEPARATOR);
58+
crate_path.push_str("src");
59+
for e in walk_dir_no_dot_or_target(&crate_path) {
60+
let e = expect_action(e, ErrAction::Read, &crate_path);
61+
if let Some(path) = e.path().to_str()
62+
&& let Some(path) = path.strip_suffix(".rs")
63+
&& let Some(path) = path.get(crate_path.len() + 1..)
64+
{
65+
let module = if path == "lib" {
66+
String::new()
67+
} else {
68+
let path = path
69+
.strip_suffix("mod")
70+
.and_then(|x| x.strip_suffix(path::MAIN_SEPARATOR))
71+
.unwrap_or(path);
72+
path.replace(['/', '\\'], "::")
73+
};
5574
parse_clippy_lint_decls(
56-
file.path(),
57-
File::open_read_to_cleared_string(file.path(), &mut contents),
75+
e.path(),
76+
File::open_read_to_cleared_string(e.path(), &mut contents),
5877
&module,
5978
&mut lints,
6079
);
@@ -132,37 +151,6 @@ impl ParseCxImpl {
132151
}
133152
}
134153

135-
/// Reads the source files from the given root directory
136-
fn read_src_with_module(src_root: &Path) -> impl use<'_> + Iterator<Item = (DirEntry, String)> {
137-
WalkDir::new(src_root).into_iter().filter_map(move |e| {
138-
let e = expect_action(e, ErrAction::Read, src_root);
139-
let path = e.path().as_os_str().as_encoded_bytes();
140-
if let Some(path) = path.strip_suffix(b".rs")
141-
&& let Some(path) = path.get(src_root.as_os_str().len() + 1..)
142-
{
143-
if path == b"lib" {
144-
Some((e, String::new()))
145-
} else {
146-
let path = if let Some(path) = path.strip_suffix(b"mod")
147-
&& let Some(path) = path.strip_suffix(b"/").or_else(|| path.strip_suffix(b"\\"))
148-
{
149-
path
150-
} else {
151-
path
152-
};
153-
if let Ok(path) = str::from_utf8(path) {
154-
let path = path.replace(['/', '\\'], "::");
155-
Some((e, path))
156-
} else {
157-
None
158-
}
159-
}
160-
} else {
161-
None
162-
}
163-
})
164-
}
165-
166154
/// Parse a source file looking for `declare_clippy_lint` macro invocations.
167155
fn parse_clippy_lint_decls(path: &Path, contents: &str, module: &str, lints: &mut Vec<Lint>) {
168156
#[allow(clippy::enum_glob_use)]

clippy_dev/src/rename_lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn rename<'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_name: &'cx str
127127
}
128128

129129
let mut update_fn = file_update_fn(old_name, new_name, mod_edit);
130-
for e in walk_dir_no_dot_or_target() {
130+
for e in walk_dir_no_dot_or_target(".") {
131131
let e = expect_action(e, ErrAction::Read, ".");
132132
if e.path().as_os_str().as_encoded_bytes().ends_with(b".rs") {
133133
updater.update_file(e.path(), &mut update_fn);

clippy_dev/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ pub fn delete_dir_if_exists(path: &Path) {
593593
}
594594

595595
/// Walks all items excluding top-level dot files/directories and any target directories.
596-
pub fn walk_dir_no_dot_or_target() -> impl Iterator<Item = ::walkdir::Result<::walkdir::DirEntry>> {
597-
WalkDir::new(".").into_iter().filter_entry(|e| {
596+
pub fn walk_dir_no_dot_or_target(p: impl AsRef<Path>) -> impl Iterator<Item = ::walkdir::Result<::walkdir::DirEntry>> {
597+
WalkDir::new(p).into_iter().filter_entry(|e| {
598598
e.path()
599599
.file_name()
600600
.is_none_or(|x| x != "target" && x.as_encoded_bytes().first().copied() != Some(b'.'))

0 commit comments

Comments
 (0)