Skip to content

Commit

Permalink
Implement uncommon_codepoints lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
crlf0710 committed Jan 3, 2020
1 parent 766fba3 commit 485e98a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3642,6 +3642,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"syntax",
"unicode-security",
]

[[package]]
Expand Down Expand Up @@ -4940,6 +4941,21 @@ dependencies = [
"smallvec 1.0.0",
]

[[package]]
name = "unicode-script"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b2c5c29e805da6817f5af6a627d65adb045cebf05cccd5a3493d6109454391c"

[[package]]
name = "unicode-security"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c49d35967fa037b881acc34ef717c38c4b5560eba10e3685271b3f530bb19634"
dependencies = [
"unicode-script",
]

[[package]]
name = "unicode-segmentation"
version = "1.6.0"
Expand Down
1 change: 1 addition & 0 deletions src/librustc_lint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "lib.rs"

[dependencies]
log = "0.4"
unicode-security = "0.0.2"
rustc = { path = "../librustc" }
rustc_target = { path = "../librustc_target" }
syntax = { path = "../libsyntax" }
Expand Down
25 changes: 21 additions & 4 deletions src/librustc_lint/non_ascii_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,32 @@ declare_lint! {
"detects non-ASCII identifiers"
}

declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS]);
declare_lint! {
pub UNCOMMON_CODEPOINTS,
Warn,
"detects uncommon Unicode codepoints in identifiers"
}

declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS]);

impl EarlyLintPass for NonAsciiIdents {
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: ast::Ident) {
if !ident.name.as_str().is_ascii() {
use unicode_security::GeneralSecurityProfile;
let name_str = ident.name.as_str();
if name_str.is_ascii() {
return;
}
cx.struct_span_lint(
NON_ASCII_IDENTS,
ident.span,
"identifier contains non-ASCII characters",
)
.emit();
if !name_str.chars().all(GeneralSecurityProfile::identifier_allowed) {
cx.struct_span_lint(
NON_ASCII_IDENTS,
UNCOMMON_CODEPOINTS,
ident.span,
"identifier contains non-ASCII characters",
"identifier contains uncommon Unicode codepoints",
)
.emit();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-48508.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// ignore-asmjs wasm2js does not support source maps yet

#![feature(non_ascii_idents)]
#[allow(uncommon_codepoints)]

#[path = "issue-48508-aux.rs"]
mod other_file;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(non_ascii_idents)]
#![deny(uncommon_codepoints)]

const µ: f64 = 0.000001; //~ ERROR identifier contains uncommon Unicode codepoints

fn dijkstra() {} //~ ERROR identifier contains uncommon Unicode codepoints

fn main() {
let ㇻㇲㇳ = "rust"; //~ ERROR identifier contains uncommon Unicode codepoints
println!("{}", ㇻㇲㇳ); //~ ERROR identifier contains uncommon Unicode codepoints
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error: identifier contains uncommon Unicode codepoints
--> $DIR/lint-uncommon-codepoints.rs:4:7
|
LL | const µ: f64 = 0.000001;
| ^
|
note: lint level defined here
--> $DIR/lint-uncommon-codepoints.rs:2:9
|
LL | #![deny(uncommon_codepoints)]
| ^^^^^^^^^^^^^^^^^^^

error: identifier contains uncommon Unicode codepoints
--> $DIR/lint-uncommon-codepoints.rs:6:4
|
LL | fn dijkstra() {}
| ^^^^^^^

error: identifier contains uncommon Unicode codepoints
--> $DIR/lint-uncommon-codepoints.rs:9:9
|
LL | let ㇻㇲㇳ = "rust";
| ^^^^^^

error: identifier contains uncommon Unicode codepoints
--> $DIR/lint-uncommon-codepoints.rs:10:20
|
LL | println!("{}", ㇻㇲㇳ);
| ^^^^^^

error: aborting due to 4 previous errors

2 changes: 2 additions & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ const WHITELIST: &[Crate<'_>] = &[
Crate("thread_local"),
Crate("ucd-util"),
Crate("unicode-normalization"),
Crate("unicode-script"),
Crate("unicode-security"),
Crate("unicode-width"),
Crate("unicode-xid"),
Crate("unreachable"),
Expand Down

0 comments on commit 485e98a

Please sign in to comment.