mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 17:18:32 +03:00
clippy_dev: Inline and simplify read_src_with_module.
This commit is contained in:
@@ -268,7 +268,7 @@ fn run_rustfmt(update_mode: UpdateMode) {
|
||||
.expect("invalid rustfmt path");
|
||||
rustfmt_path.truncate(rustfmt_path.trim_end().len());
|
||||
|
||||
let args: Vec<_> = walk_dir_no_dot_or_target()
|
||||
let args: Vec<_> = walk_dir_no_dot_or_target(".")
|
||||
.filter_map(|e| {
|
||||
let e = expect_action(e, ErrAction::Read, ".");
|
||||
e.path()
|
||||
|
||||
+31
-43
@@ -1,11 +1,10 @@
|
||||
pub mod cursor;
|
||||
|
||||
use self::cursor::{Capture, Cursor};
|
||||
use crate::utils::{ErrAction, File, Scoped, expect_action};
|
||||
use crate::utils::{ErrAction, File, Scoped, expect_action, walk_dir_no_dot_or_target};
|
||||
use core::range::Range;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
use std::path::{self, Path, PathBuf};
|
||||
|
||||
pub struct ParseCxImpl;
|
||||
pub type ParseCx<'cx> = &'cx mut ParseCxImpl;
|
||||
@@ -43,18 +42,38 @@ pub fn find_lint_decls(&mut self) -> Vec<Lint> {
|
||||
let mut contents = String::new();
|
||||
for e in expect_action(fs::read_dir("."), ErrAction::Read, ".") {
|
||||
let e = expect_action(e, ErrAction::Read, ".");
|
||||
if !expect_action(e.file_type(), ErrAction::Read, ".").is_dir() {
|
||||
continue;
|
||||
}
|
||||
let Ok(mut name) = e.file_name().into_string() else {
|
||||
|
||||
// Skip if this isn't a lint crate's directory.
|
||||
let mut crate_path = if expect_action(e.file_type(), ErrAction::Read, ".").is_dir()
|
||||
&& let Ok(crate_path) = e.file_name().into_string()
|
||||
&& crate_path.starts_with("clippy_lints")
|
||||
&& crate_path != "clippy_lints_internal"
|
||||
{
|
||||
crate_path
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
if name.starts_with("clippy_lints") && name != "clippy_lints_internal" {
|
||||
name.push_str("/src");
|
||||
for (file, module) in read_src_with_module(name.as_ref()) {
|
||||
|
||||
crate_path.push(path::MAIN_SEPARATOR);
|
||||
crate_path.push_str("src");
|
||||
for e in walk_dir_no_dot_or_target(&crate_path) {
|
||||
let e = expect_action(e, ErrAction::Read, &crate_path);
|
||||
if let Some(path) = e.path().to_str()
|
||||
&& let Some(path) = path.strip_suffix(".rs")
|
||||
&& let Some(path) = path.get(crate_path.len() + 1..)
|
||||
{
|
||||
let module = if path == "lib" {
|
||||
String::new()
|
||||
} else {
|
||||
let path = path
|
||||
.strip_suffix("mod")
|
||||
.and_then(|x| x.strip_suffix(path::MAIN_SEPARATOR))
|
||||
.unwrap_or(path);
|
||||
path.replace(['/', '\\'], "::")
|
||||
};
|
||||
parse_clippy_lint_decls(
|
||||
file.path(),
|
||||
File::open_read_to_cleared_string(file.path(), &mut contents),
|
||||
e.path(),
|
||||
File::open_read_to_cleared_string(e.path(), &mut contents),
|
||||
&module,
|
||||
&mut lints,
|
||||
);
|
||||
@@ -132,37 +151,6 @@ pub fn read_deprecated_lints(&mut self) -> (Vec<DeprecatedLint>, Vec<RenamedLint
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the source files from the given root directory
|
||||
fn read_src_with_module(src_root: &Path) -> impl use<'_> + Iterator<Item = (DirEntry, String)> {
|
||||
WalkDir::new(src_root).into_iter().filter_map(move |e| {
|
||||
let e = expect_action(e, ErrAction::Read, src_root);
|
||||
let path = e.path().as_os_str().as_encoded_bytes();
|
||||
if let Some(path) = path.strip_suffix(b".rs")
|
||||
&& let Some(path) = path.get(src_root.as_os_str().len() + 1..)
|
||||
{
|
||||
if path == b"lib" {
|
||||
Some((e, String::new()))
|
||||
} else {
|
||||
let path = if let Some(path) = path.strip_suffix(b"mod")
|
||||
&& let Some(path) = path.strip_suffix(b"/").or_else(|| path.strip_suffix(b"\\"))
|
||||
{
|
||||
path
|
||||
} else {
|
||||
path
|
||||
};
|
||||
if let Ok(path) = str::from_utf8(path) {
|
||||
let path = path.replace(['/', '\\'], "::");
|
||||
Some((e, path))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a source file looking for `declare_clippy_lint` macro invocations.
|
||||
fn parse_clippy_lint_decls(path: &Path, contents: &str, module: &str, lints: &mut Vec<Lint>) {
|
||||
#[allow(clippy::enum_glob_use)]
|
||||
|
||||
@@ -127,7 +127,7 @@ pub fn rename<'cx>(cx: ParseCx<'cx>, clippy_version: Version, old_name: &'cx str
|
||||
}
|
||||
|
||||
let mut update_fn = file_update_fn(old_name, new_name, mod_edit);
|
||||
for e in walk_dir_no_dot_or_target() {
|
||||
for e in walk_dir_no_dot_or_target(".") {
|
||||
let e = expect_action(e, ErrAction::Read, ".");
|
||||
if e.path().as_os_str().as_encoded_bytes().ends_with(b".rs") {
|
||||
updater.update_file(e.path(), &mut update_fn);
|
||||
|
||||
@@ -593,8 +593,8 @@ pub fn delete_dir_if_exists(path: &Path) {
|
||||
}
|
||||
|
||||
/// Walks all items excluding top-level dot files/directories and any target directories.
|
||||
pub fn walk_dir_no_dot_or_target() -> impl Iterator<Item = ::walkdir::Result<::walkdir::DirEntry>> {
|
||||
WalkDir::new(".").into_iter().filter_entry(|e| {
|
||||
pub fn walk_dir_no_dot_or_target(p: impl AsRef<Path>) -> impl Iterator<Item = ::walkdir::Result<::walkdir::DirEntry>> {
|
||||
WalkDir::new(p).into_iter().filter_entry(|e| {
|
||||
e.path()
|
||||
.file_name()
|
||||
.is_none_or(|x| x != "target" && x.as_encoded_bytes().first().copied() != Some(b'.'))
|
||||
|
||||
Reference in New Issue
Block a user