Skip to content

Commit 6de8e39

Browse files
committed
in which the non_ascii_idents lint appears (RFC 2457)
RFC 2457 declares: "A `non_ascii_idents` lint is added to the compiler. This lint is allow by default."
1 parent dfd52ba commit 6de8e39

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/librustc_lint/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod nonstandard_style;
3030
pub mod builtin;
3131
mod types;
3232
mod unused;
33+
mod non_ascii_idents;
3334

3435
use rustc::lint;
3536
use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
@@ -61,6 +62,7 @@ use nonstandard_style::*;
6162
use builtin::*;
6263
use types::*;
6364
use unused::*;
65+
use non_ascii_idents::*;
6466
use rustc::lint::internal::*;
6567

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

src/librustc_lint/non_ascii_idents.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
cx.struct_span_lint(
16+
NON_ASCII_IDENTS,
17+
ident.span,
18+
"identifier contains non-ASCII characters",
19+
).emit();
20+
}
21+
}
22+
}
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)