Skip to content

Commit 6915039

Browse files
Extend rustdoc template check to detect unneeded comments
1 parent 2bd1e89 commit 6915039

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/tools/tidy/src/rustdoc_templates.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,39 @@ pub fn check(librustdoc_path: &Path, bad: &mut bool) {
2020

2121
while let Some((pos, line)) = lines.next() {
2222
let line = line.trim();
23-
if TAGS.iter().any(|(_, tag)| line.ends_with(tag)) {
23+
if let Some(need_next_line_check) = TAGS.iter().find_map(|(tag, end_tag)| {
24+
// We first check if the line ends with a jinja tag.
25+
if !line.ends_with(end_tag) {
26+
return None;
27+
// Then we check if this a comment tag.
28+
} else if *tag != "{#" {
29+
return Some(false);
30+
// And finally we check if the comment is empty (ie, only there to strip
31+
// extra whitespace characters).
32+
} else if let Some(start_pos) = line.rfind(tag) {
33+
Some(line[start_pos + 2..].trim() == "#}")
34+
} else {
35+
Some(false)
36+
}
37+
}) {
38+
// All good, the line is ending is a jinja tag. But maybe this tag is useless
39+
// if the next line starts with a jinja tag as well!
40+
//
41+
// However, only (empty) comment jinja tags are concerned about it.
42+
if need_next_line_check
43+
&& lines.peek().is_some_and(|(_, next_line)| {
44+
let next_line = next_line.trim_start();
45+
TAGS.iter().any(|(tag, _)| next_line.starts_with(tag))
46+
})
47+
{
48+
// It seems like ending this line with a jinja tag is not needed after all.
49+
tidy_error!(
50+
bad,
51+
"`{}` at line {}: unneeded `{{# #}}` tag at the end of the line",
52+
path.path().display(),
53+
pos + 1,
54+
);
55+
}
2456
continue;
2557
}
2658
let Some(next_line) = lines.peek().map(|(_, next_line)| next_line.trim()) else {

0 commit comments

Comments
 (0)