Skip to content

Commit b32dc91

Browse files
authored
Rollup merge of #67810 - crlf0710:uncommon_codepoints_lint, r=Manishearth
Implement uncommon_codepoints lint. Part of #55467 . The checks of `$crate` and `{{root}}` are very unfortunate. But i'm not sure where they belongs to.
2 parents e72a214 + 485e98a commit b32dc91

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

Cargo.lock

+16
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,7 @@ dependencies = [
36423642
"rustc_span",
36433643
"rustc_target",
36443644
"syntax",
3645+
"unicode-security",
36453646
]
36463647

36473648
[[package]]
@@ -4940,6 +4941,21 @@ dependencies = [
49404941
"smallvec 1.0.0",
49414942
]
49424943

4944+
[[package]]
4945+
name = "unicode-script"
4946+
version = "0.4.0"
4947+
source = "registry+https://github.com/rust-lang/crates.io-index"
4948+
checksum = "5b2c5c29e805da6817f5af6a627d65adb045cebf05cccd5a3493d6109454391c"
4949+
4950+
[[package]]
4951+
name = "unicode-security"
4952+
version = "0.0.2"
4953+
source = "registry+https://github.com/rust-lang/crates.io-index"
4954+
checksum = "c49d35967fa037b881acc34ef717c38c4b5560eba10e3685271b3f530bb19634"
4955+
dependencies = [
4956+
"unicode-script",
4957+
]
4958+
49434959
[[package]]
49444960
name = "unicode-segmentation"
49454961
version = "1.6.0"

src/librustc_lint/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "lib.rs"
1010

1111
[dependencies]
1212
log = "0.4"
13+
unicode-security = "0.0.2"
1314
rustc = { path = "../librustc" }
1415
rustc_target = { path = "../librustc_target" }
1516
syntax = { path = "../libsyntax" }

src/librustc_lint/non_ascii_idents.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@ declare_lint! {
77
"detects non-ASCII identifiers"
88
}
99

10-
declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS]);
10+
declare_lint! {
11+
pub UNCOMMON_CODEPOINTS,
12+
Warn,
13+
"detects uncommon Unicode codepoints in identifiers"
14+
}
15+
16+
declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS, UNCOMMON_CODEPOINTS]);
1117

1218
impl EarlyLintPass for NonAsciiIdents {
1319
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: ast::Ident) {
14-
if !ident.name.as_str().is_ascii() {
20+
use unicode_security::GeneralSecurityProfile;
21+
let name_str = ident.name.as_str();
22+
if name_str.is_ascii() {
23+
return;
24+
}
25+
cx.struct_span_lint(
26+
NON_ASCII_IDENTS,
27+
ident.span,
28+
"identifier contains non-ASCII characters",
29+
)
30+
.emit();
31+
if !name_str.chars().all(GeneralSecurityProfile::identifier_allowed) {
1532
cx.struct_span_lint(
16-
NON_ASCII_IDENTS,
33+
UNCOMMON_CODEPOINTS,
1734
ident.span,
18-
"identifier contains non-ASCII characters",
35+
"identifier contains uncommon Unicode codepoints",
1936
)
2037
.emit();
2138
}

src/test/ui/issues/issue-48508.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// ignore-asmjs wasm2js does not support source maps yet
1212

1313
#![feature(non_ascii_idents)]
14+
#[allow(uncommon_codepoints)]
1415

1516
#[path = "issue-48508-aux.rs"]
1617
mod other_file;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(non_ascii_idents)]
2+
#![deny(uncommon_codepoints)]
3+
4+
const µ: f64 = 0.000001; //~ ERROR identifier contains uncommon Unicode codepoints
5+
6+
fn dijkstra() {} //~ ERROR identifier contains uncommon Unicode codepoints
7+
8+
fn main() {
9+
let ㇻㇲㇳ = "rust"; //~ ERROR identifier contains uncommon Unicode codepoints
10+
println!("{}", ㇻㇲㇳ); //~ ERROR identifier contains uncommon Unicode codepoints
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: identifier contains uncommon Unicode codepoints
2+
--> $DIR/lint-uncommon-codepoints.rs:4:7
3+
|
4+
LL | const µ: f64 = 0.000001;
5+
| ^
6+
|
7+
note: lint level defined here
8+
--> $DIR/lint-uncommon-codepoints.rs:2:9
9+
|
10+
LL | #![deny(uncommon_codepoints)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: identifier contains uncommon Unicode codepoints
14+
--> $DIR/lint-uncommon-codepoints.rs:6:4
15+
|
16+
LL | fn dijkstra() {}
17+
| ^^^^^^^
18+
19+
error: identifier contains uncommon Unicode codepoints
20+
--> $DIR/lint-uncommon-codepoints.rs:9:9
21+
|
22+
LL | let ㇻㇲㇳ = "rust";
23+
| ^^^^^^
24+
25+
error: identifier contains uncommon Unicode codepoints
26+
--> $DIR/lint-uncommon-codepoints.rs:10:20
27+
|
28+
LL | println!("{}", ㇻㇲㇳ);
29+
| ^^^^^^
30+
31+
error: aborting due to 4 previous errors
32+

src/tools/tidy/src/deps.rs

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ const WHITELIST: &[Crate<'_>] = &[
171171
Crate("thread_local"),
172172
Crate("ucd-util"),
173173
Crate("unicode-normalization"),
174+
Crate("unicode-script"),
175+
Crate("unicode-security"),
174176
Crate("unicode-width"),
175177
Crate("unicode-xid"),
176178
Crate("unreachable"),

0 commit comments

Comments
 (0)