]> git.lizzy.rs Git - rust.git/blob - xtask/src/tidy.rs
Make working with codegen less annoying
[rust.git] / xtask / src / tidy.rs
1 use std::{
2     collections::HashMap,
3     path::{Path, PathBuf},
4 };
5
6 use xshell::{cmd, read_file};
7
8 use crate::{
9     cargo_files,
10     codegen::{self, Mode},
11     project_root, run_rustfmt, rust_files,
12 };
13
14 #[test]
15 fn generated_grammar_is_fresh() {
16     codegen::generate_syntax(Mode::Ensure).unwrap()
17 }
18
19 #[test]
20 fn generated_tests_are_fresh() {
21     codegen::generate_parser_tests(Mode::Ensure).unwrap()
22 }
23
24 #[test]
25 fn generated_assists_are_fresh() {
26     codegen::generate_assists_tests(Mode::Ensure).unwrap();
27 }
28
29 #[test]
30 fn check_code_formatting() {
31     run_rustfmt(Mode::Ensure).unwrap()
32 }
33
34 #[test]
35 fn smoke_test_docs_generation() {
36     // We don't commit docs to the repo, so we can just overwrite in tests.
37     codegen::generate_assists_docs(Mode::Overwrite).unwrap();
38     codegen::generate_feature_docs(Mode::Overwrite).unwrap();
39     codegen::generate_diagnostic_docs(Mode::Overwrite).unwrap();
40 }
41
42 #[test]
43 fn check_lsp_extensions_docs() {
44     let expected_hash = {
45         let lsp_ext_rs =
46             read_file(project_root().join("crates/rust-analyzer/src/lsp_ext.rs")).unwrap();
47         stable_hash(lsp_ext_rs.as_str())
48     };
49
50     let actual_hash = {
51         let lsp_extensions_md =
52             read_file(project_root().join("docs/dev/lsp-extensions.md")).unwrap();
53         let text = lsp_extensions_md
54             .lines()
55             .find_map(|line| line.strip_prefix("lsp_ext.rs hash:"))
56             .unwrap()
57             .trim();
58         u64::from_str_radix(text, 16).unwrap()
59     };
60
61     if actual_hash != expected_hash {
62         panic!(
63             "
64 lsp_ext.rs was changed without touching lsp-extensions.md.
65
66 Expected hash: {:x}
67 Actual hash:   {:x}
68
69 Please adjust docs/dev/lsp-extensions.md.
70 ",
71             expected_hash, actual_hash
72         )
73     }
74 }
75
76 #[test]
77 fn rust_files_are_tidy() {
78     let mut tidy_docs = TidyDocs::default();
79     for path in rust_files() {
80         let text = read_file(&path).unwrap();
81         check_todo(&path, &text);
82         check_dbg(&path, &text);
83         check_trailing_ws(&path, &text);
84         deny_clippy(&path, &text);
85         tidy_docs.visit(&path, &text);
86     }
87     tidy_docs.finish();
88 }
89
90 #[test]
91 fn cargo_files_are_tidy() {
92     for cargo in cargo_files() {
93         let mut section = None;
94         for (line_no, text) in read_file(&cargo).unwrap().lines().enumerate() {
95             let text = text.trim();
96             if text.starts_with('[') {
97                 if !text.ends_with(']') {
98                     panic!(
99                         "\nplease don't add comments or trailing whitespace in section lines.\n\
100                             {}:{}\n",
101                         cargo.display(),
102                         line_no + 1
103                     )
104                 }
105                 section = Some(text);
106                 continue;
107             }
108             let text: String = text.split_whitespace().collect();
109             if !text.contains("path=") {
110                 continue;
111             }
112             match section {
113                 Some(s) if s.contains("dev-dependencies") => {
114                     if text.contains("version") {
115                         panic!(
116                             "\ncargo internal dev-dependencies should not have a version.\n\
117                             {}:{}\n",
118                             cargo.display(),
119                             line_no + 1
120                         );
121                     }
122                 }
123                 Some(s) if s.contains("dependencies") => {
124                     if !text.contains("version") {
125                         panic!(
126                             "\ncargo internal dependencies should have a version.\n\
127                             {}:{}\n",
128                             cargo.display(),
129                             line_no + 1
130                         );
131                     }
132                 }
133                 _ => {}
134             }
135         }
136     }
137 }
138
139 #[test]
140 fn check_merge_commits() {
141     let stdout = cmd!("git rev-list --merges --invert-grep --author 'bors\\[bot\\]' HEAD~19..")
142         .read()
143         .unwrap();
144     if !stdout.is_empty() {
145         panic!(
146             "
147 Merge commits are not allowed in the history.
148
149 When updating a pull-request, please rebase your feature branch
150 on top of master by running `git rebase master`. If rebase fails,
151 you can re-apply your changes like this:
152
153   # Just look around to see the current state.
154   $ git status
155   $ git log
156
157   # Abort in-progress rebase and merges, if any.
158   $ git rebase --abort
159   $ git merge --abort
160
161   # Make the branch point to the latest commit from master,
162   # while maintaining your local changes uncommited.
163   $ git reset --soft origin/master
164
165   # Commit all changes in a single batch.
166   $ git commit -am'My changes'
167
168   # Verify that everything looks alright.
169   $ git status
170   $ git log
171
172   # Push the changes. We did a rebase, so we need `--force` option.
173   # `--force-with-lease` is a more safe (Rusty) version of `--force`.
174   $ git push --force-with-lease
175
176   # Verify that both local and remote branch point to the same commit.
177   $ git log
178
179 And don't fear to mess something up during a rebase -- you can
180 always restore the previous state using `git ref-log`:
181
182 https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/#redo-after-undo-local
183 "
184         );
185     }
186 }
187
188 fn deny_clippy(path: &PathBuf, text: &String) {
189     let ignore = &[
190         // The documentation in string literals may contain anything for its own purposes
191         "ide_completion/src/generated_lint_completions.rs",
192     ];
193     if ignore.iter().any(|p| path.ends_with(p)) {
194         return;
195     }
196
197     if text.contains("\u{61}llow(clippy") {
198         panic!(
199             "\n\nallowing lints is forbidden: {}.
200 rust-analyzer intentionally doesn't check clippy on CI.
201 You can allow lint globally via `xtask clippy`.
202 See https://github.com/rust-lang/rust-clippy/issues/5537 for discussion.
203
204 ",
205             path.display()
206         )
207     }
208 }
209
210 #[test]
211 fn check_licenses() {
212     let expected = "
213 0BSD OR MIT OR Apache-2.0
214 Apache-2.0
215 Apache-2.0 OR BSL-1.0
216 Apache-2.0 OR MIT
217 Apache-2.0/MIT
218 BSD-3-Clause
219 CC0-1.0
220 ISC
221 MIT
222 MIT / Apache-2.0
223 MIT OR Apache-2.0
224 MIT OR Apache-2.0 OR Zlib
225 MIT OR Zlib OR Apache-2.0
226 MIT/Apache-2.0
227 Unlicense OR MIT
228 Unlicense/MIT
229 Zlib OR Apache-2.0 OR MIT
230 "
231     .lines()
232     .filter(|it| !it.is_empty())
233     .collect::<Vec<_>>();
234
235     let meta = cmd!("cargo metadata --format-version 1").read().unwrap();
236     let mut licenses = meta
237         .split(|c| c == ',' || c == '{' || c == '}')
238         .filter(|it| it.contains(r#""license""#))
239         .map(|it| it.trim())
240         .map(|it| it[r#""license":"#.len()..].trim_matches('"'))
241         .collect::<Vec<_>>();
242     licenses.sort();
243     licenses.dedup();
244     if licenses != expected {
245         let mut diff = String::new();
246
247         diff += &format!("New Licenses:\n");
248         for &l in licenses.iter() {
249             if !expected.contains(&l) {
250                 diff += &format!("  {}\n", l)
251             }
252         }
253
254         diff += &format!("\nMissing Licenses:\n");
255         for &l in expected.iter() {
256             if !licenses.contains(&l) {
257                 diff += &format!("  {}\n", l)
258             }
259         }
260
261         panic!("different set of licenses!\n{}", diff);
262     }
263     assert_eq!(licenses, expected);
264 }
265
266 fn check_todo(path: &Path, text: &str) {
267     let need_todo = &[
268         // This file itself obviously needs to use todo (<- like this!).
269         "tests/tidy.rs",
270         // Some of our assists generate `todo!()`.
271         "handlers/add_turbo_fish.rs",
272         "handlers/generate_function.rs",
273         // To support generating `todo!()` in assists, we have `expr_todo()` in
274         // `ast::make`.
275         "ast/make.rs",
276         // The documentation in string literals may contain anything for its own purposes
277         "ide_completion/src/generated_lint_completions.rs",
278     ];
279     if need_todo.iter().any(|p| path.ends_with(p)) {
280         return;
281     }
282     if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") {
283         // Generated by an assist
284         if text.contains("${0:todo!()}") {
285             return;
286         }
287
288         panic!(
289             "\nTODO markers or todo! macros should not be committed to the master branch,\n\
290              use FIXME instead\n\
291              {}\n",
292             path.display(),
293         )
294     }
295 }
296
297 fn check_dbg(path: &Path, text: &str) {
298     let need_dbg = &[
299         // This file itself obviously needs to use dbg.
300         "tests/tidy.rs",
301         // Assists to remove `dbg!()`
302         "handlers/remove_dbg.rs",
303         // We have .dbg postfix
304         "ide_completion/src/completions/postfix.rs",
305         // The documentation in string literals may contain anything for its own purposes
306         "ide_completion/src/lib.rs",
307         "ide_completion/src/generated_lint_completions.rs",
308         // test for doc test for remove_dbg
309         "src/tests/generated.rs",
310     ];
311     if need_dbg.iter().any(|p| path.ends_with(p)) {
312         return;
313     }
314     if text.contains("dbg!") {
315         panic!(
316             "\ndbg! macros should not be committed to the master branch,\n\
317              {}\n",
318             path.display(),
319         )
320     }
321 }
322
323 fn check_trailing_ws(path: &Path, text: &str) {
324     if is_exclude_dir(path, &["test_data"]) {
325         return;
326     }
327     for (line_number, line) in text.lines().enumerate() {
328         if line.chars().last().map(char::is_whitespace) == Some(true) {
329             panic!("Trailing whitespace in {} at line {}", path.display(), line_number)
330         }
331     }
332 }
333
334 #[derive(Default)]
335 struct TidyDocs {
336     missing_docs: Vec<String>,
337     contains_fixme: Vec<PathBuf>,
338 }
339
340 impl TidyDocs {
341     fn visit(&mut self, path: &Path, text: &str) {
342         // Test hopefully don't really need comments, and for assists we already
343         // have special comments which are source of doc tests and user docs.
344         if is_exclude_dir(path, &["tests", "test_data"]) {
345             return;
346         }
347
348         if is_exclude_file(path) {
349             return;
350         }
351
352         let first_line = match text.lines().next() {
353             Some(it) => it,
354             None => return,
355         };
356
357         if first_line.starts_with("//!") {
358             if first_line.contains("FIXME") {
359                 self.contains_fixme.push(path.to_path_buf());
360             }
361         } else {
362             if text.contains("// Feature:") || text.contains("// Assist:") {
363                 return;
364             }
365             self.missing_docs.push(path.display().to_string());
366         }
367
368         fn is_exclude_file(d: &Path) -> bool {
369             let file_names = ["tests.rs", "famous_defs_fixture.rs"];
370
371             d.file_name()
372                 .unwrap_or_default()
373                 .to_str()
374                 .map(|f_n| file_names.iter().any(|name| *name == f_n))
375                 .unwrap_or(false)
376         }
377     }
378
379     fn finish(self) {
380         if !self.missing_docs.is_empty() {
381             panic!(
382                 "\nMissing docs strings\n\n\
383                  modules:\n{}\n\n",
384                 self.missing_docs.join("\n")
385             )
386         }
387
388         let poorly_documented = [
389             "hir",
390             "hir_expand",
391             "ide",
392             "mbe",
393             "parser",
394             "profile",
395             "project_model",
396             "syntax",
397             "tt",
398             "hir_ty",
399         ];
400
401         let mut has_fixmes =
402             poorly_documented.iter().map(|it| (*it, false)).collect::<HashMap<&str, bool>>();
403         'outer: for path in self.contains_fixme {
404             for krate in poorly_documented.iter() {
405                 if path.components().any(|it| it.as_os_str() == *krate) {
406                     has_fixmes.insert(krate, true);
407                     continue 'outer;
408                 }
409             }
410             panic!("FIXME doc in a fully-documented crate: {}", path.display())
411         }
412
413         for (krate, has_fixme) in has_fixmes.iter() {
414             if !has_fixme {
415                 panic!("crate {} is fully documented :tada:, remove it from the list of poorly documented crates", krate)
416             }
417         }
418     }
419 }
420
421 fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
422     p.strip_prefix(project_root())
423         .unwrap()
424         .components()
425         .rev()
426         .skip(1)
427         .filter_map(|it| it.as_os_str().to_str())
428         .any(|it| dirs_to_exclude.contains(&it))
429 }
430
431 #[allow(deprecated)]
432 fn stable_hash(text: &str) -> u64 {
433     use std::hash::{Hash, Hasher, SipHasher};
434
435     let text = text.replace('\r', "");
436     let mut hasher = SipHasher::default();
437     text.hash(&mut hasher);
438     hasher.finish()
439 }