Skip to main content

xtask/
tidy.rs

1#![allow(clippy::disallowed_types, clippy::print_stderr)]
2use std::{
3    collections::HashSet,
4    path::{Path, PathBuf},
5};
6
7use itertools::Itertools;
8use xshell::Shell;
9
10use xshell::cmd;
11
12use crate::{flags::Tidy, project_root, util::list_files};
13
14impl Tidy {
15    pub(crate) fn run(&self, sh: &Shell) -> anyhow::Result<()> {
16        check_lsp_extensions_docs(sh);
17        files_are_tidy(sh);
18        check_licenses(sh);
19        Ok(())
20    }
21}
22
23fn check_lsp_extensions_docs(sh: &Shell) {
24    let actual_hash = {
25        let lsp_ext_rs =
26            sh.read_file(project_root().join("crates/rust-analyzer/src/lsp/ext.rs")).unwrap();
27        stable_hash(lsp_ext_rs.as_str())
28    };
29
30    let expected_hash = {
31        let lsp_extensions_md = sh
32            .read_file(project_root().join("docs/book/src/contributing/lsp-extensions.md"))
33            .unwrap();
34        let text = lsp_extensions_md
35            .lines()
36            .find_map(|line| line.strip_prefix("lsp/ext.rs hash:"))
37            .unwrap()
38            .trim();
39        u64::from_str_radix(text, 16).unwrap()
40    };
41
42    if actual_hash != expected_hash {
43        panic!(
44            "
45lsp/ext.rs was changed without touching lsp-extensions.md.
46
47Expected hash: {expected_hash:x}
48Actual hash:   {actual_hash:x}
49
50Please adjust docs/book/src/contributing/lsp-extensions.md.
51"
52        )
53    }
54}
55
56fn files_are_tidy(sh: &Shell) {
57    let files = list_files(&project_root().join("crates"));
58
59    let mut tidy_docs = TidyDocs::default();
60    let mut tidy_marks = TidyMarks::default();
61    for path in files {
62        let extension = path.extension().unwrap_or_default().to_str().unwrap_or_default();
63        match extension {
64            "rs" => {
65                let text = sh.read_file(&path).unwrap();
66                check_test_attrs(&path, &text);
67                check_trailing_ws(&path, &text);
68                tidy_docs.visit(&path, &text);
69                tidy_marks.visit(&path, &text);
70            }
71            "toml" => {
72                let text = sh.read_file(&path).unwrap();
73                check_cargo_toml(&path, text);
74            }
75            _ => (),
76        }
77    }
78
79    tidy_docs.finish();
80    tidy_marks.finish();
81}
82
83fn check_cargo_toml(path: &Path, text: String) {
84    let mut section = None;
85    for (line_no, text) in text.lines().enumerate() {
86        let text = text.trim();
87        if text.starts_with('[') {
88            if !text.ends_with(']') {
89                panic!(
90                    "\nplease don't add comments or trailing whitespace in section lines.\n\
91                        {}:{}\n",
92                    path.display(),
93                    line_no + 1
94                )
95            }
96            section = Some(text);
97            continue;
98        }
99        let text: String = text.split_whitespace().collect();
100        if !text.contains("path=") {
101            continue;
102        }
103        #[expect(
104            clippy::collapsible_match,
105            reason = "this changes meaning, as `dev-dependencies` includes `dependencies`"
106        )]
107        match section {
108            Some(s) if s.contains("dev-dependencies") => {
109                if text.contains("version") {
110                    panic!(
111                        "\ncargo internal dev-dependencies should not have a version.\n\
112                        {}:{}\n",
113                        path.display(),
114                        line_no + 1
115                    );
116                }
117            }
118            Some(s) if s.contains("dependencies") => {
119                if !text.contains("version") {
120                    panic!(
121                        "\ncargo internal dependencies should have a version.\n\
122                        {}:{}\n",
123                        path.display(),
124                        line_no + 1
125                    );
126                }
127            }
128            _ => {}
129        }
130    }
131}
132
133fn check_licenses(sh: &Shell) {
134    const EXPECTED: &[&str] = &[
135        "(MIT OR Apache-2.0) AND Unicode-3.0",
136        "0BSD OR MIT OR Apache-2.0",
137        "Apache-2.0 / MIT",
138        "Apache-2.0 OR MIT",
139        "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT",
140        "Apache-2.0 WITH LLVM-exception",
141        "Apache-2.0",
142        "Apache-2.0/MIT",
143        "BSD-2-Clause OR Apache-2.0 OR MIT",
144        "CC0-1.0",
145        "ISC",
146        "MIT / Apache-2.0",
147        "MIT OR Apache-2.0 OR LGPL-2.1-or-later",
148        "MIT OR Apache-2.0",
149        "MIT OR Zlib OR Apache-2.0",
150        "MIT",
151        "MIT/Apache-2.0",
152        "MPL-2.0",
153        "Unicode-3.0",
154        "Unlicense OR MIT",
155        "Unlicense/MIT",
156        "Zlib",
157        "Zlib OR Apache-2.0 OR MIT",
158    ];
159
160    let meta = cmd!(sh, "cargo metadata --format-version 1").read().unwrap();
161    let mut licenses = meta
162        .split([',', '{', '}'])
163        .filter(|it| it.contains(r#""license""#))
164        .map(|it| it.trim())
165        .map(|it| it[r#""license":"#.len()..].trim_matches('"'))
166        .collect::<Vec<_>>();
167    licenses.sort_unstable();
168    licenses.dedup();
169    let mut expected = EXPECTED.to_vec();
170    expected.sort_unstable();
171    if licenses != expected {
172        let mut diff = String::new();
173
174        diff.push_str("New Licenses:\n");
175        for &l in licenses.iter() {
176            if !expected.contains(&l) {
177                diff += &format!("  {l}\n")
178            }
179        }
180
181        diff.push_str("\nMissing Licenses:\n");
182        for l in expected {
183            if !licenses.contains(&l) {
184                diff += &format!("  {l}\n")
185            }
186        }
187
188        panic!("different set of licenses!\n{diff}");
189    }
190    assert_eq!(licenses, expected);
191}
192
193fn check_test_attrs(path: &Path, text: &str) {
194    let panic_rule = "https://github.com/rust-lang/rust-analyzer/blob/master/docs/book/src/contributing/style.md#should_panic";
195    let need_panic: &[&str] = &[
196        // This file.
197        "slow-tests/tidy.rs",
198        "test-utils/src/fixture.rs",
199        // Generated code from lints contains doc tests in string literals.
200        "ide-db/src/generated/lints.rs",
201        "proc-macro-srv/src/tests/mod.rs",
202    ];
203    if need_panic.iter().any(|p| path.ends_with(p)) {
204        return;
205    }
206    if let Some((line, _)) = text
207        .lines()
208        .tuple_windows()
209        .enumerate()
210        .find(|(_, (a, b))| b.contains("#[should_panic") && !a.contains("FIXME"))
211    {
212        panic!(
213            "\ndon't add `#[should_panic]` tests, see:\n\n    {}\n\n   {}:{line}\n",
214            panic_rule,
215            path.display(),
216        )
217    }
218}
219
220fn check_trailing_ws(path: &Path, text: &str) {
221    if is_exclude_dir(path, &["test_data"]) {
222        return;
223    }
224    for (line_number, line) in text.lines().enumerate() {
225        if line.chars().last().is_some_and(char::is_whitespace) {
226            panic!("Trailing whitespace in {} at line {}", path.display(), line_number + 1)
227        }
228    }
229}
230
231#[derive(Default)]
232struct TidyDocs {
233    missing_docs: Vec<String>,
234    contains_fixme: Vec<PathBuf>,
235}
236
237impl TidyDocs {
238    fn visit(&mut self, path: &Path, text: &str) {
239        // Tests and diagnostic fixes don't need module level comments.
240        if is_exclude_dir(path, &["tests", "test_data", "fixes", "grammar", "stdx"]) {
241            return;
242        }
243
244        if is_exclude_file(path) {
245            return;
246        }
247
248        if is_ported_from_rustc(path, &["crates/hir-ty/src/next_solver"]) {
249            return;
250        }
251
252        let first_line = match text.lines().next() {
253            Some(it) => it,
254            None => return,
255        };
256
257        if first_line.starts_with("//!") {
258            if first_line.contains("FIXME") {
259                self.contains_fixme.push(path.to_path_buf());
260            }
261        } else {
262            if text.contains("// Feature:")
263                || text.contains("// Assist:")
264                || text.contains("// Diagnostic:")
265            {
266                return;
267            }
268            self.missing_docs.push(path.display().to_string());
269        }
270
271        fn is_exclude_file(d: &Path) -> bool {
272            let file_names = ["tests.rs", "famous_defs_fixture.rs", "frontmatter.rs"];
273
274            d.file_name()
275                .unwrap_or_default()
276                .to_str()
277                .map(|f_n| file_names.contains(&f_n))
278                .unwrap_or(false)
279        }
280    }
281
282    fn finish(self) {
283        if !self.missing_docs.is_empty() {
284            panic!(
285                "\nMissing docs strings\n\n\
286                 modules:\n{}\n\n",
287                self.missing_docs.join("\n")
288            )
289        }
290
291        if let Some(path) = self.contains_fixme.first() {
292            panic!("FIXME doc in a fully-documented crate: {}", path.display())
293        }
294    }
295}
296
297fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
298    p.strip_prefix(project_root())
299        .unwrap()
300        .components()
301        .rev()
302        .skip(1)
303        .filter_map(|it| it.as_os_str().to_str())
304        .any(|it| dirs_to_exclude.contains(&it))
305}
306
307fn is_ported_from_rustc(p: &Path, dirs_to_exclude: &[&str]) -> bool {
308    let p = p.strip_prefix(project_root()).unwrap();
309    dirs_to_exclude.iter().any(|exclude| p.starts_with(exclude))
310}
311
312#[derive(Default)]
313struct TidyMarks {
314    hits: HashSet<String>,
315    checks: HashSet<String>,
316}
317
318impl TidyMarks {
319    fn visit(&mut self, _path: &Path, text: &str) {
320        find_marks(&mut self.hits, text, "hit");
321        find_marks(&mut self.checks, text, "check");
322        find_marks(&mut self.checks, text, "check_count");
323    }
324
325    fn finish(self) {
326        assert!(!self.hits.is_empty());
327
328        let diff: Vec<_> =
329            self.hits.symmetric_difference(&self.checks).map(|it| it.as_str()).collect();
330
331        if !diff.is_empty() {
332            panic!("unpaired marks: {diff:?}")
333        }
334    }
335}
336
337#[allow(deprecated)]
338fn stable_hash(text: &str) -> u64 {
339    use std::hash::{Hash, Hasher, SipHasher};
340
341    let text = text.replace('\r', "");
342    let mut hasher = SipHasher::default();
343    text.hash(&mut hasher);
344    hasher.finish()
345}
346
347fn find_marks(set: &mut HashSet<String>, text: &str, mark: &str) {
348    let mut text = text;
349    let mut prev_text = "";
350    while text != prev_text {
351        prev_text = text;
352        if let Some(idx) = text.find(mark) {
353            text = &text[idx + mark.len()..];
354            if let Some(stripped_text) = text.strip_prefix("!(") {
355                text = stripped_text.trim_start();
356                if let Some(idx2) = text.find(|c: char| !(c.is_alphanumeric() || c == '_')) {
357                    let mark_text = &text[..idx2];
358                    set.insert(mark_text.to_owned());
359                    text = &text[idx2..];
360                }
361            }
362        }
363    }
364}
365
366#[test]
367fn test() {
368    Tidy {}.run(&Shell::new().unwrap()).unwrap();
369}