Skip to content

Commit a61b924

Browse files
committed
implement some suggestions
1 parent 3bf4164 commit a61b924

File tree

5 files changed

+27
-90
lines changed

5 files changed

+27
-90
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,12 @@ lint_identifier_uncommon_codepoints = identifier contains {$codepoints_len ->
258258
}: {$codepoints}
259259
.note = {$codepoints_len ->
260260
[one] { $identifier_type ->
261-
[Exclusion] this character is included in the {$identifier_type} Unicode general security profile
262-
[Technical] this character is included in the {$identifier_type} Unicode general security profile
263-
[Limited_Use] this character is included in the {$identifier_type} Unicode general security profile
264-
[Not_NFKC] this character is included in the {$identifier_type} Unicode general security profile
265-
*[other] this Unicode codepoint is uncommon
261+
[Restricted] this Unicode codepoint is included in the Unicode general security profile
262+
*[other] this character is included in the {$identifier_type} Unicode general security profile
266263
}
267264
*[other] { $identifier_type ->
268-
[Exclusion] these characters are included in the {$identifier_type} Unicode general security profile
269-
[Technical] these characters are included in the {$identifier_type} Unicode general security profile
270-
[Limited_Use] these characters are included in the {$identifier_type} Unicode general security profile
271-
[Not_NFKC] these characters are included in the {$identifier_type} Unicode general security profile
272-
*[other] these Unicode codepoints are uncommon
265+
[Restricted] these Unicode codepoints are included in the Unicode general security profile
266+
*[other] these characters are included in the {$identifier_type} Unicode general security profile
273267
}
274268
}
275269

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ pub struct IdentifierNonAsciiChar;
11021102
pub struct IdentifierUncommonCodepoints {
11031103
pub codepoints: Vec<char>,
11041104
pub codepoints_len: usize,
1105-
pub identifier_type: String,
1105+
pub identifier_type: &'static str,
11061106
}
11071107

11081108
#[derive(LintDiagnostic)]

compiler/rustc_lint/src/non_ascii_idents.rs

Lines changed: 20 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -190,90 +190,33 @@ impl EarlyLintPass for NonAsciiIdents {
190190
if check_uncommon_codepoints
191191
&& !symbol_str.chars().all(GeneralSecurityProfile::identifier_allowed)
192192
{
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-
193+
let mut chars: Vec<_> = symbol_str.chars()
194+
.map(|c| (c, GeneralSecurityProfile::identifier_type(c)))
195+
.collect();
196+
197+
for (id_ty, id_ty_descr) in [
198+
(IdentifierType::Exclusion, "Exclusion"),
199+
(IdentifierType::Technical, "Technical"),
200+
(IdentifierType::Limited_Use, "Limited_Use"),
201+
(IdentifierType::Not_NFKC, "Not_NFKC"),
202+
] {
203+
let codepoints: Vec<_> = chars.extract_if(|(_, ty)| *ty == Some(id_ty)).collect();
204+
if codepoints.is_empty() {
205+
continue;
206+
}
264207
cx.emit_span_lint(
265208
UNCOMMON_CODEPOINTS,
266209
sp,
267210
IdentifierUncommonCodepoints {
268-
codepoints: not_nfkc,
269-
codepoints_len: not_nfkc_len,
270-
identifier_type: String::from("Not_NFKC"),
211+
codepoints_len: codepoints.len(),
212+
codepoints: codepoints.into_iter().map(|(c, _)| c).collect(),
213+
identifier_type: id_ty_descr,
271214
},
272215
);
273216
}
274217

275218
let remaining = chars
276-
.extract_if(|c| !GeneralSecurityProfile::identifier_allowed(*c))
219+
.extract_if(|(c, _)| !GeneralSecurityProfile::identifier_allowed(*c))
277220
.collect::<Vec<_>>();
278221
if !remaining.is_empty() {
279222
let remaining_len = remaining.len();
@@ -282,9 +225,9 @@ impl EarlyLintPass for NonAsciiIdents {
282225
UNCOMMON_CODEPOINTS,
283226
sp,
284227
IdentifierUncommonCodepoints {
285-
codepoints: remaining,
228+
codepoints: remaining.iter().map(|(c, _)| *c).collect(),
286229
codepoints_len: remaining_len,
287-
identifier_type: String::new(),
230+
identifier_type: "Restricted",
288231
},
289232
);
290233
}

tests/ui/lexer/lex-emoji-identifiers.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ warning: identifier contains an uncommon Unicode codepoint: '\u{fe0f}'
4646
LL | let key1️⃣ = "keycap sequence";
4747
| ^^^^
4848
|
49-
= note: this Unicode codepoint is uncommon
49+
= note: this Unicode codepoint is included in the Unicode general security profile
5050
= note: `#[warn(uncommon_codepoints)]` on by default
5151

5252
error: aborting due to 7 previous errors; 1 warning emitted

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ error: identifier contains uncommon Unicode codepoints: 'ㇻ', 'ㇲ', and 'ㇳ'
2525
LL | let ㇻㇲㇳ = "rust";
2626
| ^^^^^^
2727
|
28-
= note: these Unicode codepoints are uncommon
28+
= note: these Unicode codepoints are included in the Unicode general security profile
2929

3030
warning: constant `µ` should have an upper case name
3131
--> $DIR/lint-uncommon-codepoints.rs:3:7

0 commit comments

Comments
 (0)