Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement uncommon_codepoints lint. #67810

Merged
merged 1 commit into from Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock
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
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
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) {
crlf0710 marked this conversation as resolved.
Show resolved Hide resolved
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
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
@@ -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
}
@@ -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
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