6 use xshell::{cmd, read_file};
10 codegen::{self, Mode},
11 project_root, run_rustfmt, rust_files,
15 fn generated_grammar_is_fresh() {
16 codegen::generate_syntax(Mode::Ensure).unwrap()
20 fn generated_tests_are_fresh() {
21 codegen::generate_parser_tests(Mode::Ensure).unwrap()
25 fn generated_assists_are_fresh() {
26 codegen::generate_assists_tests(Mode::Ensure).unwrap();
30 fn check_code_formatting() {
31 run_rustfmt(Mode::Ensure).unwrap()
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();
43 fn check_lsp_extensions_docs() {
46 read_file(project_root().join("crates/rust-analyzer/src/lsp_ext.rs")).unwrap();
47 stable_hash(lsp_ext_rs.as_str())
51 let lsp_extensions_md =
52 read_file(project_root().join("docs/dev/lsp-extensions.md")).unwrap();
53 let text = lsp_extensions_md
55 .find_map(|line| line.strip_prefix("lsp_ext.rs hash:"))
58 u64::from_str_radix(text, 16).unwrap()
61 if actual_hash != expected_hash {
64 lsp_ext.rs was changed without touching lsp-extensions.md.
69 Please adjust docs/dev/lsp-extensions.md.
71 expected_hash, actual_hash
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);
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(']') {
99 "\nplease don't add comments or trailing whitespace in section lines.\n\
105 section = Some(text);
108 let text: String = text.split_whitespace().collect();
109 if !text.contains("path=") {
113 Some(s) if s.contains("dev-dependencies") => {
114 if text.contains("version") {
116 "\ncargo internal dev-dependencies should not have a version.\n\
123 Some(s) if s.contains("dependencies") => {
124 if !text.contains("version") {
126 "\ncargo internal dependencies should have a version.\n\
140 fn check_merge_commits() {
141 let stdout = cmd!("git rev-list --merges --invert-grep --author 'bors\\[bot\\]' HEAD~19..")
144 if !stdout.is_empty() {
147 Merge commits are not allowed in the history.
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:
153 # Just look around to see the current state.
157 # Abort in-progress rebase and merges, if any.
161 # Make the branch point to the latest commit from master,
162 # while maintaining your local changes uncommited.
163 $ git reset --soft origin/master
165 # Commit all changes in a single batch.
166 $ git commit -am'My changes'
168 # Verify that everything looks alright.
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
176 # Verify that both local and remote branch point to the same commit.
179 And don't fear to mess something up during a rebase -- you can
180 always restore the previous state using `git ref-log`:
182 https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/#redo-after-undo-local
188 fn deny_clippy(path: &PathBuf, text: &String) {
190 // The documentation in string literals may contain anything for its own purposes
191 "ide_completion/src/generated_lint_completions.rs",
193 if ignore.iter().any(|p| path.ends_with(p)) {
197 if text.contains("\u{61}llow(clippy") {
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.
211 fn check_licenses() {
213 0BSD OR MIT OR Apache-2.0
215 Apache-2.0 OR BSL-1.0
224 MIT OR Apache-2.0 OR Zlib
225 MIT OR Zlib OR Apache-2.0
229 Zlib OR Apache-2.0 OR MIT
232 .filter(|it| !it.is_empty())
233 .collect::<Vec<_>>();
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""#))
240 .map(|it| it[r#""license":"#.len()..].trim_matches('"'))
241 .collect::<Vec<_>>();
244 if licenses != expected {
245 let mut diff = String::new();
247 diff += &format!("New Licenses:\n");
248 for &l in licenses.iter() {
249 if !expected.contains(&l) {
250 diff += &format!(" {}\n", l)
254 diff += &format!("\nMissing Licenses:\n");
255 for &l in expected.iter() {
256 if !licenses.contains(&l) {
257 diff += &format!(" {}\n", l)
261 panic!("different set of licenses!\n{}", diff);
263 assert_eq!(licenses, expected);
266 fn check_todo(path: &Path, text: &str) {
268 // This file itself obviously needs to use todo (<- like this!).
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
276 // The documentation in string literals may contain anything for its own purposes
277 "ide_completion/src/generated_lint_completions.rs",
279 if need_todo.iter().any(|p| path.ends_with(p)) {
282 if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") {
283 // Generated by an assist
284 if text.contains("${0:todo!()}") {
289 "\nTODO markers or todo! macros should not be committed to the master branch,\n\
297 fn check_dbg(path: &Path, text: &str) {
299 // This file itself obviously needs to use dbg.
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",
311 if need_dbg.iter().any(|p| path.ends_with(p)) {
314 if text.contains("dbg!") {
316 "\ndbg! macros should not be committed to the master branch,\n\
323 fn check_trailing_ws(path: &Path, text: &str) {
324 if is_exclude_dir(path, &["test_data"]) {
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)
336 missing_docs: Vec<String>,
337 contains_fixme: Vec<PathBuf>,
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"]) {
348 if is_exclude_file(path) {
352 let first_line = match text.lines().next() {
357 if first_line.starts_with("//!") {
358 if first_line.contains("FIXME") {
359 self.contains_fixme.push(path.to_path_buf());
362 if text.contains("// Feature:") || text.contains("// Assist:") {
365 self.missing_docs.push(path.display().to_string());
368 fn is_exclude_file(d: &Path) -> bool {
369 let file_names = ["tests.rs", "famous_defs_fixture.rs"];
374 .map(|f_n| file_names.iter().any(|name| *name == f_n))
380 if !self.missing_docs.is_empty() {
382 "\nMissing docs strings\n\n\
384 self.missing_docs.join("\n")
388 let poorly_documented = [
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);
410 panic!("FIXME doc in a fully-documented crate: {}", path.display())
413 for (krate, has_fixme) in has_fixmes.iter() {
415 panic!("crate {} is fully documented :tada:, remove it from the list of poorly documented crates", krate)
421 fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
422 p.strip_prefix(project_root())
427 .filter_map(|it| it.as_os_str().to_str())
428 .any(|it| dirs_to_exclude.contains(&it))
432 fn stable_hash(text: &str) -> u64 {
433 use std::hash::{Hash, Hasher, SipHasher};
435 let text = text.replace('\r', "");
436 let mut hasher = SipHasher::default();
437 text.hash(&mut hasher);