forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#121860 - mu001999:master, r=Nilstrieb
Add a tidy check that checks whether the fluent slugs only appear once As `````@Nilstrieb````` said in rust-lang#121828 (comment): > Might make sense to have a tidy check that checks whether the fluent slugs only appear once in the source code and lint for that there's a tidy check already for sorting We can get the tidy check error: ``` tidy check tidy error: /path/to/rust/compiler/rustc_const_eval/messages.ftl: message `const_eval_invalid_align` is not used tidy error: /path/to/rust/compiler/rustc_lint/messages.ftl: message `lint_trivial_untranslatable_diag` is not used tidy error: /path/to/rust/compiler/rustc_parse/messages.ftl: message `parse_invalid_literal_suffix` is not used tidy error: /path/to/rust/compiler/rustc_infer/messages.ftl: message `infer_need_type_info_in_coroutine` is not used tidy error: /path/to/rust/compiler/rustc_passes/messages.ftl: message `passes_expr_not_allowed_in_context` is not used tidy error: /path/to/rust/compiler/rustc_passes/messages.ftl: message `passes_layout` is not used tidy error: /path/to/rust/compiler/rustc_parse/messages.ftl: message `parse_not_supported` is not used ``` r? `````@Nilstrieb`````
- Loading branch information
Showing
8 changed files
with
96 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! Checks that all Fluent messages appear at least twice | ||
|
||
use crate::walk::{filter_dirs, walk}; | ||
use regex::Regex; | ||
use std::collections::HashMap; | ||
use std::path::Path; | ||
|
||
lazy_static::lazy_static! { | ||
static ref WORD: Regex = Regex::new(r"\w+").unwrap(); | ||
} | ||
|
||
fn filter_used_messages( | ||
contents: &str, | ||
msgs_not_appeared_yet: &mut HashMap<String, String>, | ||
msgs_appeared_only_once: &mut HashMap<String, String>, | ||
) { | ||
// we don't just check messages never appear in Rust files, | ||
// because messages can be used as parts of other fluent messages in Fluent files, | ||
// so we do checking messages appear only once in all Rust and Fluent files. | ||
let mut matches = WORD.find_iter(contents); | ||
while let Some(name) = matches.next() { | ||
if let Some((name, filename)) = msgs_not_appeared_yet.remove_entry(name.as_str()) { | ||
// if one msg appears for the first time, | ||
// remove it from `msgs_not_appeared_yet` and insert it into `msgs_appeared_only_once`. | ||
msgs_appeared_only_once.insert(name, filename); | ||
} else { | ||
// if one msg appears for the second time, | ||
// remove it from `msgs_appeared_only_once`. | ||
msgs_appeared_only_once.remove(name.as_str()); | ||
} | ||
} | ||
} | ||
|
||
pub fn check(path: &Path, mut all_defined_msgs: HashMap<String, String>, bad: &mut bool) { | ||
let mut msgs_appear_only_once = HashMap::new(); | ||
walk(path, |path, _| filter_dirs(path), &mut |_, contents| { | ||
filter_used_messages(contents, &mut all_defined_msgs, &mut msgs_appear_only_once); | ||
}); | ||
|
||
for (name, filename) in msgs_appear_only_once { | ||
tidy_error!(bad, "{filename}: message `{}` is not used", name,); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters