|
1 | 1 | use std::num::NonZeroU32;
|
2 | 2 |
|
3 |
| -use rustc_errors::{fluent, AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage}; |
| 3 | +use rustc_errors::{ |
| 4 | + fluent, AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage, SuggestionStyle, |
| 5 | +}; |
4 | 6 | use rustc_hir::def_id::DefId;
|
5 | 7 | use rustc_macros::{LintDiagnostic, Subdiagnostic};
|
6 | 8 | use rustc_middle::ty::{Predicate, Ty, TyCtxt};
|
@@ -363,6 +365,92 @@ pub struct ForLoopsOverFalliblesSuggestion<'a> {
|
363 | 365 | pub end_span: Span,
|
364 | 366 | }
|
365 | 367 |
|
| 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 | + |
366 | 454 | // internal.rs
|
367 | 455 | #[derive(LintDiagnostic)]
|
368 | 456 | #[diag(lint_default_hash_types)]
|
|
0 commit comments