Skip to content

Commit f9881c8

Browse files
committed
Migrate the fluent_alphabetical and fluent_used check to diagnostics
1 parent 456a2fb commit f9881c8

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/tools/tidy/src/fluent_alphabetical.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::path::Path;
77

88
use regex::Regex;
99

10+
use crate::diagnostics::{CheckId, DiagCtx, RunningCheck};
1011
use crate::walk::{filter_dirs, walk};
1112

1213
fn message() -> &'static Regex {
@@ -20,33 +21,30 @@ fn is_fluent(path: &Path) -> bool {
2021
fn check_alphabetic(
2122
filename: &str,
2223
fluent: &str,
23-
bad: &mut bool,
24+
check: &mut RunningCheck,
2425
all_defined_msgs: &mut HashMap<String, String>,
2526
) {
2627
let mut matches = message().captures_iter(fluent).peekable();
2728
while let Some(m) = matches.next() {
2829
let name = m.get(1).unwrap();
2930
if let Some(defined_filename) = all_defined_msgs.get(name.as_str()) {
30-
tidy_error!(
31-
bad,
32-
"{filename}: message `{}` is already defined in {}",
31+
check.error(format!(
32+
"{filename}: message `{}` is already defined in {defined_filename}",
3333
name.as_str(),
34-
defined_filename,
35-
);
34+
));
3635
}
3736

3837
all_defined_msgs.insert(name.as_str().to_owned(), filename.to_owned());
3938

4039
if let Some(next) = matches.peek() {
4140
let next = next.get(1).unwrap();
4241
if name.as_str() > next.as_str() {
43-
tidy_error!(
44-
bad,
42+
check.error(format!(
4543
"{filename}: message `{}` appears before `{}`, but is alphabetically later than it
4644
run `./x.py test tidy --bless` to sort the file correctly",
4745
name.as_str(),
4846
next.as_str()
49-
);
47+
));
5048
}
5149
} else {
5250
break;
@@ -57,20 +55,18 @@ run `./x.py test tidy --bless` to sort the file correctly",
5755
fn sort_messages(
5856
filename: &str,
5957
fluent: &str,
60-
bad: &mut bool,
58+
check: &mut RunningCheck,
6159
all_defined_msgs: &mut HashMap<String, String>,
6260
) -> String {
6361
let mut chunks = vec![];
6462
let mut cur = String::new();
6563
for line in fluent.lines() {
6664
if let Some(name) = message().find(line) {
6765
if let Some(defined_filename) = all_defined_msgs.get(name.as_str()) {
68-
tidy_error!(
69-
bad,
70-
"{filename}: message `{}` is already defined in {}",
66+
check.error(format!(
67+
"{filename}: message `{}` is already defined in {defined_filename}",
7168
name.as_str(),
72-
defined_filename,
73-
);
69+
));
7470
}
7571

7672
all_defined_msgs.insert(name.as_str().to_owned(), filename.to_owned());
@@ -88,7 +84,9 @@ fn sort_messages(
8884
out
8985
}
9086

91-
pub fn check(path: &Path, bless: bool, bad: &mut bool) {
87+
pub fn check(path: &Path, bless: bool, diag_ctx: DiagCtx) {
88+
let mut check = diag_ctx.start_check(CheckId::new("fluent_alphabetical").path(path));
89+
9290
let mut all_defined_msgs = HashMap::new();
9391
walk(
9492
path,
@@ -98,7 +96,7 @@ pub fn check(path: &Path, bless: bool, bad: &mut bool) {
9896
let sorted = sort_messages(
9997
ent.path().to_str().unwrap(),
10098
contents,
101-
bad,
99+
&mut check,
102100
&mut all_defined_msgs,
103101
);
104102
if sorted != contents {
@@ -110,12 +108,12 @@ pub fn check(path: &Path, bless: bool, bad: &mut bool) {
110108
check_alphabetic(
111109
ent.path().to_str().unwrap(),
112110
contents,
113-
bad,
111+
&mut check,
114112
&mut all_defined_msgs,
115113
);
116114
}
117115
},
118116
);
119117

120-
crate::fluent_used::check(path, all_defined_msgs, bad);
118+
crate::fluent_used::check(path, all_defined_msgs, diag_ctx);
121119
}

src/tools/tidy/src/fluent_used.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::collections::HashMap;
44
use std::path::Path;
55

6+
use crate::diagnostics::{CheckId, DiagCtx};
67
use crate::walk::{filter_dirs, walk};
78

89
fn filter_used_messages(
@@ -27,13 +28,15 @@ fn filter_used_messages(
2728
}
2829
}
2930

30-
pub fn check(path: &Path, mut all_defined_msgs: HashMap<String, String>, bad: &mut bool) {
31+
pub fn check(path: &Path, mut all_defined_msgs: HashMap<String, String>, diag_ctx: DiagCtx) {
32+
let mut check = diag_ctx.start_check(CheckId::new("fluent_used").path(path));
33+
3134
let mut msgs_appear_only_once = HashMap::new();
3235
walk(path, |path, _| filter_dirs(path), &mut |_, contents| {
3336
filter_used_messages(contents, &mut all_defined_msgs, &mut msgs_appear_only_once);
3437
});
3538

3639
for (name, filename) in msgs_appear_only_once {
37-
tidy_error!(bad, "{filename}: message `{}` is not used", name,);
40+
check.error(format!("{filename}: message `{name}` is not used"));
3841
}
3942
}

src/tools/tidy/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn main() {
119119

120120
// Checks that only make sense for the compiler.
121121
check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], &ci_info);
122-
// check!(fluent_alphabetical, &compiler_path, bless);
122+
check!(fluent_alphabetical, &compiler_path, bless);
123123
// check!(fluent_period, &compiler_path);
124124
// check!(fluent_lowercase, &compiler_path);
125125
// check!(target_policy, &root_path);

0 commit comments

Comments
 (0)