about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume.gomez@huawei.com>2024-09-22 17:06:00 +0200
committerGuillaume Gomez <guillaume.gomez@huawei.com>2024-09-27 00:53:04 +0200
commit69150396bf145ab19488263f2a111ef48338dced (patch)
treefe7acfb423f176d2b7e276c250f90715ea341f36
parent2bd1e894efde3b6be857ad345914a3b1cea51def (diff)
downloadrust-69150396bf145ab19488263f2a111ef48338dced.tar.gz
rust-69150396bf145ab19488263f2a111ef48338dced.zip
Extend rustdoc template check to detect unneeded comments
-rw-r--r--src/tools/tidy/src/rustdoc_templates.rs34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/tools/tidy/src/rustdoc_templates.rs b/src/tools/tidy/src/rustdoc_templates.rs
index 6c8530e6366..2173dbf7e74 100644
--- a/src/tools/tidy/src/rustdoc_templates.rs
+++ b/src/tools/tidy/src/rustdoc_templates.rs
@@ -20,7 +20,39 @@ pub fn check(librustdoc_path: &Path, bad: &mut bool) {
 
             while let Some((pos, line)) = lines.next() {
                 let line = line.trim();
-                if TAGS.iter().any(|(_, tag)| line.ends_with(tag)) {
+                if let Some(need_next_line_check) = TAGS.iter().find_map(|(tag, end_tag)| {
+                    // We first check if the line ends with a jinja tag.
+                    if !line.ends_with(end_tag) {
+                        return None;
+                    // Then we check if this a comment tag.
+                    } else if *tag != "{#" {
+                        return Some(false);
+                    // And finally we check if the comment is empty (ie, only there to strip
+                    // extra whitespace characters).
+                    } else if let Some(start_pos) = line.rfind(tag) {
+                        Some(line[start_pos + 2..].trim() == "#}")
+                    } else {
+                        Some(false)
+                    }
+                }) {
+                    // All good, the line is ending is a jinja tag. But maybe this tag is useless
+                    // if the next line starts with a jinja tag as well!
+                    //
+                    // However, only (empty) comment jinja tags are concerned about it.
+                    if need_next_line_check
+                        && lines.peek().is_some_and(|(_, next_line)| {
+                            let next_line = next_line.trim_start();
+                            TAGS.iter().any(|(tag, _)| next_line.starts_with(tag))
+                        })
+                    {
+                        // It seems like ending this line with a jinja tag is not needed after all.
+                        tidy_error!(
+                            bad,
+                            "`{}` at line {}: unneeded `{{# #}}` tag at the end of the line",
+                            path.path().display(),
+                            pos + 1,
+                        );
+                    }
                     continue;
                 }
                 let Some(next_line) = lines.peek().map(|(_, next_line)| next_line.trim()) else {