Skip to content

Commit 0094065

Browse files
committed
Convert early lints to diag structs
1 parent 1dbc890 commit 0094065

File tree

9 files changed

+301
-161
lines changed

9 files changed

+301
-161
lines changed

compiler/rustc_lint/messages.ftl

+30
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ lint_dropping_references = calls to `std::mem::drop` with a reference instead of
235235
lint_duplicate_macro_attribute =
236236
duplicated attribute
237237
238+
lint_duplicate_matcher_binding = duplicate matcher binding
239+
238240
lint_elided_lifetime_not_allowed = `'_` cannot be used here
239241
240242
lint_enum_intrinsics_mem_discriminant =
@@ -312,6 +314,11 @@ lint_identifier_uncommon_codepoints = identifier contains {$codepoints_len ->
312314
313315
lint_ignored_unless_crate_specified = {$level}({$name}) is ignored unless specified at crate level
314316
317+
lint_ill_formed_attribute_input = {$num_suggestions ->
318+
[1] attribute must be of the form {$suggestions}
319+
*[other] valid forms for the attribute are {$suggestions}
320+
}
321+
315322
lint_improper_ctypes = `extern` {$desc} uses type `{$ty}`, which is not FFI-safe
316323
.label = not FFI-safe
317324
.note = the type is defined here
@@ -415,6 +422,16 @@ lint_lintpass_by_hand = implementing `LintPass` by hand
415422
lint_macro_expanded_macro_exports_accessed_by_absolute_paths =
416423
macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
417424
425+
lint_macro_is_private = macro `{$ident}` is private
426+
427+
lint_macro_rule_never_used = ${NUMBER($n, type: "ordinal") ->
428+
[1] first
429+
[one] {$n}st
430+
[two] {$n}nd
431+
[few] {$n}rd
432+
*[other] {$n}th
433+
} rule of macro `{$name}` is never used
434+
418435
lint_macro_use_deprecated =
419436
deprecated `#[macro_use]` attribute used to import macros should be replaced at use sites with a `use` item to import the macro instead
420437
@@ -427,6 +444,10 @@ lint_map_unit_fn = `Iterator::map` call that discard the iterator's values
427444
.map_label = after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
428445
.suggestion = you might have meant to use `Iterator::for_each`
429446
447+
lint_metavariable_still_repeating = variable '{$name}' is still repeating at this depth
448+
449+
lint_metavariable_wrong_operator = meta-variable repeats with different Kleene operator
450+
430451
lint_missing_fragment_specifier = missing fragment specifier
431452
432453
lint_mixed_script_confusables =
@@ -583,6 +604,9 @@ lint_pattern_in_bodiless = patterns aren't allowed in functions without bodies
583604
lint_pattern_in_foreign = patterns aren't allowed in foreign function declarations
584605
.label = pattern not allowed in foreign function
585606
607+
lint_private_extern_crate_reexport =
608+
extern crate `{$ident}` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
609+
586610
lint_proc_macro_back_compat_rental = using an old version of `rental`
587611
588612
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
@@ -681,6 +705,8 @@ lint_unknown_lint =
681705
*[false] did you mean: `{$replace}`
682706
}
683707
708+
lint_unknown_macro_variable = unknown macro variable `{$name}`
709+
684710
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
685711
.help = add `#![register_tool({$tool_name})]` to the crate root
686712
@@ -711,6 +737,8 @@ lint_unused_coroutine =
711737
}{$post} that must be used
712738
.note = coroutines are lazy and do nothing unless resumed
713739
740+
lint_unused_crate_dependency = external crate `{$extern_crate}` unused in `{$local_crate}`: remove the dependency or add `use {$extern_crate} as _;`
741+
714742
lint_unused_def = unused {$pre}`{$def}`{$post} that must be used
715743
.suggestion = use `let _ = ...` to ignore the resulting value
716744
@@ -723,6 +751,8 @@ lint_unused_import_braces = braces around {$node} is unnecessary
723751
724752
lint_unused_label = unused label
725753
754+
lint_unused_macro_definition = unused macro definition: `{$name}`
755+
726756
lint_unused_macro_use = unused `#[macro_use]` import
727757
728758
lint_unused_op = unused {$op} that must be used

compiler/rustc_lint/src/context/diagnostics.rs

+113-109
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33

44
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
55
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,
77
};
88
use rustc_errors::{Applicability, SuggestionStyle};
99
use rustc_middle::middle::stability;
1010
use rustc_session::lint::{BuiltinLintDiag, Lint};
1111
use rustc_session::Session;
1212
use rustc_span::BytePos;
1313

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+
};
1623
use crate::{fluent_generated as fluent, LintContext};
1724

1825
mod check_cfg;
1926

20-
#[cfg(test)]
21-
mod tests;
22-
2327
fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
2428
match diagnostic {
2529
BuiltinLintDiag::UnicodeTextFlow(span, content) => {
@@ -374,11 +378,11 @@ fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Dia
374378
| BuiltinLintDiag::MetaVariableWrongOperator
375379
| BuiltinLintDiag::DuplicateMatcherBinding
376380
| BuiltinLintDiag::UnknownMacroVariable(_)
377-
| BuiltinLintDiag::UnusedExternCrate2 { .. }
381+
| BuiltinLintDiag::UnusedCrateDependency { .. }
378382
| BuiltinLintDiag::WasmCAbi
379383
| BuiltinLintDiag::IllFormedAttributeInput { .. }
380384
| BuiltinLintDiag::InnerAttributeUnstable { .. }
381-
| BuiltinLintDiag::UnknownDiagnosticAttribute => {}
385+
| BuiltinLintDiag::UnknownDiagnosticAttribute => unreachable!(),
382386
}
383387
}
384388

@@ -474,68 +478,34 @@ fn buffered_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
474478
import_vis
475479
)
476480
.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!(),
539509
}
540510
}
541511

@@ -578,49 +548,83 @@ pub(super) fn emit_buffered_lint<C: LintContext + ?Sized>(
578548
| BuiltinLintDiag::HiddenGlobReexports { .. }
579549
| BuiltinLintDiag::UnusedQualifications { .. }
580550
| 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 { .. } => {
609552
ctx.span_lint(lint, span, buffered_message(&diagnostic), |db| {
610553
// Now, set up surrounding context.
611554
buffered_decorate(ctx.sess(), diagnostic, db);
612555
})
613556
}
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+
}
614629
}
615630
}
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-
}

compiler/rustc_lint/src/context/diagnostics/tests.rs

-40
This file was deleted.

0 commit comments

Comments
 (0)