Skip to content

Commit af4b62b

Browse files
authored
Rollup merge of rust-lang#61883 - zackmdavis:non_ascii_idents_lint, r=Manishearth
`non_ascii_idents` lint (part of RFC 2457) RFC 2457 [declares](https://github.com/rust-lang/rfcs/blob/121bbeff500c3274cea22c7e0ca176274d592646/text/2457-non-ascii-idents.md): "A `non_ascii_idents` lint is added to the compiler. This lint is allow by default." (Part of rust-lang#55467.) r? @Manishearth
2 parents e58fbbb + 0ae61d8 commit af4b62b

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

src/librustc_lint/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod nonstandard_style;
3131
pub mod builtin;
3232
mod types;
3333
mod unused;
34+
mod non_ascii_idents;
3435

3536
use rustc::lint;
3637
use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
@@ -62,6 +63,7 @@ use nonstandard_style::*;
6263
use builtin::*;
6364
use types::*;
6465
use unused::*;
66+
use non_ascii_idents::*;
6567
use rustc::lint::internal::*;
6668

6769
/// Useful for other parts of the compiler.
@@ -97,6 +99,7 @@ macro_rules! early_lint_passes {
9799
EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
98100
NonCamelCaseTypes: NonCamelCaseTypes,
99101
DeprecatedAttr: DeprecatedAttr::new(),
102+
NonAsciiIdents: NonAsciiIdents,
100103
]);
101104
)
102105
}

src/librustc_lint/non_ascii_idents.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::lint::{EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
2+
use syntax::ast;
3+
4+
declare_lint! {
5+
pub NON_ASCII_IDENTS,
6+
Allow,
7+
"detects non-ASCII identifiers"
8+
}
9+
10+
declare_lint_pass!(NonAsciiIdents => [NON_ASCII_IDENTS]);
11+
12+
impl EarlyLintPass for NonAsciiIdents {
13+
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: ast::Ident) {
14+
if !ident.name.as_str().is_ascii() {
15+
let mut err = cx.struct_span_lint(
16+
NON_ASCII_IDENTS,
17+
ident.span,
18+
"identifier contains non-ASCII characters",
19+
);
20+
err.emit();
21+
}
22+
}
23+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(non_ascii_idents)]
2+
#![deny(non_ascii_idents)]
3+
4+
const חלודה: usize = 2; //~ ERROR identifier contains non-ASCII characters
5+
6+
fn coöperation() {} //~ ERROR identifier contains non-ASCII characters
7+
8+
fn main() {
9+
let naïveté = 2; //~ ERROR identifier contains non-ASCII characters
10+
println!("{}", naïveté); //~ ERROR identifier contains non-ASCII characters
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: identifier contains non-ASCII characters
2+
--> $DIR/lint-non-ascii-idents.rs:4:7
3+
|
4+
LL | const חלודה: usize = 2;
5+
| ^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/lint-non-ascii-idents.rs:2:9
9+
|
10+
LL | #![deny(non_ascii_idents)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: identifier contains non-ASCII characters
14+
--> $DIR/lint-non-ascii-idents.rs:6:4
15+
|
16+
LL | fn coöperation() {}
17+
| ^^^^^^^^^^^
18+
19+
error: identifier contains non-ASCII characters
20+
--> $DIR/lint-non-ascii-idents.rs:9:9
21+
|
22+
LL | let naïveté = 2;
23+
| ^^^^^^^
24+
25+
error: identifier contains non-ASCII characters
26+
--> $DIR/lint-non-ascii-idents.rs:10:20
27+
|
28+
LL | println!("{}", naïveté);
29+
| ^^^^^^^
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)