Skip to content

Commit

Permalink
Rollup merge of rust-lang#67810 - crlf0710:uncommon_codepoints_lint, …
Browse files Browse the repository at this point in the history
…r=Manishearth

Implement uncommon_codepoints lint.

Part of rust-lang#55467 .  The checks of `$crate` and `{{root}}` are very unfortunate. But i'm not sure where they belongs to.
  • Loading branch information
Dylan-DPC committed Jan 2, 2020
2 parents 614fb59 + a204e81 commit 6683871
Show file tree
Hide file tree
Showing 6 changed files with 83 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
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 6683871

Please sign in to comment.