Skip to content

Commit b7bea6e

Browse files
committed
Fix a bug in tidy's alphabetical checking.
Currently, if a `tidy-alphabetical-end` marker appears on the last line of a file, tidy will erroneously issue a "reach end of file expecting `tidy-alphabetical-end`" error. This is because it uses `take_while` within `check_section`, which consumes the line with the end marker, and then after `check_section` returns `check` peeks for at least one more line, which won't be there is the marker was on the last line. This commit fixes the problem, by removing the use of `take_while`, and doing the "reached end of file" test within `check_section` without using `peek`. It also renames `{START,END}_COMMENT` as `{START,END}_MARKER`, which is a more appropriate name.
1 parent 6aeac60 commit b7bea6e

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/tools/tidy/src/alphabetical.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Checks that a list of items is in alphabetical order
22
//!
3-
//! To use, use the following annotation in the code:
3+
//! Use the following marker in the code:
44
//! ```rust
55
//! // tidy-alphabetical-start
66
//! fn aaa() {}
@@ -30,27 +30,25 @@ fn is_close_bracket(c: char) -> bool {
3030
}
3131

3232
// Don't let tidy check this here :D
33-
const START_COMMENT: &str = concat!("tidy-alphabetical", "-start");
34-
const END_COMMENT: &str = "tidy-alphabetical-end";
33+
const START_MARKER: &str = concat!("tidy-alphabetical", "-start");
34+
const END_MARKER: &str = "tidy-alphabetical-end";
3535

3636
fn check_section<'a>(
3737
file: impl Display,
3838
lines: impl Iterator<Item = (usize, &'a str)>,
3939
bad: &mut bool,
4040
) {
41-
let content_lines = lines.take_while(|(_, line)| !line.contains(END_COMMENT));
42-
4341
let mut prev_line = String::new();
4442
let mut first_indent = None;
4543
let mut in_split_line = None;
4644

47-
for (line_idx, line) in content_lines {
48-
if line.contains(START_COMMENT) {
49-
tidy_error!(
50-
bad,
51-
"{file}:{} found `{START_COMMENT}` expecting `{END_COMMENT}`",
52-
line_idx
53-
)
45+
for (line_idx, line) in lines {
46+
if line.contains(START_MARKER) {
47+
tidy_error!(bad, "{file}:{} found `{START_MARKER}` expecting `{END_MARKER}`", line_idx)
48+
}
49+
50+
if line.contains(END_MARKER) {
51+
return;
5452
}
5553

5654
let indent = first_indent.unwrap_or_else(|| {
@@ -92,19 +90,18 @@ fn check_section<'a>(
9290

9391
prev_line = line;
9492
}
93+
94+
tidy_error!(bad, "{file}: reached end of file expecting `{END_MARKER}`")
9595
}
9696

9797
pub fn check(path: &Path, bad: &mut bool) {
9898
walk(path, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
9999
let file = &entry.path().display();
100100

101-
let mut lines = contents.lines().enumerate().peekable();
101+
let mut lines = contents.lines().enumerate();
102102
while let Some((_, line)) = lines.next() {
103-
if line.contains(START_COMMENT) {
103+
if line.contains(START_MARKER) {
104104
check_section(file, &mut lines, bad);
105-
if lines.peek().is_none() {
106-
tidy_error!(bad, "{file}: reached end of file expecting `{END_COMMENT}`")
107-
}
108105
}
109106
}
110107
});

0 commit comments

Comments
 (0)