diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
commit | 64d98f8ee037282c35007b64c2649055c56af1db (patch) | |
tree | 5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /compiler/rustc_lint/src/hidden_unicode_codepoints.rs | |
parent | Adding debian version 1.67.1+dfsg1-1. (diff) | |
download | rustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_lint/src/hidden_unicode_codepoints.rs')
-rw-r--r-- | compiler/rustc_lint/src/hidden_unicode_codepoints.rs | 68 |
1 files changed, 19 insertions, 49 deletions
diff --git a/compiler/rustc_lint/src/hidden_unicode_codepoints.rs b/compiler/rustc_lint/src/hidden_unicode_codepoints.rs index 7106e75db..7c1af6bee 100644 --- a/compiler/rustc_lint/src/hidden_unicode_codepoints.rs +++ b/compiler/rustc_lint/src/hidden_unicode_codepoints.rs @@ -1,7 +1,12 @@ -use crate::{EarlyContext, EarlyLintPass, LintContext}; +use crate::{ + lints::{ + HiddenUnicodeCodepointsDiag, HiddenUnicodeCodepointsDiagLabels, + HiddenUnicodeCodepointsDiagSub, + }, + EarlyContext, EarlyLintPass, LintContext, +}; use ast::util::unicode::{contains_text_flow_control_chars, TEXT_FLOW_CONTROL_CHARS}; use rustc_ast as ast; -use rustc_errors::{fluent, Applicability, SuggestionStyle}; use rustc_span::{BytePos, Span, Symbol}; declare_lint! { @@ -60,55 +65,19 @@ impl HiddenUnicodeCodepoints { }) .collect(); - cx.struct_span_lint( + let count = spans.len(); + let labels = point_at_inner_spans + .then_some(HiddenUnicodeCodepointsDiagLabels { spans: spans.clone() }); + let sub = if point_at_inner_spans && !spans.is_empty() { + HiddenUnicodeCodepointsDiagSub::Escape { spans } + } else { + HiddenUnicodeCodepointsDiagSub::NoEscape { spans } + }; + + cx.emit_spanned_lint( TEXT_DIRECTION_CODEPOINT_IN_LITERAL, span, - fluent::lint_hidden_unicode_codepoints, - |lint| { - lint.set_arg("label", label); - lint.set_arg("count", spans.len()); - lint.span_label(span, fluent::label); - lint.note(fluent::note); - if point_at_inner_spans { - for (c, span) in &spans { - lint.span_label(*span, format!("{:?}", c)); - } - } - if point_at_inner_spans && !spans.is_empty() { - lint.multipart_suggestion_with_style( - fluent::suggestion_remove, - spans.iter().map(|(_, span)| (*span, "".to_string())).collect(), - Applicability::MachineApplicable, - SuggestionStyle::HideCodeAlways, - ); - lint.multipart_suggestion( - fluent::suggestion_escape, - spans - .into_iter() - .map(|(c, span)| { - let c = format!("{:?}", c); - (span, c[1..c.len() - 1].to_string()) - }) - .collect(), - Applicability::MachineApplicable, - ); - } else { - // FIXME: in other suggestions we've reversed the inner spans of doc comments. We - // should do the same here to provide the same good suggestions as we do for - // literals above. - lint.set_arg( - "escaped", - spans - .into_iter() - .map(|(c, _)| format!("{:?}", c)) - .collect::<Vec<String>>() - .join(", "), - ); - lint.note(fluent::suggestion_remove); - lint.note(fluent::no_suggestion_note_escape); - } - lint - }, + HiddenUnicodeCodepointsDiag { label, count, span_label: span, labels, sub }, ); } } @@ -121,6 +90,7 @@ impl EarlyLintPass for HiddenUnicodeCodepoints { } } + #[inline] fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { // byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString` match &expr.kind { |