|
3 | 3 |
|
4 | 4 | use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS; |
5 | 5 | use rustc_errors::{ |
6 | | - add_elided_lifetime_in_path_suggestion, pluralize, Diag, DiagMessage, MultiSpan, |
| 6 | + add_elided_lifetime_in_path_suggestion, pluralize, Diag, DiagArgValue, DiagMessage, MultiSpan, |
7 | 7 | }; |
8 | 8 | use rustc_errors::{Applicability, SuggestionStyle}; |
9 | 9 | use rustc_middle::middle::stability; |
10 | 10 | use rustc_session::lint::{BuiltinLintDiag, Lint}; |
11 | 11 | use rustc_session::Session; |
12 | 12 | use rustc_span::BytePos; |
13 | 13 |
|
14 | | -use std::fmt::Write; |
15 | | - |
| 14 | +use crate::lints::{ |
| 15 | + AvoidAttSyntax, AvoidIntelSyntax, CfgAttrNoAttributes, CrateNameInCfgAttr, CrateTypeInCfgAttr, |
| 16 | + DuplicateMacroAttribute, DuplicateMatcherBinding, IllFormedAttributeInput, IncompleteInclude, |
| 17 | + InnerAttributeUnstable, InvalidCrateTypeValue, MacroIsPrivate, MacroRuleNeverUsed, |
| 18 | + MacroUseDeprecated, MetaVariableStillRepeating, MetaVariableWrongOperator, |
| 19 | + MissingFragmentSpecifier, PrivateExternCrateReexport, UnknownDiagnosticAttribute, |
| 20 | + UnknownMacroVariable, UnnameableTestItems, UnstableFeature, UnusedCrateDependency, UnusedLabel, |
| 21 | + UnusedMacroDefinition, UnusedMacroUse, WasmCAbi, |
| 22 | +}; |
16 | 23 | use crate::{fluent_generated as fluent, LintContext}; |
17 | 24 |
|
18 | 25 | mod check_cfg; |
19 | 26 |
|
20 | | -#[cfg(test)] |
21 | | -mod tests; |
22 | | - |
23 | 27 | fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) { |
24 | 28 | match diagnostic { |
25 | 29 | BuiltinLintDiag::UnicodeTextFlow(span, content) => { |
@@ -374,11 +378,11 @@ fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Dia |
374 | 378 | | BuiltinLintDiag::MetaVariableWrongOperator |
375 | 379 | | BuiltinLintDiag::DuplicateMatcherBinding |
376 | 380 | | BuiltinLintDiag::UnknownMacroVariable(_) |
377 | | - | BuiltinLintDiag::UnusedExternCrate2 { .. } |
| 381 | + | BuiltinLintDiag::UnusedCrateDependency { .. } |
378 | 382 | | BuiltinLintDiag::WasmCAbi |
379 | 383 | | BuiltinLintDiag::IllFormedAttributeInput { .. } |
380 | 384 | | BuiltinLintDiag::InnerAttributeUnstable { .. } |
381 | | - | BuiltinLintDiag::UnknownDiagnosticAttribute => {} |
| 385 | + | BuiltinLintDiag::UnknownDiagnosticAttribute => unreachable!(), |
382 | 386 | } |
383 | 387 | } |
384 | 388 |
|
@@ -474,68 +478,34 @@ fn buffered_message(diagnostic: &BuiltinLintDiag) -> DiagMessage { |
474 | 478 | import_vis |
475 | 479 | ) |
476 | 480 | .into(), |
477 | | - BuiltinLintDiag::InvalidCrateTypeValue => fluent::lint_invalid_crate_type_value, |
478 | | - BuiltinLintDiag::MacroUseDeprecated => fluent::lint_macro_use_deprecated, |
479 | | - BuiltinLintDiag::UnusedMacroUse => fluent::lint_unused_macro_use, |
480 | | - BuiltinLintDiag::PrivateExternCrateReexport(ident) => format!( |
481 | | - "extern crate `{ident}` is private, and cannot be \ |
482 | | - re-exported (error E0365), consider declaring with \ |
483 | | - `pub`" |
484 | | - ) |
485 | | - .into(), |
486 | | - BuiltinLintDiag::UnusedLabel => fluent::lint_unused_label, |
487 | | - BuiltinLintDiag::MacroIsPrivate(ident) => format!("macro `{ident}` is private").into(), |
488 | | - BuiltinLintDiag::UnusedMacroDefinition(name) => { |
489 | | - format!("unused macro definition: `{}`", name).into() |
490 | | - } |
491 | | - BuiltinLintDiag::MacroRuleNeverUsed(n, name) => { |
492 | | - format!("{} rule of macro `{}` is never used", ordinalize(n + 1), name).into() |
493 | | - } |
494 | | - BuiltinLintDiag::UnstableFeature(msg) => msg.clone().into(), |
495 | | - BuiltinLintDiag::AvoidUsingIntelSyntax => fluent::lint_avoid_intel_syntax, |
496 | | - BuiltinLintDiag::AvoidUsingAttSyntax => fluent::lint_avoid_att_syntax, |
497 | | - BuiltinLintDiag::IncompleteInclude => fluent::lint_incomplete_include, |
498 | | - BuiltinLintDiag::UnnameableTestItems => fluent::lint_unnameable_test_items, |
499 | | - BuiltinLintDiag::DuplicateMacroAttribute => fluent::lint_duplicate_macro_attribute, |
500 | | - BuiltinLintDiag::CfgAttrNoAttributes => fluent::lint_cfg_attr_no_attributes, |
501 | | - BuiltinLintDiag::CrateTypeInCfgAttr => fluent::lint_crate_type_in_cfg_attr_deprecated, |
502 | | - BuiltinLintDiag::CrateNameInCfgAttr => fluent::lint_crate_name_in_cfg_attr_deprecated, |
503 | | - BuiltinLintDiag::MissingFragmentSpecifier => fluent::lint_missing_fragment_specifier, |
504 | | - BuiltinLintDiag::MetaVariableStillRepeating(name) => { |
505 | | - format!("variable '{name}' is still repeating at this depth").into() |
506 | | - } |
507 | | - BuiltinLintDiag::MetaVariableWrongOperator => { |
508 | | - "meta-variable repeats with different Kleene operator".into() |
509 | | - } |
510 | | - BuiltinLintDiag::DuplicateMatcherBinding => "duplicate matcher binding".into(), |
511 | | - BuiltinLintDiag::UnknownMacroVariable(name) => { |
512 | | - format!("unknown macro variable `{name}`").into() |
513 | | - } |
514 | | - BuiltinLintDiag::UnusedExternCrate2 { extern_crate, local_crate } => format!( |
515 | | - "external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`", |
516 | | - extern_crate, local_crate, extern_crate |
517 | | - ) |
518 | | - .into(), |
519 | | - BuiltinLintDiag::WasmCAbi => fluent::lint_wasm_c_abi, |
520 | | - BuiltinLintDiag::IllFormedAttributeInput { suggestions } => suggestions |
521 | | - .iter() |
522 | | - .enumerate() |
523 | | - .fold("attribute must be of the form ".to_string(), |mut acc, (i, sugg)| { |
524 | | - if i != 0 { |
525 | | - write!(acc, " or ").unwrap(); |
526 | | - } |
527 | | - write!(acc, "`{sugg}`").unwrap(); |
528 | | - acc |
529 | | - }) |
530 | | - .into(), |
531 | | - BuiltinLintDiag::InnerAttributeUnstable { is_macro } => { |
532 | | - if *is_macro { |
533 | | - fluent::lint_inner_macro_attribute_unstable |
534 | | - } else { |
535 | | - fluent::lint_custom_inner_attribute_unstable |
536 | | - } |
537 | | - } |
538 | | - BuiltinLintDiag::UnknownDiagnosticAttribute => fluent::lint_unknown_diagnostic_attribute, |
| 481 | + |
| 482 | + BuiltinLintDiag::InvalidCrateTypeValue |
| 483 | + | BuiltinLintDiag::MacroUseDeprecated |
| 484 | + | BuiltinLintDiag::UnusedMacroUse |
| 485 | + | BuiltinLintDiag::PrivateExternCrateReexport(_) |
| 486 | + | BuiltinLintDiag::UnusedLabel |
| 487 | + | BuiltinLintDiag::MacroIsPrivate(_) |
| 488 | + | BuiltinLintDiag::UnusedMacroDefinition(_) |
| 489 | + | BuiltinLintDiag::MacroRuleNeverUsed(_, _) |
| 490 | + | BuiltinLintDiag::UnstableFeature(_) |
| 491 | + | BuiltinLintDiag::AvoidUsingIntelSyntax |
| 492 | + | BuiltinLintDiag::AvoidUsingAttSyntax |
| 493 | + | BuiltinLintDiag::IncompleteInclude |
| 494 | + | BuiltinLintDiag::UnnameableTestItems |
| 495 | + | BuiltinLintDiag::DuplicateMacroAttribute |
| 496 | + | BuiltinLintDiag::CfgAttrNoAttributes |
| 497 | + | BuiltinLintDiag::CrateTypeInCfgAttr |
| 498 | + | BuiltinLintDiag::CrateNameInCfgAttr |
| 499 | + | BuiltinLintDiag::MissingFragmentSpecifier |
| 500 | + | BuiltinLintDiag::MetaVariableStillRepeating(_) |
| 501 | + | BuiltinLintDiag::MetaVariableWrongOperator |
| 502 | + | BuiltinLintDiag::DuplicateMatcherBinding |
| 503 | + | BuiltinLintDiag::UnknownMacroVariable(_) |
| 504 | + | BuiltinLintDiag::UnusedCrateDependency { .. } |
| 505 | + | BuiltinLintDiag::WasmCAbi |
| 506 | + | BuiltinLintDiag::IllFormedAttributeInput { .. } |
| 507 | + | BuiltinLintDiag::InnerAttributeUnstable { .. } |
| 508 | + | BuiltinLintDiag::UnknownDiagnosticAttribute => unreachable!(), |
539 | 509 | } |
540 | 510 | } |
541 | 511 |
|
@@ -578,49 +548,83 @@ pub(super) fn emit_buffered_lint<C: LintContext + ?Sized>( |
578 | 548 | | BuiltinLintDiag::HiddenGlobReexports { .. } |
579 | 549 | | BuiltinLintDiag::UnusedQualifications { .. } |
580 | 550 | | BuiltinLintDiag::AssociatedConstElidedLifetime { .. } |
581 | | - | BuiltinLintDiag::RedundantImportVisibility { .. } |
582 | | - | BuiltinLintDiag::InvalidCrateTypeValue |
583 | | - | BuiltinLintDiag::MacroUseDeprecated |
584 | | - | BuiltinLintDiag::UnusedMacroUse |
585 | | - | BuiltinLintDiag::PrivateExternCrateReexport(_) |
586 | | - | BuiltinLintDiag::UnusedLabel |
587 | | - | BuiltinLintDiag::MacroIsPrivate(_) |
588 | | - | BuiltinLintDiag::UnusedMacroDefinition(_) |
589 | | - | BuiltinLintDiag::MacroRuleNeverUsed(_, _) |
590 | | - | BuiltinLintDiag::UnstableFeature(_) |
591 | | - | BuiltinLintDiag::AvoidUsingIntelSyntax |
592 | | - | BuiltinLintDiag::AvoidUsingAttSyntax |
593 | | - | BuiltinLintDiag::IncompleteInclude |
594 | | - | BuiltinLintDiag::UnnameableTestItems |
595 | | - | BuiltinLintDiag::DuplicateMacroAttribute |
596 | | - | BuiltinLintDiag::CfgAttrNoAttributes |
597 | | - | BuiltinLintDiag::CrateTypeInCfgAttr |
598 | | - | BuiltinLintDiag::CrateNameInCfgAttr |
599 | | - | BuiltinLintDiag::MissingFragmentSpecifier |
600 | | - | BuiltinLintDiag::MetaVariableStillRepeating(_) |
601 | | - | BuiltinLintDiag::MetaVariableWrongOperator |
602 | | - | BuiltinLintDiag::DuplicateMatcherBinding |
603 | | - | BuiltinLintDiag::UnknownMacroVariable(_) |
604 | | - | BuiltinLintDiag::UnusedExternCrate2 { .. } |
605 | | - | BuiltinLintDiag::WasmCAbi |
606 | | - | BuiltinLintDiag::IllFormedAttributeInput { .. } |
607 | | - | BuiltinLintDiag::InnerAttributeUnstable { .. } |
608 | | - | BuiltinLintDiag::UnknownDiagnosticAttribute => { |
| 551 | + | BuiltinLintDiag::RedundantImportVisibility { .. } => { |
609 | 552 | ctx.span_lint(lint, span, buffered_message(&diagnostic), |db| { |
610 | 553 | // Now, set up surrounding context. |
611 | 554 | buffered_decorate(ctx.sess(), diagnostic, db); |
612 | 555 | }) |
613 | 556 | } |
| 557 | + BuiltinLintDiag::InvalidCrateTypeValue => { |
| 558 | + ctx.emit_span_lint(lint, span, InvalidCrateTypeValue) |
| 559 | + } |
| 560 | + BuiltinLintDiag::MacroUseDeprecated => ctx.emit_span_lint(lint, span, MacroUseDeprecated), |
| 561 | + BuiltinLintDiag::UnusedMacroUse => ctx.emit_span_lint(lint, span, UnusedMacroUse), |
| 562 | + BuiltinLintDiag::PrivateExternCrateReexport(ident) => { |
| 563 | + ctx.emit_span_lint(lint, span, PrivateExternCrateReexport { ident }) |
| 564 | + } |
| 565 | + BuiltinLintDiag::UnusedLabel => ctx.emit_span_lint(lint, span, UnusedLabel), |
| 566 | + BuiltinLintDiag::MacroIsPrivate(ident) => { |
| 567 | + ctx.emit_span_lint(lint, span, MacroIsPrivate { ident }) |
| 568 | + } |
| 569 | + BuiltinLintDiag::UnusedMacroDefinition(name) => { |
| 570 | + ctx.emit_span_lint(lint, span, UnusedMacroDefinition { name }) |
| 571 | + } |
| 572 | + BuiltinLintDiag::MacroRuleNeverUsed(n, name) => { |
| 573 | + ctx.emit_span_lint(lint, span, MacroRuleNeverUsed { n: n + 1, name }) |
| 574 | + } |
| 575 | + BuiltinLintDiag::UnstableFeature(msg) => { |
| 576 | + ctx.emit_span_lint(lint, span, UnstableFeature { msg }) |
| 577 | + } |
| 578 | + BuiltinLintDiag::AvoidUsingIntelSyntax => ctx.emit_span_lint(lint, span, AvoidIntelSyntax), |
| 579 | + BuiltinLintDiag::AvoidUsingAttSyntax => ctx.emit_span_lint(lint, span, AvoidAttSyntax), |
| 580 | + BuiltinLintDiag::IncompleteInclude => ctx.emit_span_lint(lint, span, IncompleteInclude), |
| 581 | + BuiltinLintDiag::UnnameableTestItems => ctx.emit_span_lint(lint, span, UnnameableTestItems), |
| 582 | + BuiltinLintDiag::DuplicateMacroAttribute => { |
| 583 | + ctx.emit_span_lint(lint, span, DuplicateMacroAttribute) |
| 584 | + } |
| 585 | + BuiltinLintDiag::CfgAttrNoAttributes => ctx.emit_span_lint(lint, span, CfgAttrNoAttributes), |
| 586 | + BuiltinLintDiag::CrateTypeInCfgAttr => ctx.emit_span_lint(lint, span, CrateTypeInCfgAttr), |
| 587 | + BuiltinLintDiag::CrateNameInCfgAttr => ctx.emit_span_lint(lint, span, CrateNameInCfgAttr), |
| 588 | + BuiltinLintDiag::MissingFragmentSpecifier => { |
| 589 | + ctx.emit_span_lint(lint, span, MissingFragmentSpecifier) |
| 590 | + } |
| 591 | + BuiltinLintDiag::MetaVariableStillRepeating(name) => { |
| 592 | + ctx.emit_span_lint(lint, span, MetaVariableStillRepeating { name }) |
| 593 | + } |
| 594 | + BuiltinLintDiag::MetaVariableWrongOperator => { |
| 595 | + ctx.emit_span_lint(lint, span, MetaVariableWrongOperator) |
| 596 | + } |
| 597 | + BuiltinLintDiag::DuplicateMatcherBinding => { |
| 598 | + ctx.emit_span_lint(lint, span, DuplicateMatcherBinding) |
| 599 | + } |
| 600 | + BuiltinLintDiag::UnknownMacroVariable(name) => { |
| 601 | + ctx.emit_span_lint(lint, span, UnknownMacroVariable { name }) |
| 602 | + } |
| 603 | + BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => { |
| 604 | + ctx.emit_span_lint(lint, span, UnusedCrateDependency { extern_crate, local_crate }) |
| 605 | + } |
| 606 | + BuiltinLintDiag::WasmCAbi => ctx.emit_span_lint(lint, span, WasmCAbi), |
| 607 | + BuiltinLintDiag::IllFormedAttributeInput { suggestions } => ctx.emit_span_lint( |
| 608 | + lint, |
| 609 | + span, |
| 610 | + IllFormedAttributeInput { |
| 611 | + num_suggestions: suggestions.len(), |
| 612 | + suggestions: DiagArgValue::StrListSepByAnd( |
| 613 | + suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(), |
| 614 | + ), |
| 615 | + }, |
| 616 | + ), |
| 617 | + BuiltinLintDiag::InnerAttributeUnstable { is_macro } => ctx.emit_span_lint( |
| 618 | + lint, |
| 619 | + span, |
| 620 | + if is_macro { |
| 621 | + InnerAttributeUnstable::InnerMacroAttribute |
| 622 | + } else { |
| 623 | + InnerAttributeUnstable::CustomInnerAttribute |
| 624 | + }, |
| 625 | + ), |
| 626 | + BuiltinLintDiag::UnknownDiagnosticAttribute => { |
| 627 | + ctx.emit_span_lint(lint, span, UnknownDiagnosticAttribute) |
| 628 | + } |
614 | 629 | } |
615 | 630 | } |
616 | | - |
617 | | -/// Convert the given number into the corresponding ordinal |
618 | | -pub(crate) fn ordinalize(v: usize) -> String { |
619 | | - let suffix = match ((11..=13).contains(&(v % 100)), v % 10) { |
620 | | - (false, 1) => "st", |
621 | | - (false, 2) => "nd", |
622 | | - (false, 3) => "rd", |
623 | | - _ => "th", |
624 | | - }; |
625 | | - format!("{v}{suffix}") |
626 | | -} |
|
0 commit comments