Skip to content

Commit 52fadd8

Browse files
committed
Migrate BuiltinLintDiag::HiddenUnicodeCodepoints to use LintDiagnostic directly
1 parent 863387c commit 52fadd8

File tree

7 files changed

+98
-126
lines changed

7 files changed

+98
-126
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,6 @@ lint_hidden_glob_reexport = private item shadows public glob re-export
288288
289289
lint_hidden_lifetime_parameters = hidden lifetime parameters in types are deprecated
290290
291-
lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
292-
.label = this {$label} contains {$count ->
293-
[one] an invisible
294-
*[other] invisible
295-
} unicode text flow control {$count ->
296-
[one] codepoint
297-
*[other] codepoints
298-
}
299-
.note = these kind of unicode codepoints change the way text flows on applications that support them, but can cause confusion because they change the order of characters on the screen
300-
.suggestion_remove = if their presence wasn't intentional, you can remove them
301-
.suggestion_escape = if you want to keep them but make them visible in your source code, you can escape them
302-
.no_suggestion_note_escape = if you want to keep them but make them visible in your source code, you can escape them: {$escaped}
303-
304291
lint_identifier_non_ascii_char = identifier contains non-ASCII characters
305292
306293
lint_identifier_uncommon_codepoints = identifier contains {$codepoints_len ->

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,27 +183,6 @@ pub fn decorate_builtin_lint(
183183
lints::ReservedMultihash { suggestion }.decorate_lint(diag);
184184
}
185185
}
186-
BuiltinLintDiag::HiddenUnicodeCodepoints {
187-
label,
188-
count,
189-
span_label,
190-
labels,
191-
escape,
192-
spans,
193-
} => {
194-
lints::HiddenUnicodeCodepointsDiag {
195-
label: &label,
196-
count,
197-
span_label,
198-
labels: labels.map(|spans| lints::HiddenUnicodeCodepointsDiagLabels { spans }),
199-
sub: if escape {
200-
lints::HiddenUnicodeCodepointsDiagSub::Escape { spans }
201-
} else {
202-
lints::HiddenUnicodeCodepointsDiagSub::NoEscape { spans }
203-
},
204-
}
205-
.decorate_lint(diag);
206-
}
207186
BuiltinLintDiag::UnusedBuiltinAttribute {
208187
attr_name,
209188
macro_name,

compiler/rustc_lint/src/lints.rs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -815,80 +815,6 @@ pub(crate) enum InvalidReferenceCastingDiag<'tcx> {
815815
},
816816
}
817817

818-
// hidden_unicode_codepoints.rs
819-
#[derive(LintDiagnostic)]
820-
#[diag(lint_hidden_unicode_codepoints)]
821-
#[note]
822-
pub(crate) struct HiddenUnicodeCodepointsDiag<'a> {
823-
pub label: &'a str,
824-
pub count: usize,
825-
#[label]
826-
pub span_label: Span,
827-
#[subdiagnostic]
828-
pub labels: Option<HiddenUnicodeCodepointsDiagLabels>,
829-
#[subdiagnostic]
830-
pub sub: HiddenUnicodeCodepointsDiagSub,
831-
}
832-
833-
pub(crate) struct HiddenUnicodeCodepointsDiagLabels {
834-
pub spans: Vec<(char, Span)>,
835-
}
836-
837-
impl Subdiagnostic for HiddenUnicodeCodepointsDiagLabels {
838-
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
839-
for (c, span) in self.spans {
840-
diag.span_label(span, format!("{c:?}"));
841-
}
842-
}
843-
}
844-
845-
pub(crate) enum HiddenUnicodeCodepointsDiagSub {
846-
Escape { spans: Vec<(char, Span)> },
847-
NoEscape { spans: Vec<(char, Span)> },
848-
}
849-
850-
// Used because of multiple multipart_suggestion and note
851-
impl Subdiagnostic for HiddenUnicodeCodepointsDiagSub {
852-
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
853-
match self {
854-
HiddenUnicodeCodepointsDiagSub::Escape { spans } => {
855-
diag.multipart_suggestion_with_style(
856-
fluent::lint_suggestion_remove,
857-
spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
858-
Applicability::MachineApplicable,
859-
SuggestionStyle::HideCodeAlways,
860-
);
861-
diag.multipart_suggestion(
862-
fluent::lint_suggestion_escape,
863-
spans
864-
.into_iter()
865-
.map(|(c, span)| {
866-
let c = format!("{c:?}");
867-
(span, c[1..c.len() - 1].to_string())
868-
})
869-
.collect(),
870-
Applicability::MachineApplicable,
871-
);
872-
}
873-
HiddenUnicodeCodepointsDiagSub::NoEscape { spans } => {
874-
// FIXME: in other suggestions we've reversed the inner spans of doc comments. We
875-
// should do the same here to provide the same good suggestions as we do for
876-
// literals above.
877-
diag.arg(
878-
"escaped",
879-
spans
880-
.into_iter()
881-
.map(|(c, _)| format!("{c:?}"))
882-
.collect::<Vec<String>>()
883-
.join(", "),
884-
);
885-
diag.note(fluent::lint_suggestion_remove);
886-
diag.note(fluent::lint_no_suggestion_note_escape);
887-
}
888-
}
889-
}
890-
}
891-
892818
// map_unit_fn.rs
893819
#[derive(LintDiagnostic)]
894820
#[diag(lint_map_unit_fn)]

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,6 @@ pub enum BuiltinLintDiag {
668668
is_string: bool,
669669
suggestion: Span,
670670
},
671-
HiddenUnicodeCodepoints {
672-
label: String,
673-
count: usize,
674-
span_label: Span,
675-
labels: Option<Vec<(char, Span)>>,
676-
escape: bool,
677-
spans: Vec<(char, Span)>,
678-
},
679671
TrailingMacro(bool, Ident),
680672
BreakWithLabelAndLoop(Span),
681673
UnicodeTextFlow(Span, String),

compiler/rustc_parse/messages.ftl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ parse_generics_in_path = unexpected generic arguments in path
359359
360360
parse_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
361361
parse_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
362+
363+
parse_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
364+
.label = this {$label} contains {$count ->
365+
[one] an invisible
366+
*[other] invisible
367+
} unicode text flow control {$count ->
368+
[one] codepoint
369+
*[other] codepoints
370+
}
371+
.note = these kind of unicode codepoints change the way text flows on applications that support them, but can cause confusion because they change the order of characters on the screen
372+
.suggestion_remove = if their presence wasn't intentional, you can remove them
373+
.suggestion_escape = if you want to keep them but make them visible in your source code, you can escape them
374+
.no_suggestion_note_escape = if you want to keep them but make them visible in your source code, you can escape them: {$escaped}
375+
362376
parse_if_expression_missing_condition = missing condition for `if` expression
363377
.condition_label = expected condition here
364378
.block_label = if this block is the condition of the `if` expression, then it must be followed by another block

compiler/rustc_parse/src/errors.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use rustc_ast::{Path, Visibility};
88
use rustc_errors::codes::*;
99
use rustc_errors::{
1010
Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, Subdiagnostic,
11+
SuggestionStyle,
1112
};
12-
use rustc_macros::{Diagnostic, Subdiagnostic};
13+
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1314
use rustc_session::errors::ExprParenthesesNeeded;
1415
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
1516
use rustc_span::{Ident, Span, Symbol};
@@ -3643,3 +3644,76 @@ pub(crate) struct ExpectedRegisterClassOrExplicitRegister {
36433644
#[primary_span]
36443645
pub(crate) span: Span,
36453646
}
3647+
3648+
#[derive(LintDiagnostic)]
3649+
#[diag(parse_hidden_unicode_codepoints)]
3650+
#[note]
3651+
pub(crate) struct HiddenUnicodeCodepointsDiag {
3652+
pub label: String,
3653+
pub count: usize,
3654+
#[label]
3655+
pub span_label: Span,
3656+
#[subdiagnostic]
3657+
pub labels: Option<HiddenUnicodeCodepointsDiagLabels>,
3658+
#[subdiagnostic]
3659+
pub sub: HiddenUnicodeCodepointsDiagSub,
3660+
}
3661+
3662+
pub(crate) struct HiddenUnicodeCodepointsDiagLabels {
3663+
pub spans: Vec<(char, Span)>,
3664+
}
3665+
3666+
impl Subdiagnostic for HiddenUnicodeCodepointsDiagLabels {
3667+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
3668+
for (c, span) in self.spans {
3669+
diag.span_label(span, format!("{c:?}"));
3670+
}
3671+
}
3672+
}
3673+
3674+
pub(crate) enum HiddenUnicodeCodepointsDiagSub {
3675+
Escape { spans: Vec<(char, Span)> },
3676+
NoEscape { spans: Vec<(char, Span)> },
3677+
}
3678+
3679+
// Used because of multiple multipart_suggestion and note
3680+
impl Subdiagnostic for HiddenUnicodeCodepointsDiagSub {
3681+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
3682+
match self {
3683+
HiddenUnicodeCodepointsDiagSub::Escape { spans } => {
3684+
diag.multipart_suggestion_with_style(
3685+
fluent::parse_suggestion_remove,
3686+
spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
3687+
Applicability::MachineApplicable,
3688+
SuggestionStyle::HideCodeAlways,
3689+
);
3690+
diag.multipart_suggestion(
3691+
fluent::parse_suggestion_escape,
3692+
spans
3693+
.into_iter()
3694+
.map(|(c, span)| {
3695+
let c = format!("{c:?}");
3696+
(span, c[1..c.len() - 1].to_string())
3697+
})
3698+
.collect(),
3699+
Applicability::MachineApplicable,
3700+
);
3701+
}
3702+
HiddenUnicodeCodepointsDiagSub::NoEscape { spans } => {
3703+
// FIXME: in other suggestions we've reversed the inner spans of doc comments. We
3704+
// should do the same here to provide the same good suggestions as we do for
3705+
// literals above.
3706+
diag.arg(
3707+
"escaped",
3708+
spans
3709+
.into_iter()
3710+
.map(|(c, _)| format!("{c:?}"))
3711+
.collect::<Vec<String>>()
3712+
.join(", "),
3713+
);
3714+
diag.note(fluent::parse_suggestion_remove);
3715+
diag.note(fluent::parse_no_suggestion_note_escape);
3716+
}
3717+
}
3718+
}
3719+
}

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,21 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
543543
})
544544
.collect();
545545

546+
let label = label.to_string();
546547
let count = spans.len();
547-
let labels = point_at_inner_spans.then_some(spans.clone());
548+
let labels = point_at_inner_spans
549+
.then_some(errors::HiddenUnicodeCodepointsDiagLabels { spans: spans.clone() });
550+
let sub = if point_at_inner_spans && !spans.is_empty() {
551+
errors::HiddenUnicodeCodepointsDiagSub::Escape { spans }
552+
} else {
553+
errors::HiddenUnicodeCodepointsDiagSub::NoEscape { spans }
554+
};
548555

549556
self.psess.buffer_lint(
550557
TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
551558
span,
552559
ast::CRATE_NODE_ID,
553-
BuiltinLintDiag::HiddenUnicodeCodepoints {
554-
label: label.to_string(),
555-
count,
556-
span_label: span,
557-
labels,
558-
escape: point_at_inner_spans && !spans.is_empty(),
559-
spans,
560-
},
560+
errors::HiddenUnicodeCodepointsDiag { label, count, span_label: span, labels, sub },
561561
);
562562
}
563563

0 commit comments

Comments
 (0)