6 use xshell::{cmd, read_file};
9 project_root, run_rustfmt, rust_files,
13 fn generated_grammar_is_fresh() {
14 if let Err(error) = codegen::generate_syntax(Mode::Verify) {
15 panic!("{}. Please update it by running `cargo xtask codegen`", error);
20 fn generated_tests_are_fresh() {
21 if let Err(error) = codegen::generate_parser_tests(Mode::Verify) {
22 panic!("{}. Please update tests by running `cargo xtask codegen`", error);
27 fn generated_assists_are_fresh() {
28 if let Err(error) = codegen::generate_assists_tests(Mode::Verify) {
29 panic!("{}. Please update assists by running `cargo xtask codegen`", error);
34 fn check_code_formatting() {
35 if let Err(error) = run_rustfmt(Mode::Verify) {
36 panic!("{}. Please format the code by running `cargo format`", error);
41 fn smoke_test_docs_generation() {
42 // We don't commit docs to the repo, so we can just overwrite in tests.
43 codegen::generate_assists_docs(Mode::Overwrite).unwrap();
44 codegen::generate_feature_docs(Mode::Overwrite).unwrap();
45 codegen::generate_diagnostic_docs(Mode::Overwrite).unwrap();
49 fn check_lsp_extensions_docs() {
52 read_file(project_root().join("crates/rust-analyzer/src/lsp_ext.rs")).unwrap();
53 stable_hash(lsp_ext_rs.as_str())
57 let lsp_extensions_md =
58 read_file(project_root().join("docs/dev/lsp-extensions.md")).unwrap();
59 let text = lsp_extensions_md
61 .find_map(|line| line.strip_prefix("lsp_ext.rs hash:"))
64 u64::from_str_radix(text, 16).unwrap()
67 if actual_hash != expected_hash {
70 lsp_ext.rs was changed without touching lsp-extensions.md.
75 Please adjust docs/dev/lsp-extensions.md.
77 expected_hash, actual_hash
83 fn rust_files_are_tidy() {
84 let mut tidy_docs = TidyDocs::default();
85 for path in rust_files() {
86 let text = read_file(&path).unwrap();
87 check_todo(&path, &text);
88 check_dbg(&path, &text);
89 check_trailing_ws(&path, &text);
90 deny_clippy(&path, &text);
91 tidy_docs.visit(&path, &text);
97 fn check_merge_commits() {
98 let stdout = cmd!("git rev-list --merges --invert-grep --author 'bors\\[bot\\]' HEAD~19..")
101 if !stdout.is_empty() {
104 Merge commits are not allowed in the history.
106 When updating a pull-request, please rebase your feature branch
107 on top of master by running `git rebase master`. If rebase fails,
108 you can re-apply your changes like this:
110 # Just look around to see the current state.
114 # Abort in-progress rebase and merges, if any.
118 # Make the branch point to the latest commit from master,
119 # while maintaining your local changes uncommited.
120 $ git reset --soft origin/master
122 # Commit all changes in a single batch.
123 $ git commit -am'My changes'
125 # Verify that everything looks alright.
129 # Push the changes. We did a rebase, so we need `--force` option.
130 # `--force-with-lease` is a more safe (Rusty) version of `--force`.
131 $ git push --force-with-lease
133 # Verify that both local and remote branch point to the same commit.
136 And don't fear to mess something up during a rebase -- you can
137 always restore the previous state using `git ref-log`:
139 https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/#redo-after-undo-local
145 fn deny_clippy(path: &PathBuf, text: &String) {
147 // The documentation in string literals may contain anything for its own purposes
148 "completion/src/generated_lint_completions.rs",
150 if ignore.iter().any(|p| path.ends_with(p)) {
154 if text.contains("\u{61}llow(clippy") {
156 "\n\nallowing lints is forbidden: {}.
157 rust-analyzer intentionally doesn't check clippy on CI.
158 You can allow lint globally via `xtask clippy`.
159 See https://github.com/rust-lang/rust-clippy/issues/5537 for discussion.
168 fn check_licenses() {
170 0BSD OR MIT OR Apache-2.0
172 Apache-2.0 OR BSL-1.0
174 Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT
182 MIT OR Apache-2.0 OR Zlib
183 MIT OR Zlib OR Apache-2.0
187 Zlib OR Apache-2.0 OR MIT
190 .filter(|it| !it.is_empty())
191 .collect::<Vec<_>>();
193 let meta = cmd!("cargo metadata --format-version 1").read().unwrap();
194 let mut licenses = meta
195 .split(|c| c == ',' || c == '{' || c == '}')
196 .filter(|it| it.contains(r#""license""#))
198 .map(|it| it[r#""license":"#.len()..].trim_matches('"'))
199 .collect::<Vec<_>>();
202 if licenses != expected {
203 let mut diff = String::new();
205 diff += &format!("New Licenses:\n");
206 for &l in licenses.iter() {
207 if !expected.contains(&l) {
208 diff += &format!(" {}\n", l)
212 diff += &format!("\nMissing Licenses:\n");
213 for &l in expected.iter() {
214 if !licenses.contains(&l) {
215 diff += &format!(" {}\n", l)
219 panic!("different set of licenses!\n{}", diff);
221 assert_eq!(licenses, expected);
224 fn check_todo(path: &Path, text: &str) {
226 // This file itself obviously needs to use todo (<- like this!).
228 // Some of our assists generate `todo!()`.
229 "handlers/add_turbo_fish.rs",
230 "handlers/generate_function.rs",
231 // To support generating `todo!()` in assists, we have `expr_todo()` in
234 // The documentation in string literals may contain anything for its own purposes
235 "completion/src/generated_lint_completions.rs",
237 if need_todo.iter().any(|p| path.ends_with(p)) {
240 if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") {
241 // Generated by an assist
242 if text.contains("${0:todo!()}") {
247 "\nTODO markers or todo! macros should not be committed to the master branch,\n\
255 fn check_dbg(path: &Path, text: &str) {
257 // This file itself obviously needs to use dbg.
259 // Assists to remove `dbg!()`
260 "handlers/remove_dbg.rs",
261 // We have .dbg postfix
262 "completion/src/completions/postfix.rs",
263 // The documentation in string literals may contain anything for its own purposes
264 "completion/src/lib.rs",
265 "completion/src/generated_lint_completions.rs",
266 // test for doc test for remove_dbg
267 "src/tests/generated.rs",
269 if need_dbg.iter().any(|p| path.ends_with(p)) {
272 if text.contains("dbg!") {
274 "\ndbg! macros should not be committed to the master branch,\n\
281 fn check_trailing_ws(path: &Path, text: &str) {
282 if is_exclude_dir(path, &["test_data"]) {
285 for (line_number, line) in text.lines().enumerate() {
286 if line.chars().last().map(char::is_whitespace) == Some(true) {
287 panic!("Trailing whitespace in {} at line {}", path.display(), line_number)
294 missing_docs: Vec<String>,
295 contains_fixme: Vec<PathBuf>,
299 fn visit(&mut self, path: &Path, text: &str) {
300 // Test hopefully don't really need comments, and for assists we already
301 // have special comments which are source of doc tests and user docs.
302 if is_exclude_dir(path, &["tests", "test_data"]) {
306 if is_exclude_file(path) {
310 let first_line = match text.lines().next() {
315 if first_line.starts_with("//!") {
316 if first_line.contains("FIXME") {
317 self.contains_fixme.push(path.to_path_buf());
320 if text.contains("// Feature:") || text.contains("// Assist:") {
323 self.missing_docs.push(path.display().to_string());
326 fn is_exclude_file(d: &Path) -> bool {
327 let file_names = ["tests.rs", "famous_defs_fixture.rs"];
332 .map(|f_n| file_names.iter().any(|name| *name == f_n))
338 if !self.missing_docs.is_empty() {
340 "\nMissing docs strings\n\n\
342 self.missing_docs.join("\n")
346 let poorly_documented = [
360 poorly_documented.iter().map(|it| (*it, false)).collect::<HashMap<&str, bool>>();
361 'outer: for path in self.contains_fixme {
362 for krate in poorly_documented.iter() {
363 if path.components().any(|it| it.as_os_str() == *krate) {
364 has_fixmes.insert(krate, true);
368 panic!("FIXME doc in a fully-documented crate: {}", path.display())
371 for (krate, has_fixme) in has_fixmes.iter() {
373 panic!("crate {} is fully documented :tada:, remove it from the list of poorly documented crates", krate)
379 fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool {
380 p.strip_prefix(project_root())
385 .filter_map(|it| it.as_os_str().to_str())
386 .any(|it| dirs_to_exclude.contains(&it))
390 fn stable_hash(text: &str) -> u64 {
391 use std::hash::{Hash, Hasher, SipHasher};
393 let text = text.replace('\r', "");
394 let mut hasher = SipHasher::default();
395 text.hash(&mut hasher);