Skip to content

Commit de88a4d

Browse files
committed
separate messages for individual categories
1 parent 98aa362 commit de88a4d

File tree

6 files changed

+121
-18
lines changed

6 files changed

+121
-18
lines changed

compiler/rustc_lint/messages.ftl

+14-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,20 @@ lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of
241241
lint_identifier_non_ascii_char = identifier contains non-ASCII characters
242242
243243
lint_identifier_uncommon_codepoints = identifier contains {$codepoints_len ->
244-
[one] an uncommon Unicode codepoint
245-
*[other] uncommon Unicode codepoints
244+
[one] { $identifier_type ->
245+
[Exclusion] an {$identifier_type} Unicode codepoint
246+
[Technical] a {$identifier_type} Unicode codepoint
247+
[Limited_Use] a {$identifier_type} Unicode codepoint
248+
[Not_NFKC] a {$identifier_type} Unicode codepoint
249+
*[other] an uncommon Unicode codepoint
250+
}
251+
*[other] { $identifier_type ->
252+
[Exclusion] {$identifier_type} Unicode codepoints
253+
[Technical] {$identifier_type} Unicode codepoints
254+
[Limited_Use] {$identifier_type} Unicode codepoints
255+
[Not_NFKC] {$identifier_type} Unicode codepoints
256+
*[other] uncommon Unicode codepoints
257+
}
246258
}: {$codepoints}
247259
248260
lint_ignored_unless_crate_specified = {$level}({$name}) is ignored unless specified at crate level

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(array_windows)]
3232
#![feature(box_patterns)]
3333
#![feature(control_flow_enum)]
34+
#![feature(extract_if)]
3435
#![feature(if_let_guard)]
3536
#![feature(iter_order_by)]
3637
#![feature(let_chains)]

compiler/rustc_lint/src/lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ pub struct IdentifierNonAsciiChar;
10891089
pub struct IdentifierUncommonCodepoints {
10901090
pub codepoints: Vec<char>,
10911091
pub codepoints_len: usize,
1092+
pub identifier_type: String,
10921093
}
10931094

10941095
#[derive(LintDiagnostic)]

compiler/rustc_lint/src/non_ascii_idents.rs

+99-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast as ast;
77
use rustc_data_structures::fx::FxIndexMap;
88
use rustc_data_structures::unord::UnordMap;
99
use rustc_span::symbol::Symbol;
10+
use unicode_security::general_security_profile::IdentifierType;
1011

1112
declare_lint! {
1213
/// The `non_ascii_idents` lint detects non-ASCII identifiers.
@@ -190,17 +191,104 @@ impl EarlyLintPass for NonAsciiIdents {
190191
if check_uncommon_codepoints
191192
&& !symbol_str.chars().all(GeneralSecurityProfile::identifier_allowed)
192193
{
193-
let codepoints: Vec<_> = symbol_str
194-
.chars()
195-
.filter(|c| !GeneralSecurityProfile::identifier_allowed(*c))
196-
.collect();
197-
let codepoints_len = codepoints.len();
198-
199-
cx.emit_span_lint(
200-
UNCOMMON_CODEPOINTS,
201-
sp,
202-
IdentifierUncommonCodepoints { codepoints, codepoints_len },
203-
);
194+
let mut chars = symbol_str.chars().collect::<Vec<_>>();
195+
196+
let exclusion = chars
197+
.extract_if(|c| {
198+
GeneralSecurityProfile::identifier_type(*c)
199+
== Some(IdentifierType::Exclusion)
200+
})
201+
.collect::<Vec<_>>();
202+
if !exclusion.is_empty() {
203+
let exclusion_len = exclusion.len();
204+
205+
cx.emit_span_lint(
206+
UNCOMMON_CODEPOINTS,
207+
sp,
208+
IdentifierUncommonCodepoints {
209+
codepoints: exclusion,
210+
codepoints_len: exclusion_len,
211+
identifier_type: String::from("Exclusion"),
212+
},
213+
);
214+
}
215+
216+
let technical = chars
217+
.extract_if(|c| {
218+
GeneralSecurityProfile::identifier_type(*c)
219+
== Some(IdentifierType::Technical)
220+
})
221+
.collect::<Vec<_>>();
222+
if !technical.is_empty() {
223+
let technical_len = technical.len();
224+
225+
cx.emit_span_lint(
226+
UNCOMMON_CODEPOINTS,
227+
sp,
228+
IdentifierUncommonCodepoints {
229+
codepoints: technical,
230+
codepoints_len: technical_len,
231+
identifier_type: String::from("Technical"),
232+
},
233+
);
234+
}
235+
236+
let limited_use = chars
237+
.extract_if(|c| {
238+
GeneralSecurityProfile::identifier_type(*c)
239+
== Some(IdentifierType::Limited_Use)
240+
})
241+
.collect::<Vec<_>>();
242+
if !limited_use.is_empty() {
243+
let limited_use_len = limited_use.len();
244+
245+
cx.emit_span_lint(
246+
UNCOMMON_CODEPOINTS,
247+
sp,
248+
IdentifierUncommonCodepoints {
249+
codepoints: limited_use,
250+
codepoints_len: limited_use_len,
251+
identifier_type: String::from("Limited_Use"),
252+
},
253+
);
254+
}
255+
256+
let not_nfkc = chars
257+
.extract_if(|c| {
258+
GeneralSecurityProfile::identifier_type(*c)
259+
== Some(IdentifierType::Not_NFKC)
260+
})
261+
.collect::<Vec<_>>();
262+
if !not_nfkc.is_empty() {
263+
let not_nfkc_len = not_nfkc.len();
264+
265+
cx.emit_span_lint(
266+
UNCOMMON_CODEPOINTS,
267+
sp,
268+
IdentifierUncommonCodepoints {
269+
codepoints: not_nfkc,
270+
codepoints_len: not_nfkc_len,
271+
identifier_type: String::from("Not_NFKC"),
272+
},
273+
);
274+
}
275+
276+
let remaining = chars
277+
.extract_if(|c| !GeneralSecurityProfile::identifier_allowed(*c))
278+
.collect::<Vec<_>>();
279+
if !remaining.is_empty() {
280+
let remaining_len = remaining.len();
281+
282+
cx.emit_span_lint(
283+
UNCOMMON_CODEPOINTS,
284+
sp,
285+
IdentifierUncommonCodepoints {
286+
codepoints: remaining,
287+
codepoints_len: remaining_len,
288+
identifier_type: String::new(),
289+
},
290+
);
291+
}
204292
}
205293
}
206294

tests/ui/lint/rfc-2457-non-ascii-idents/lint-uncommon-codepoints.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![deny(uncommon_codepoints)]
22

3-
const µ: f64 = 0.000001; //~ ERROR identifier contains an uncommon Unicode codepoint
3+
const µ: f64 = 0.000001; //~ ERROR identifier contains a Not_NFKC Unicode codepoint: 'µ'
44
//~| WARNING should have an upper case name
55

6-
fn dijkstra() {} //~ ERROR identifier contains an uncommon Unicode codepoint
6+
fn dijkstra() {}
7+
//~^ ERROR identifier contains a Not_NFKC Unicode codepoint: 'ij'
78

89
fn main() {
910
let ㇻㇲㇳ = "rust"; //~ ERROR identifier contains uncommon Unicode codepoints

tests/ui/lint/rfc-2457-non-ascii-idents/lint-uncommon-codepoints.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: identifier contains an uncommon Unicode codepoint: 'µ'
1+
error: identifier contains a Not_NFKC Unicode codepoint: 'µ'
22
--> $DIR/lint-uncommon-codepoints.rs:3:7
33
|
44
LL | const µ: f64 = 0.000001;
@@ -10,14 +10,14 @@ note: the lint level is defined here
1010
LL | #![deny(uncommon_codepoints)]
1111
| ^^^^^^^^^^^^^^^^^^^
1212

13-
error: identifier contains an uncommon Unicode codepoint: 'ij'
13+
error: identifier contains a Not_NFKC Unicode codepoint: 'ij'
1414
--> $DIR/lint-uncommon-codepoints.rs:6:4
1515
|
1616
LL | fn dijkstra() {}
1717
| ^^^^^^^
1818

1919
error: identifier contains uncommon Unicode codepoints: 'ㇻ', 'ㇲ', and 'ㇳ'
20-
--> $DIR/lint-uncommon-codepoints.rs:9:9
20+
--> $DIR/lint-uncommon-codepoints.rs:10:9
2121
|
2222
LL | let ㇻㇲㇳ = "rust";
2323
| ^^^^^^

0 commit comments

Comments
 (0)