|
1 | 1 | pub mod cursor; |
2 | 2 |
|
3 | 3 | 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}; |
5 | 5 | use core::range::Range; |
6 | 6 | use std::fs; |
7 | | -use std::path::{Path, PathBuf}; |
8 | | -use walkdir::{DirEntry, WalkDir}; |
| 7 | +use std::path::{self, Path, PathBuf}; |
9 | 8 |
|
10 | 9 | pub struct ParseCxImpl; |
11 | 10 | pub type ParseCx<'cx> = &'cx mut ParseCxImpl; |
@@ -43,18 +42,38 @@ impl ParseCxImpl { |
43 | 42 | let mut contents = String::new(); |
44 | 43 | for e in expect_action(fs::read_dir("."), ErrAction::Read, ".") { |
45 | 44 | 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 { |
50 | 54 | continue; |
51 | 55 | }; |
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 | + }; |
55 | 74 | 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), |
58 | 77 | &module, |
59 | 78 | &mut lints, |
60 | 79 | ); |
@@ -132,37 +151,6 @@ impl ParseCxImpl { |
132 | 151 | } |
133 | 152 | } |
134 | 153 |
|
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 | | - |
166 | 154 | /// Parse a source file looking for `declare_clippy_lint` macro invocations. |
167 | 155 | fn parse_clippy_lint_decls(path: &Path, contents: &str, module: &str, lints: &mut Vec<Lint>) { |
168 | 156 | #[allow(clippy::enum_glob_use)] |
|
0 commit comments