Skip to content

Commit a79a2c7

Browse files
committed
tidy: some minor improvements.
- Tweak some comments. - No need to do the `concat!` trick on `START_MARKER` because it's immediately followed by `END_MARKER`. - Fix an off-by-one error in the line number for an error message. - When a second start marker is found without an intervening end marker, after giving an error, treat it as though it ends the section. It's hard to know exactly what to do in this case, but it makes unit testing this case a little simpler (in the next commit). - If an end marker occurs without a preceding start marker, issue an error.
1 parent 40797ee commit a79a2c7

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Diff for: src/tools/tidy/src/alphabetical.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
//! - Lines that are indented with more or less spaces than the first line
1515
//! - Lines starting with `//`, `#` (except those starting with `#!`), `)`, `]`, `}` if the comment
1616
//! has the same indentation as the first line
17+
//! - Lines starting with a closing delimiter (`)`, `[`, `}`) are ignored.
1718
//!
18-
//! If a line ends with an opening bracket, the line is ignored and the next line will have
19-
//! its extra indentation ignored.
19+
//! If a line ends with an opening delimiter, we effectively join the following line to it before
20+
//! checking it. E.g. `foo(\nbar)` is treated like `foo(bar)`.
2021
21-
use std::{fmt::Display, path::Path};
22+
use std::fmt::Display;
23+
use std::path::Path;
2224

2325
use crate::walk::{filter_dirs, walk};
2426

@@ -30,8 +32,7 @@ fn is_close_bracket(c: char) -> bool {
3032
matches!(c, ')' | ']' | '}')
3133
}
3234

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

3738
fn check_section<'a>(
@@ -43,13 +44,14 @@ fn check_section<'a>(
4344
let mut first_indent = None;
4445
let mut in_split_line = None;
4546

46-
for (line_idx, line) in lines {
47+
for (idx, line) in lines {
4748
if line.is_empty() {
4849
continue;
4950
}
5051

5152
if line.contains(START_MARKER) {
52-
tidy_error!(bad, "{file}:{} found `{START_MARKER}` expecting `{END_MARKER}`", line_idx)
53+
tidy_error!(bad, "{file}:{} found `{START_MARKER}` expecting `{END_MARKER}`", idx + 1);
54+
return;
5355
}
5456

5557
if line.contains(END_MARKER) {
@@ -63,6 +65,7 @@ fn check_section<'a>(
6365
});
6466

6567
let line = if let Some(prev_split_line) = in_split_line {
68+
// Join the split lines.
6669
in_split_line = None;
6770
format!("{prev_split_line}{}", line.trim_start())
6871
} else {
@@ -90,7 +93,7 @@ fn check_section<'a>(
9093
let prev_line_trimmed_lowercase = prev_line.trim_start_matches(' ').to_lowercase();
9194

9295
if trimmed_line.to_lowercase() < prev_line_trimmed_lowercase {
93-
tidy_error!(bad, "{file}:{}: line not in alphabetical order", line_idx + 1,);
96+
tidy_error!(bad, "{file}:{}: line not in alphabetical order", idx + 1);
9497
}
9598

9699
prev_line = line;
@@ -104,7 +107,15 @@ pub fn check(path: &Path, bad: &mut bool) {
104107
let file = &entry.path().display();
105108

106109
let mut lines = contents.lines().enumerate();
107-
while let Some((_, line)) = lines.next() {
110+
while let Some((idx, line)) = lines.next() {
111+
if line.contains(END_MARKER) {
112+
tidy_error!(
113+
bad,
114+
"{file}:{} found `{END_MARKER}` expecting `{START_MARKER}`",
115+
idx + 1
116+
)
117+
}
118+
108119
if line.contains(START_MARKER) {
109120
check_section(file, &mut lines, bad);
110121
}

0 commit comments

Comments
 (0)