lint: port non-ascii-idents diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
c29e05e745
commit
48e4bf115f
2 changed files with 29 additions and 18 deletions
|
@ -60,3 +60,15 @@ lint-cstring-ptr = getting the inner pointer of a temporary `CString`
|
||||||
.unwrap-label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
.unwrap-label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
||||||
.note = pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
|
.note = pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
|
||||||
.help = for more information, see https://doc.rust-lang.org/reference/destructors.html
|
.help = for more information, see https://doc.rust-lang.org/reference/destructors.html
|
||||||
|
|
||||||
|
lint-identifier-non-ascii-char = identifier contains non-ASCII characters
|
||||||
|
|
||||||
|
lint-identifier-uncommon-codepoints = identifier contains uncommon Unicode codepoints
|
||||||
|
|
||||||
|
lint-confusable-identifier-pair = identifier pair considered confusable between `{$existing_sym}` and `{$sym}`
|
||||||
|
.label = this is where the previous identifier occurred
|
||||||
|
|
||||||
|
lint-mixed-script-confusables =
|
||||||
|
the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables
|
||||||
|
.includes-note = the usage includes {$includes}
|
||||||
|
.note = please recheck to make sure their usages are indeed what you want
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::{EarlyContext, EarlyLintPass, LintContext};
|
use crate::{EarlyContext, EarlyLintPass, LintContext};
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
|
use rustc_errors::fluent;
|
||||||
use rustc_span::symbol::Symbol;
|
use rustc_span::symbol::Symbol;
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
|
@ -180,13 +181,13 @@ impl EarlyLintPass for NonAsciiIdents {
|
||||||
}
|
}
|
||||||
has_non_ascii_idents = true;
|
has_non_ascii_idents = true;
|
||||||
cx.struct_span_lint(NON_ASCII_IDENTS, sp, |lint| {
|
cx.struct_span_lint(NON_ASCII_IDENTS, sp, |lint| {
|
||||||
lint.build("identifier contains non-ASCII characters").emit();
|
lint.build(fluent::lint::identifier_non_ascii_char).emit();
|
||||||
});
|
});
|
||||||
if check_uncommon_codepoints
|
if check_uncommon_codepoints
|
||||||
&& !symbol_str.chars().all(GeneralSecurityProfile::identifier_allowed)
|
&& !symbol_str.chars().all(GeneralSecurityProfile::identifier_allowed)
|
||||||
{
|
{
|
||||||
cx.struct_span_lint(UNCOMMON_CODEPOINTS, sp, |lint| {
|
cx.struct_span_lint(UNCOMMON_CODEPOINTS, sp, |lint| {
|
||||||
lint.build("identifier contains uncommon Unicode codepoints").emit();
|
lint.build(fluent::lint::identifier_uncommon_codepoints).emit();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -216,15 +217,11 @@ impl EarlyLintPass for NonAsciiIdents {
|
||||||
.and_modify(|(existing_symbol, existing_span, existing_is_ascii)| {
|
.and_modify(|(existing_symbol, existing_span, existing_is_ascii)| {
|
||||||
if !*existing_is_ascii || !is_ascii {
|
if !*existing_is_ascii || !is_ascii {
|
||||||
cx.struct_span_lint(CONFUSABLE_IDENTS, sp, |lint| {
|
cx.struct_span_lint(CONFUSABLE_IDENTS, sp, |lint| {
|
||||||
lint.build(&format!(
|
lint.build(fluent::lint::confusable_identifier_pair)
|
||||||
"identifier pair considered confusable between `{}` and `{}`",
|
.set_arg("existing_sym", *existing_symbol)
|
||||||
existing_symbol, symbol
|
.set_arg("sym", symbol)
|
||||||
))
|
.span_label(*existing_span, fluent::lint::label)
|
||||||
.span_label(
|
.emit();
|
||||||
*existing_span,
|
|
||||||
"this is where the previous identifier occurred",
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if *existing_is_ascii && !is_ascii {
|
if *existing_is_ascii && !is_ascii {
|
||||||
|
@ -326,18 +323,20 @@ impl EarlyLintPass for NonAsciiIdents {
|
||||||
|
|
||||||
for ((sp, ch_list), script_set) in lint_reports {
|
for ((sp, ch_list), script_set) in lint_reports {
|
||||||
cx.struct_span_lint(MIXED_SCRIPT_CONFUSABLES, sp, |lint| {
|
cx.struct_span_lint(MIXED_SCRIPT_CONFUSABLES, sp, |lint| {
|
||||||
let message = format!(
|
let mut includes = String::new();
|
||||||
"the usage of Script Group `{}` in this crate consists solely of mixed script confusables",
|
|
||||||
script_set);
|
|
||||||
let mut note = "the usage includes ".to_string();
|
|
||||||
for (idx, ch) in ch_list.into_iter().enumerate() {
|
for (idx, ch) in ch_list.into_iter().enumerate() {
|
||||||
if idx != 0 {
|
if idx != 0 {
|
||||||
note += ", ";
|
includes += ", ";
|
||||||
}
|
}
|
||||||
let char_info = format!("'{}' (U+{:04X})", ch, ch as u32);
|
let char_info = format!("'{}' (U+{:04X})", ch, ch as u32);
|
||||||
note += &char_info;
|
includes += &char_info;
|
||||||
}
|
}
|
||||||
lint.build(&message).note(¬e).note("please recheck to make sure their usages are indeed what you want").emit();
|
lint.build(fluent::lint::mixed_script_confusables)
|
||||||
|
.set_arg("set", script_set.to_string())
|
||||||
|
.set_arg("includes", includes)
|
||||||
|
.note(fluent::lint::includes_note)
|
||||||
|
.note(fluent::lint::note)
|
||||||
|
.emit();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue