Skip to content

Commit 25cf1ef

Browse files
committed
add check_unused_messages in tidy
1 parent ca2b74f commit 25cf1ef

File tree

10 files changed

+55
-60
lines changed

10 files changed

+55
-60
lines changed

compiler/rustc_hir_analysis/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ hir_analysis_enum_discriminant_overflowed = enum discriminant overflowed
7272
.label = overflowed on value after {$discr}
7373
.note = explicitly set `{$item_name} = {$wrapped_discr}` if that is desired outcome
7474
75-
hir_analysis_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
76-
7775
hir_analysis_field_already_declared =
7876
field `{$field_name}` is already declared
7977
.label = field already declared

compiler/rustc_infer/messages.ftl

-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ infer_await_both_futures = consider `await`ing on both `Future`s
6666
infer_await_future = consider `await`ing on the `Future`
6767
infer_await_note = calling an async function returns a future
6868
69-
infer_borrowed_too_long = a value of type `{$ty}` is borrowed for too long
7069
infer_but_calling_introduces = {$has_param_name ->
7170
[true] `{$param_name}`
7271
*[false] `fn` parameter
@@ -181,8 +180,6 @@ infer_more_targeted = {$has_param_name ->
181180
} but calling `{$ident}` introduces an implicit `'static` lifetime requirement
182181
183182
infer_msl_introduces_static = introduces a `'static` lifetime requirement
184-
infer_msl_trait_note = this has an implicit `'static` lifetime requirement
185-
infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement
186183
infer_msl_unmet_req = because this has an unmet lifetime requirement
187184
infer_need_type_info_in_generator =
188185
type inside {$generator_kind ->
@@ -229,7 +226,6 @@ infer_prlf_known_limitation = this is a known limitation that will be removed in
229226
infer_prlf_must_outlive_with_sup = ...must outlive the lifetime `{$sup_symbol}` defined here
230227
infer_prlf_must_outlive_without_sup = ...must outlive the lifetime defined here
231228
infer_reborrow = ...so that reference does not outlive borrowed content
232-
infer_reborrow_upvar = ...so that closure can access `{$name}`
233229
infer_ref_longer_than_data = in type `{$ty}`, reference has a longer lifetime than the data it references
234230
235231
infer_reference_outlives_referent = ...so that the reference type `{$name}` does not outlive the data it points at

compiler/rustc_monomorphize/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ monomorphize_couldnt_dump_mono_stats =
77
monomorphize_encountered_error_while_instantiating =
88
the above error was encountered while instantiating `{$formatted_item}`
99
10-
monomorphize_fatal_error = {$error_message}
11-
1210
monomorphize_large_assignments =
1311
moving {$size} bytes
1412
.label = value moved from here

compiler/rustc_parse/messages.ftl

-30
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ parse_bare_cr = {$double_quotes ->
5959
6060
parse_bare_cr_in_raw_string = bare CR not allowed in raw string
6161
62-
parse_binary_float_literal_not_supported = binary float literal is not supported
6362
parse_bounds_not_allowed_on_trait_aliases = bounds are not allowed on trait aliases
6463
6564
parse_box_not_pat = expected pattern, found {$descr}
@@ -284,7 +283,6 @@ parse_generics_in_path = unexpected generic arguments in path
284283
285284
parse_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
286285
parse_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
287-
parse_hexadecimal_float_literal_not_supported = hexadecimal float literal is not supported
288286
parse_if_expression_missing_condition = missing condition for `if` expression
289287
.condition_label = expected condition here
290288
.block_label = if this block is the condition of the `if` expression, then it must be followed by another block
@@ -356,8 +354,6 @@ parse_inner_doc_comment_not_permitted = expected outer doc comment
356354
.label_does_not_annotate_this = the inner doc comment doesn't annotate this {$item}
357355
.sugg_change_inner_to_outer = to annotate the {$item}, change the doc comment from inner to outer style
358356
359-
parse_int_literal_too_large = integer literal is too large
360-
361357
parse_invalid_block_macro_segment = cannot use a `block` macro fragment here
362358
.label = the `block` fragment is within this context
363359
.suggestion = wrap this in another block
@@ -382,18 +378,8 @@ parse_invalid_dyn_keyword = invalid `dyn` keyword
382378
.suggestion = remove this keyword
383379
384380
parse_invalid_expression_in_let_else = a `{$operator}` expression cannot be directly assigned in `let...else`
385-
parse_invalid_float_literal_suffix = invalid suffix `{$suffix}` for float literal
386-
.label = invalid suffix `{$suffix}`
387-
.help = valid suffixes are `f32` and `f64`
388-
389-
parse_invalid_float_literal_width = invalid width `{$width}` for float literal
390-
.help = valid widths are 32 and 64
391-
392381
parse_invalid_identifier_with_leading_number = identifiers cannot start with a number
393382
394-
parse_invalid_int_literal_width = invalid width `{$width}` for integer literal
395-
.help = valid widths are 8, 16, 32, 64 and 128
396-
397383
parse_invalid_interpolated_expression = invalid interpolated expression
398384
399385
parse_invalid_literal_suffix = suffixes on {$kind} literals are invalid
@@ -412,14 +398,6 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
412398
413399
parse_invalid_meta_item = expected unsuffixed literal or identifier, found `{$token}`
414400
415-
parse_invalid_num_literal_base_prefix = invalid base prefix for number literal
416-
.note = base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
417-
.suggestion = try making the prefix lowercase
418-
419-
parse_invalid_num_literal_suffix = invalid suffix `{$suffix}` for number literal
420-
.label = invalid suffix `{$suffix}`
421-
.help = the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)
422-
423401
parse_invalid_unicode_escape = invalid unicode character escape
424402
.label = invalid escape
425403
.help = unicode escape must {$surrogate ->
@@ -603,13 +581,6 @@ parse_no_brace_unicode_escape = incorrect unicode escape sequence
603581
604582
parse_no_digits_literal = no valid digits found for number
605583
606-
parse_non_item_in_item_list = non-item in item list
607-
.suggestion_use_const_not_let = consider using `const` instead of `let` for associated const
608-
.label_list_start = item list starts here
609-
.label_non_item = non-item starts here
610-
.label_list_end = item list ends here
611-
.suggestion_remove_semicolon = consider removing this semicolon
612-
613584
parse_non_string_abi_literal = non-string ABI literal
614585
.suggestion = specify the ABI with a string literal
615586
@@ -626,7 +597,6 @@ parse_note_mut_pattern_usage = `mut` may be followed by `variable` and `variable
626597
627598
parse_note_pattern_alternatives_use_single_vert = alternatives in or-patterns are separated with `|`, not `||`
628599
629-
parse_octal_float_literal_not_supported = octal float literal is not supported
630600
parse_or_pattern_not_allowed_in_fn_parameters = top-level or-patterns are not allowed in function parameters
631601
parse_or_pattern_not_allowed_in_let_binding = top-level or-patterns are not allowed in `let` bindings
632602
parse_out_of_range_hex_escape = out of range hex escape

compiler/rustc_query_system/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ query_system_cycle_stack_single = ...which immediately requires {$stack_bottom}
1515
1616
query_system_cycle_usage = cycle used when {$usage}
1717
18-
query_system_cycle_which_requires = ...which requires {$desc}...
19-
2018
query_system_increment_compilation = internal compiler error: encountered incremental compilation error with {$dep_node}
2119
.help = This is a known issue with the compiler. Run {$run_cmd} to allow your project to compile
2220

compiler/rustc_resolve/messages.ftl

-12
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ resolve_generic_params_from_outer_item_ty_param = type parameter from outer item
130130
resolve_glob_import_doesnt_reexport =
131131
glob import doesn't reexport anything because no candidate is public enough
132132
133-
resolve_help_try_using_local_generic_param =
134-
try using a local generic parameter instead
135-
136133
resolve_ident_bound_more_than_once_in_parameter_list =
137134
identifier `{$identifier}` is bound more than once in this parameter list
138135
.label = used as parameter more than once
@@ -249,9 +246,6 @@ resolve_self_in_generic_param_default =
249246
generic parameters cannot use `Self` in their defaults
250247
.label = `Self` in generic parameter default
251248
252-
resolve_self_type_implicitly_declared_by_impl =
253-
`Self` type implicitly declared here, by this `impl`
254-
255249
resolve_tool_module_imported =
256250
cannot use a tool module through an import
257251
.note = the tool module imported here
@@ -267,12 +261,6 @@ resolve_trait_impl_mismatch =
267261
.label = does not match trait
268262
.label_trait_item = item in trait
269263
270-
resolve_try_adding_local_generic_param_on_method =
271-
try adding a local generic parameter in this method instead
272-
273-
resolve_try_using_local_generic_parameter =
274-
try using a local generic parameter instead
275-
276264
resolve_try_using_similarly_named_label =
277265
try using similarly named label
278266

compiler/rustc_session/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ session_feature_diagnostic_for_issue =
2525
session_feature_diagnostic_help =
2626
add `#![feature({$feature})]` to the crate attributes to enable
2727
28-
session_feature_gate_error = {$explain}
29-
3028
session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions
3129
3230
session_file_write_fail = failed to write `{$path}` due to error `{$err}`

src/tools/tidy/src/fluent_alphabetical.rs renamed to src/tools/tidy/src/fluent_checks.rs

+53-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Checks that all Flunt files have messages in alphabetical order
2+
//! Checks that all messages are referenced in the code
23
34
use crate::walk::{filter_dirs, walk};
45
use std::{fs::OpenOptions, io::Write, path::Path};
@@ -13,6 +14,10 @@ fn filter_fluent(path: &Path) -> bool {
1314
if let Some(ext) = path.extension() { ext.to_str() != Some("ftl") } else { true }
1415
}
1516

17+
fn filter_not_rust(path: &Path) -> bool {
18+
path.extension() != Some(std::ffi::OsStr::new("rs")) && !path.is_dir()
19+
}
20+
1621
fn check_alphabetic(filename: &str, fluent: &str, bad: &mut bool) {
1722
let mut matches = MESSAGE.captures_iter(fluent).peekable();
1823
while let Some(m) = matches.next() {
@@ -34,16 +39,58 @@ run `./x.py test tidy --bless` to sort the file correctly",
3439
}
3540
}
3641

37-
fn sort_messages(fluent: &str) -> String {
42+
fn check_unused_messages(
43+
filename: &str,
44+
fluent: &str,
45+
bad: &mut bool,
46+
report: bool,
47+
) -> Vec<String> {
48+
let mut matches = MESSAGE.captures_iter(fluent).peekable();
49+
let mut sources: Vec<String> = vec![];
50+
if matches.peek().is_some() {
51+
let src_dir = Path::new(filename).parent().unwrap().join("src");
52+
walk(
53+
&src_dir,
54+
|path, is_dir| filter_dirs(path) || (!is_dir && filter_not_rust(path)),
55+
&mut |_ent, contents| {
56+
sources.push(contents.to_string());
57+
},
58+
);
59+
}
60+
let mut unused_messages = vec![];
61+
while let Some(m) = matches.next() {
62+
let name = m.get(1).unwrap();
63+
// message maybe referenced as {name} in fluent files
64+
let name_var = "{".to_string() + name.as_str() + "}";
65+
if !sources.iter().any(|file| file.contains(name.as_str())) && !fluent.contains(&name_var) {
66+
unused_messages.push(name.as_str().to_owned());
67+
if report {
68+
tidy_error!(
69+
bad,
70+
"{filename}: message `{}` is not referenced in the code",
71+
name.as_str()
72+
);
73+
}
74+
}
75+
}
76+
unused_messages
77+
}
78+
79+
fn sort_messages(filename: &str, fluent: &str) -> String {
3880
let mut chunks = vec![];
3981
let mut cur = String::new();
82+
let unused_messages = check_unused_messages(filename, fluent, &mut false, false);
4083
for line in fluent.lines() {
4184
if MESSAGE.is_match(line) {
42-
chunks.push(std::mem::take(&mut cur));
85+
let chunk = std::mem::take(&mut cur);
86+
if !unused_messages.iter().any(|key| chunk.starts_with(key)) {
87+
chunks.push(chunk);
88+
}
4389
}
4490
cur += line;
4591
cur.push('\n');
4692
}
93+
4794
chunks.push(cur);
4895
chunks.sort();
4996
let mut out = chunks.join("");
@@ -57,15 +104,17 @@ pub fn check(path: &Path, bless: bool, bad: &mut bool) {
57104
path,
58105
|path, is_dir| filter_dirs(path) || (!is_dir && filter_fluent(path)),
59106
&mut |ent, contents| {
107+
let filename = ent.path().to_str().unwrap();
60108
if bless {
61-
let sorted = sort_messages(contents);
109+
let sorted = sort_messages(filename, contents);
62110
if sorted != contents {
63111
let mut f =
64112
OpenOptions::new().write(true).truncate(true).open(ent.path()).unwrap();
65113
f.write(sorted.as_bytes()).unwrap();
66114
}
67115
} else {
68-
check_alphabetic(ent.path().to_str().unwrap(), contents, bad);
116+
check_alphabetic(filename, contents, bad);
117+
check_unused_messages(filename, contents, bad, true);
69118
}
70119
},
71120
);

src/tools/tidy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub mod error_codes;
6060
pub mod ext_tool_checks;
6161
pub mod extdeps;
6262
pub mod features;
63-
pub mod fluent_alphabetical;
63+
pub mod fluent_checks;
6464
pub mod mir_opt_tests;
6565
pub mod pal;
6666
pub mod primitive_docs;

src/tools/tidy/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn main() {
107107

108108
// Checks that only make sense for the compiler.
109109
check!(error_codes, &root_path, &[&compiler_path, &librustdoc_path], verbose);
110-
check!(fluent_alphabetical, &compiler_path, bless);
110+
check!(fluent_checks, &compiler_path, bless);
111111

112112
// Checks that only make sense for the std libs.
113113
check!(pal, &library_path);

0 commit comments

Comments
 (0)