|
| 1 | +pub mod cursor; |
| 2 | + |
| 3 | +use self::cursor::{Capture, Cursor}; |
| 4 | +use crate::utils::{ErrAction, File, expect_action}; |
| 5 | +use core::range::Range; |
| 6 | +use std::fs; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use walkdir::{DirEntry, WalkDir}; |
| 9 | + |
| 10 | +pub struct Lint { |
| 11 | + pub name: String, |
| 12 | + pub group: String, |
| 13 | + pub module: String, |
| 14 | + pub path: PathBuf, |
| 15 | + pub declaration_range: Range<usize>, |
| 16 | +} |
| 17 | + |
| 18 | +pub struct DeprecatedLint { |
| 19 | + pub name: String, |
| 20 | + pub reason: String, |
| 21 | + pub version: String, |
| 22 | +} |
| 23 | + |
| 24 | +pub struct RenamedLint { |
| 25 | + pub old_name: String, |
| 26 | + pub new_name: String, |
| 27 | + pub version: String, |
| 28 | +} |
| 29 | + |
| 30 | +/// Finds all lint declarations (`declare_clippy_lint!`) |
| 31 | +#[must_use] |
| 32 | +pub fn find_lint_decls() -> Vec<Lint> { |
| 33 | + let mut lints = Vec::with_capacity(1000); |
| 34 | + let mut contents = String::new(); |
| 35 | + for e in expect_action(fs::read_dir("."), ErrAction::Read, ".") { |
| 36 | + let e = expect_action(e, ErrAction::Read, "."); |
| 37 | + if !expect_action(e.file_type(), ErrAction::Read, ".").is_dir() { |
| 38 | + continue; |
| 39 | + } |
| 40 | + let Ok(mut name) = e.file_name().into_string() else { |
| 41 | + continue; |
| 42 | + }; |
| 43 | + if name.starts_with("clippy_lints") && name != "clippy_lints_internal" { |
| 44 | + name.push_str("/src"); |
| 45 | + for (file, module) in read_src_with_module(name.as_ref()) { |
| 46 | + parse_clippy_lint_decls( |
| 47 | + file.path(), |
| 48 | + File::open_read_to_cleared_string(file.path(), &mut contents), |
| 49 | + &module, |
| 50 | + &mut lints, |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + lints.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name)); |
| 56 | + lints |
| 57 | +} |
| 58 | + |
| 59 | +/// Reads the source files from the given root directory |
| 60 | +fn read_src_with_module(src_root: &Path) -> impl use<'_> + Iterator<Item = (DirEntry, String)> { |
| 61 | + WalkDir::new(src_root).into_iter().filter_map(move |e| { |
| 62 | + let e = expect_action(e, ErrAction::Read, src_root); |
| 63 | + let path = e.path().as_os_str().as_encoded_bytes(); |
| 64 | + if let Some(path) = path.strip_suffix(b".rs") |
| 65 | + && let Some(path) = path.get(src_root.as_os_str().len() + 1..) |
| 66 | + { |
| 67 | + if path == b"lib" { |
| 68 | + Some((e, String::new())) |
| 69 | + } else { |
| 70 | + let path = if let Some(path) = path.strip_suffix(b"mod") |
| 71 | + && let Some(path) = path.strip_suffix(b"/").or_else(|| path.strip_suffix(b"\\")) |
| 72 | + { |
| 73 | + path |
| 74 | + } else { |
| 75 | + path |
| 76 | + }; |
| 77 | + if let Ok(path) = str::from_utf8(path) { |
| 78 | + let path = path.replace(['/', '\\'], "::"); |
| 79 | + Some((e, path)) |
| 80 | + } else { |
| 81 | + None |
| 82 | + } |
| 83 | + } |
| 84 | + } else { |
| 85 | + None |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +/// Parse a source file looking for `declare_clippy_lint` macro invocations. |
| 91 | +fn parse_clippy_lint_decls(path: &Path, contents: &str, module: &str, lints: &mut Vec<Lint>) { |
| 92 | + #[allow(clippy::enum_glob_use)] |
| 93 | + use cursor::Pat::*; |
| 94 | + #[rustfmt::skip] |
| 95 | + static DECL_TOKENS: &[cursor::Pat<'_>] = &[ |
| 96 | + // !{ /// docs |
| 97 | + Bang, OpenBrace, AnyComment, |
| 98 | + // #[clippy::version = "version"] |
| 99 | + Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, LitStr, CloseBracket, |
| 100 | + // pub NAME, GROUP, |
| 101 | + Ident("pub"), CaptureIdent, Comma, AnyComment, CaptureIdent, Comma, |
| 102 | + ]; |
| 103 | + |
| 104 | + let mut cursor = Cursor::new(contents); |
| 105 | + let mut captures = [Capture::EMPTY; 2]; |
| 106 | + while let Some(start) = cursor.find_ident("declare_clippy_lint") { |
| 107 | + if cursor.match_all(DECL_TOKENS, &mut captures) && cursor.find_pat(CloseBrace) { |
| 108 | + lints.push(Lint { |
| 109 | + name: cursor.get_text(captures[0]).to_lowercase(), |
| 110 | + group: cursor.get_text(captures[1]).into(), |
| 111 | + module: module.into(), |
| 112 | + path: path.into(), |
| 113 | + declaration_range: start as usize..cursor.pos() as usize, |
| 114 | + }); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[must_use] |
| 120 | +pub fn read_deprecated_lints() -> (Vec<DeprecatedLint>, Vec<RenamedLint>) { |
| 121 | + #[allow(clippy::enum_glob_use)] |
| 122 | + use cursor::Pat::*; |
| 123 | + #[rustfmt::skip] |
| 124 | + static DECL_TOKENS: &[cursor::Pat<'_>] = &[ |
| 125 | + // #[clippy::version = "version"] |
| 126 | + Pound, OpenBracket, Ident("clippy"), DoubleColon, Ident("version"), Eq, CaptureLitStr, CloseBracket, |
| 127 | + // ("first", "second"), |
| 128 | + OpenParen, CaptureLitStr, Comma, CaptureLitStr, CloseParen, Comma, |
| 129 | + ]; |
| 130 | + #[rustfmt::skip] |
| 131 | + static DEPRECATED_TOKENS: &[cursor::Pat<'_>] = &[ |
| 132 | + // !{ DEPRECATED(DEPRECATED_VERSION) = [ |
| 133 | + Bang, OpenBrace, Ident("DEPRECATED"), OpenParen, Ident("DEPRECATED_VERSION"), CloseParen, Eq, OpenBracket, |
| 134 | + ]; |
| 135 | + #[rustfmt::skip] |
| 136 | + static RENAMED_TOKENS: &[cursor::Pat<'_>] = &[ |
| 137 | + // !{ RENAMED(RENAMED_VERSION) = [ |
| 138 | + Bang, OpenBrace, Ident("RENAMED"), OpenParen, Ident("RENAMED_VERSION"), CloseParen, Eq, OpenBracket, |
| 139 | + ]; |
| 140 | + |
| 141 | + let path = "clippy_lints/src/deprecated_lints.rs"; |
| 142 | + let mut deprecated = Vec::with_capacity(30); |
| 143 | + let mut renamed = Vec::with_capacity(80); |
| 144 | + let mut contents = String::new(); |
| 145 | + File::open_read_to_cleared_string(path, &mut contents); |
| 146 | + |
| 147 | + let mut cursor = Cursor::new(&contents); |
| 148 | + let mut captures = [Capture::EMPTY; 3]; |
| 149 | + |
| 150 | + // First instance is the macro definition. |
| 151 | + assert!( |
| 152 | + cursor.find_ident("declare_with_version").is_some(), |
| 153 | + "error reading deprecated lints" |
| 154 | + ); |
| 155 | + |
| 156 | + if cursor.find_ident("declare_with_version").is_some() && cursor.match_all(DEPRECATED_TOKENS, &mut []) { |
| 157 | + while cursor.match_all(DECL_TOKENS, &mut captures) { |
| 158 | + deprecated.push(DeprecatedLint { |
| 159 | + name: parse_str_single_line(path.as_ref(), cursor.get_text(captures[1])), |
| 160 | + reason: parse_str_single_line(path.as_ref(), cursor.get_text(captures[2])), |
| 161 | + version: parse_str_single_line(path.as_ref(), cursor.get_text(captures[0])), |
| 162 | + }); |
| 163 | + } |
| 164 | + } else { |
| 165 | + panic!("error reading deprecated lints"); |
| 166 | + } |
| 167 | + |
| 168 | + if cursor.find_ident("declare_with_version").is_some() && cursor.match_all(RENAMED_TOKENS, &mut []) { |
| 169 | + while cursor.match_all(DECL_TOKENS, &mut captures) { |
| 170 | + renamed.push(RenamedLint { |
| 171 | + old_name: parse_str_single_line(path.as_ref(), cursor.get_text(captures[1])), |
| 172 | + new_name: parse_str_single_line(path.as_ref(), cursor.get_text(captures[2])), |
| 173 | + version: parse_str_single_line(path.as_ref(), cursor.get_text(captures[0])), |
| 174 | + }); |
| 175 | + } |
| 176 | + } else { |
| 177 | + panic!("error reading renamed lints"); |
| 178 | + } |
| 179 | + |
| 180 | + deprecated.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name)); |
| 181 | + renamed.sort_by(|lhs, rhs| lhs.old_name.cmp(&rhs.old_name)); |
| 182 | + (deprecated, renamed) |
| 183 | +} |
| 184 | + |
| 185 | +/// Removes the line splices and surrounding quotes from a string literal |
| 186 | +fn parse_str_lit(s: &str) -> String { |
| 187 | + let (s, is_raw) = if let Some(s) = s.strip_prefix("r") { |
| 188 | + (s.trim_matches('#'), true) |
| 189 | + } else { |
| 190 | + (s, false) |
| 191 | + }; |
| 192 | + let s = s |
| 193 | + .strip_prefix('"') |
| 194 | + .and_then(|s| s.strip_suffix('"')) |
| 195 | + .unwrap_or_else(|| panic!("expected quoted string, found `{s}`")); |
| 196 | + |
| 197 | + if is_raw { |
| 198 | + s.into() |
| 199 | + } else { |
| 200 | + let mut res = String::with_capacity(s.len()); |
| 201 | + rustc_literal_escaper::unescape_str(s, &mut |_, ch| { |
| 202 | + if let Ok(ch) = ch { |
| 203 | + res.push(ch); |
| 204 | + } |
| 205 | + }); |
| 206 | + res |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +fn parse_str_single_line(path: &Path, s: &str) -> String { |
| 211 | + let value = parse_str_lit(s); |
| 212 | + assert!( |
| 213 | + !value.contains('\n'), |
| 214 | + "error parsing `{}`: `{s}` should be a single line string", |
| 215 | + path.display(), |
| 216 | + ); |
| 217 | + value |
| 218 | +} |
0 commit comments