Skip to content

Commit c06a242

Browse files
committed
migrate: hidden_unicode_codepoints.rs
1 parent ce72f94 commit c06a242

File tree

2 files changed

+107
-50
lines changed

2 files changed

+107
-50
lines changed

compiler/rustc_lint/src/hidden_unicode_codepoints.rs

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
use crate::{EarlyContext, EarlyLintPass, LintContext};
1+
use crate::{
2+
lints::{
3+
HiddenUnicodeCodepointsDiag, HiddenUnicodeCodepointsDiagLabels,
4+
HiddenUnicodeCodepointsDiagSub,
5+
},
6+
EarlyContext, EarlyLintPass, LintContext,
7+
};
28
use ast::util::unicode::{contains_text_flow_control_chars, TEXT_FLOW_CONTROL_CHARS};
39
use rustc_ast as ast;
4-
use rustc_errors::{fluent, Applicability, SuggestionStyle};
510
use rustc_span::{BytePos, Span, Symbol};
611

712
declare_lint! {
@@ -60,55 +65,19 @@ impl HiddenUnicodeCodepoints {
6065
})
6166
.collect();
6267

63-
cx.struct_span_lint(
68+
let count = spans.len();
69+
let labels = point_at_inner_spans
70+
.then_some(HiddenUnicodeCodepointsDiagLabels { spans: spans.clone() });
71+
let sub = if point_at_inner_spans && !spans.is_empty() {
72+
HiddenUnicodeCodepointsDiagSub::Escape { spans }
73+
} else {
74+
HiddenUnicodeCodepointsDiagSub::NoEscape { spans }
75+
};
76+
77+
cx.emit_spanned_lint(
6478
TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
6579
span,
66-
fluent::lint_hidden_unicode_codepoints,
67-
|lint| {
68-
lint.set_arg("label", label);
69-
lint.set_arg("count", spans.len());
70-
lint.span_label(span, fluent::label);
71-
lint.note(fluent::note);
72-
if point_at_inner_spans {
73-
for (c, span) in &spans {
74-
lint.span_label(*span, format!("{:?}", c));
75-
}
76-
}
77-
if point_at_inner_spans && !spans.is_empty() {
78-
lint.multipart_suggestion_with_style(
79-
fluent::suggestion_remove,
80-
spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
81-
Applicability::MachineApplicable,
82-
SuggestionStyle::HideCodeAlways,
83-
);
84-
lint.multipart_suggestion(
85-
fluent::suggestion_escape,
86-
spans
87-
.into_iter()
88-
.map(|(c, span)| {
89-
let c = format!("{:?}", c);
90-
(span, c[1..c.len() - 1].to_string())
91-
})
92-
.collect(),
93-
Applicability::MachineApplicable,
94-
);
95-
} else {
96-
// FIXME: in other suggestions we've reversed the inner spans of doc comments. We
97-
// should do the same here to provide the same good suggestions as we do for
98-
// literals above.
99-
lint.set_arg(
100-
"escaped",
101-
spans
102-
.into_iter()
103-
.map(|(c, _)| format!("{:?}", c))
104-
.collect::<Vec<String>>()
105-
.join(", "),
106-
);
107-
lint.note(fluent::suggestion_remove);
108-
lint.note(fluent::no_suggestion_note_escape);
109-
}
110-
lint
111-
},
80+
HiddenUnicodeCodepointsDiag { label, count, span_label: span, labels, sub },
11281
);
11382
}
11483
}

compiler/rustc_lint/src/lints.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::num::NonZeroU32;
22

3-
use rustc_errors::{fluent, AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage};
3+
use rustc_errors::{
4+
fluent, AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage, SuggestionStyle,
5+
};
46
use rustc_hir::def_id::DefId;
57
use rustc_macros::{LintDiagnostic, Subdiagnostic};
68
use rustc_middle::ty::{Predicate, Ty, TyCtxt};
@@ -363,6 +365,92 @@ pub struct ForLoopsOverFalliblesSuggestion<'a> {
363365
pub end_span: Span,
364366
}
365367

368+
// hidden_unicode_codepoints.rs
369+
#[derive(LintDiagnostic)]
370+
#[diag(lint_hidden_unicode_codepoints)]
371+
#[note]
372+
pub struct HiddenUnicodeCodepointsDiag<'a> {
373+
pub label: &'a str,
374+
pub count: usize,
375+
#[label]
376+
pub span_label: Span,
377+
#[subdiagnostic]
378+
pub labels: Option<HiddenUnicodeCodepointsDiagLabels>,
379+
#[subdiagnostic]
380+
pub sub: HiddenUnicodeCodepointsDiagSub,
381+
}
382+
383+
pub struct HiddenUnicodeCodepointsDiagLabels {
384+
pub spans: Vec<(char, Span)>,
385+
}
386+
387+
impl AddToDiagnostic for HiddenUnicodeCodepointsDiagLabels {
388+
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F)
389+
where
390+
F: Fn(
391+
&mut rustc_errors::Diagnostic,
392+
rustc_errors::SubdiagnosticMessage,
393+
) -> rustc_errors::SubdiagnosticMessage,
394+
{
395+
for (c, span) in self.spans {
396+
diag.span_label(span, format!("{:?}", c));
397+
}
398+
}
399+
}
400+
401+
pub enum HiddenUnicodeCodepointsDiagSub {
402+
Escape { spans: Vec<(char, Span)> },
403+
NoEscape { spans: Vec<(char, Span)> },
404+
}
405+
406+
// Used because of multiple multipart_suggestion and note
407+
impl AddToDiagnostic for HiddenUnicodeCodepointsDiagSub {
408+
fn add_to_diagnostic_with<F>(self, diag: &mut rustc_errors::Diagnostic, _: F)
409+
where
410+
F: Fn(
411+
&mut rustc_errors::Diagnostic,
412+
rustc_errors::SubdiagnosticMessage,
413+
) -> rustc_errors::SubdiagnosticMessage,
414+
{
415+
match self {
416+
HiddenUnicodeCodepointsDiagSub::Escape { spans } => {
417+
diag.multipart_suggestion_with_style(
418+
fluent::suggestion_remove,
419+
spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
420+
Applicability::MachineApplicable,
421+
SuggestionStyle::HideCodeAlways,
422+
);
423+
diag.multipart_suggestion(
424+
fluent::suggestion_escape,
425+
spans
426+
.into_iter()
427+
.map(|(c, span)| {
428+
let c = format!("{:?}", c);
429+
(span, c[1..c.len() - 1].to_string())
430+
})
431+
.collect(),
432+
Applicability::MachineApplicable,
433+
);
434+
}
435+
HiddenUnicodeCodepointsDiagSub::NoEscape { spans } => {
436+
// FIXME: in other suggestions we've reversed the inner spans of doc comments. We
437+
// should do the same here to provide the same good suggestions as we do for
438+
// literals above.
439+
diag.set_arg(
440+
"escaped",
441+
spans
442+
.into_iter()
443+
.map(|(c, _)| format!("{:?}", c))
444+
.collect::<Vec<String>>()
445+
.join(", "),
446+
);
447+
diag.note(fluent::suggestion_remove);
448+
diag.note(fluent::no_suggestion_note_escape);
449+
}
450+
}
451+
}
452+
}
453+
366454
// internal.rs
367455
#[derive(LintDiagnostic)]
368456
#[diag(lint_default_hash_types)]

0 commit comments

Comments
 (0)