Skip to content

Commit 4d08e6e

Browse files
committed
tidy: check that error messages don't start with a capitalized letter
1 parent 033c0a4 commit 4d08e6e

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ codegen_ssa_cpu_required = target requires explicitly specifying a cpu with `-C
3131
codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}
3232
3333
codegen_ssa_dlltool_fail_import_library =
34-
Dlltool could not create import library with {$dlltool_path} {$dlltool_args}:
34+
dlltool could not create import library with {$dlltool_path} {$dlltool_args}:
3535
{$stdout}
3636
{$stderr}
3737

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ passes_const_stable_not_stable =
7575
.label = attribute specified here
7676
7777
passes_custom_mir_incompatible_dialect_and_phase =
78-
The {$dialect} dialect is not compatible with the {$phase} phase
78+
the {$dialect} dialect is not compatible with the {$phase} phase
7979
.dialect_span = this dialect...
8080
.phase_span = ... is not compatible with this phase
8181
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! Checks that the error messages start with a lowercased letter (except when allowed to).
2+
3+
use std::path::Path;
4+
5+
use fluent_syntax::ast::{Entry, Message, PatternElement};
6+
7+
use crate::walk::{filter_dirs, walk};
8+
9+
#[rustfmt::skip]
10+
const ALLOWED_CAPITALIZED_WORDS: &[&str] = &[
11+
// tidy-alphabetical-start
12+
"ABI",
13+
"ABIs",
14+
"ADT",
15+
"C",
16+
"CGU",
17+
"Ferris",
18+
"MIR",
19+
"OK",
20+
"Rust",
21+
"VS", // VS Code
22+
// tidy-alphabetical-end
23+
];
24+
25+
fn filter_fluent(path: &Path) -> bool {
26+
if let Some(ext) = path.extension() { ext.to_str() != Some("ftl") } else { true }
27+
}
28+
29+
fn is_allowed_capitalized_word(msg: &str) -> bool {
30+
ALLOWED_CAPITALIZED_WORDS.iter().any(|word| {
31+
msg.strip_prefix(word)
32+
.map(|tail| tail.chars().next().map(|c| c == '-' || c.is_whitespace()).unwrap_or(true))
33+
.unwrap_or_default()
34+
})
35+
}
36+
37+
fn check_lowercase(filename: &str, contents: &str, bad: &mut bool) {
38+
let (Ok(parse) | Err((parse, _))) = fluent_syntax::parser::parse(contents);
39+
40+
for entry in &parse.body {
41+
if let Entry::Message(msg) = entry
42+
&& let Message { value: Some(pattern), .. } = msg
43+
&& let [first_pattern, ..] = &pattern.elements[..]
44+
&& let PatternElement::TextElement { value } = first_pattern
45+
&& value.chars().next().map(char::is_uppercase).unwrap_or_default()
46+
&& !is_allowed_capitalized_word(value)
47+
{
48+
let value = value.trim().replace('`', "\\`");
49+
tidy_error!(
50+
bad,
51+
"{filename}: message `{value}` starts with an uppercase letter. Fix it or add it to `ALLOWED_CAPITALIZED_WORDS`"
52+
);
53+
}
54+
}
55+
}
56+
57+
pub fn check(path: &Path, bad: &mut bool) {
58+
walk(
59+
path,
60+
|path, is_dir| filter_dirs(path) || (!is_dir && filter_fluent(path)),
61+
&mut |ent, contents| {
62+
check_lowercase(ent.path().to_str().unwrap(), contents, bad);
63+
},
64+
);
65+
}

src/tools/tidy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub mod extra_checks;
257257
pub mod features;
258258
pub mod filenames;
259259
pub mod fluent_alphabetical;
260+
pub mod fluent_lowercase;
260261
pub mod fluent_period;
261262
mod fluent_used;
262263
pub mod gcc_submodule;

src/tools/tidy/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ fn main() {
121121
check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose, &ci_info);
122122
check!(fluent_alphabetical, &compiler_path, bless);
123123
check!(fluent_period, &compiler_path);
124+
check!(fluent_lowercase, &compiler_path);
124125
check!(target_policy, &root_path);
125126
check!(gcc_submodule, &root_path, &compiler_path);
126127

tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ pub fn lib_main() {
2020
unsafe { f(42); }
2121
}
2222

23-
//~? ERROR Dlltool could not create import library with
23+
//~? ERROR dlltool could not create import library with

tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Dlltool could not create import library with $DLLTOOL -d $DEF_FILE -D foo.dll -l $LIB_FILE $TARGET_MACHINE $ASM_FLAGS --no-leading-underscore $TEMP_PREFIX:
1+
error: dlltool could not create import library with $DLLTOOL -d $DEF_FILE -D foo.dll -l $LIB_FILE $TARGET_MACHINE $ASM_FLAGS --no-leading-underscore $TEMP_PREFIX:
22

33
$DLLTOOL: Syntax error in def file $DEF_FILE:1␍
44

0 commit comments

Comments
 (0)