Skip to content

Commit 9fa45c9

Browse files
committed
separate messages for individual categories
1 parent 23a3d77 commit 9fa45c9

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(generic_nonzero)]
3536
#![feature(if_let_guard)]
3637
#![feature(iter_order_by)]

compiler/rustc_lint/src/lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ pub struct IdentifierNonAsciiChar;
11011101
pub struct IdentifierUncommonCodepoints {
11021102
pub codepoints: Vec<char>,
11031103
pub codepoints_len: usize,
1104+
pub identifier_type: String,
11041105
}
11051106

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

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)