1 use std::{io::Error, path::Path};
4 use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
5 use rustc_hir::{self as hir, ExprKind, Target};
6 use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
7 use rustc_span::{Span, Symbol};
9 #[derive(LintDiagnostic)]
10 #[diag(passes::outer_crate_level_attr)]
11 pub struct OuterCrateLevelAttr;
13 #[derive(LintDiagnostic)]
14 #[diag(passes::inner_crate_level_attr)]
15 pub struct InnerCrateLevelAttr;
17 #[derive(LintDiagnostic)]
18 #[diag(passes::ignored_attr_with_macro)]
19 pub struct IgnoredAttrWithMacro<'a> {
23 #[derive(LintDiagnostic)]
24 #[diag(passes::ignored_attr)]
25 pub struct IgnoredAttr<'a> {
29 #[derive(LintDiagnostic)]
30 #[diag(passes::inline_ignored_function_prototype)]
31 pub struct IgnoredInlineAttrFnProto;
33 #[derive(LintDiagnostic)]
34 #[diag(passes::inline_ignored_constants)]
37 pub struct IgnoredInlineAttrConstants;
40 #[diag(passes::inline_not_fn_or_closure, code = "E0518")]
41 pub struct InlineNotFnOrClosure {
48 #[derive(LintDiagnostic)]
49 #[diag(passes::no_coverage_ignored_function_prototype)]
50 pub struct IgnoredNoCoverageFnProto;
52 #[derive(LintDiagnostic)]
53 #[diag(passes::no_coverage_propagate)]
54 pub struct IgnoredNoCoveragePropagate;
56 #[derive(LintDiagnostic)]
57 #[diag(passes::no_coverage_fn_defn)]
58 pub struct IgnoredNoCoverageFnDefn;
61 #[diag(passes::no_coverage_not_coverable, code = "E0788")]
62 pub struct IgnoredNoCoverageNotCoverable {
70 #[diag(passes::should_be_applied_to_fn)]
71 pub struct AttrShouldBeAppliedToFn {
79 #[diag(passes::naked_tracked_caller, code = "E0736")]
80 pub struct NakedTrackedCaller {
86 #[diag(passes::should_be_applied_to_fn, code = "E0739")]
87 pub struct TrackedCallerWrongLocation {
95 #[diag(passes::should_be_applied_to_struct_enum, code = "E0701")]
96 pub struct NonExhaustiveWrongLocation {
103 #[derive(Diagnostic)]
104 #[diag(passes::should_be_applied_to_trait)]
105 pub struct AttrShouldBeAppliedToTrait {
112 #[derive(LintDiagnostic)]
113 #[diag(passes::target_feature_on_statement)]
114 pub struct TargetFeatureOnStatement;
116 #[derive(Diagnostic)]
117 #[diag(passes::should_be_applied_to_static)]
118 pub struct AttrShouldBeAppliedToStatic {
125 #[derive(Diagnostic)]
126 #[diag(passes::doc_expect_str)]
127 pub struct DocExpectStr<'a> {
130 pub attr_name: &'a str,
133 #[derive(Diagnostic)]
134 #[diag(passes::doc_alias_empty)]
135 pub struct DocAliasEmpty<'a> {
138 pub attr_str: &'a str,
141 #[derive(Diagnostic)]
142 #[diag(passes::doc_alias_bad_char)]
143 pub struct DocAliasBadChar<'a> {
146 pub attr_str: &'a str,
150 #[derive(Diagnostic)]
151 #[diag(passes::doc_alias_start_end)]
152 pub struct DocAliasStartEnd<'a> {
155 pub attr_str: &'a str,
158 #[derive(Diagnostic)]
159 #[diag(passes::doc_alias_bad_location)]
160 pub struct DocAliasBadLocation<'a> {
163 pub attr_str: &'a str,
164 pub location: &'a str,
167 #[derive(Diagnostic)]
168 #[diag(passes::doc_alias_not_an_alias)]
169 pub struct DocAliasNotAnAlias<'a> {
172 pub attr_str: &'a str,
175 #[derive(LintDiagnostic)]
176 #[diag(passes::doc_alias_duplicated)]
177 pub struct DocAliasDuplicated {
179 pub first_defn: Span,
182 #[derive(Diagnostic)]
183 #[diag(passes::doc_alias_not_string_literal)]
184 pub struct DocAliasNotStringLiteral {
189 #[derive(Diagnostic)]
190 #[diag(passes::doc_alias_malformed)]
191 pub struct DocAliasMalformed {
196 #[derive(Diagnostic)]
197 #[diag(passes::doc_keyword_empty_mod)]
198 pub struct DocKeywordEmptyMod {
203 #[derive(Diagnostic)]
204 #[diag(passes::doc_keyword_not_mod)]
205 pub struct DocKeywordNotMod {
210 #[derive(Diagnostic)]
211 #[diag(passes::doc_keyword_invalid_ident)]
212 pub struct DocKeywordInvalidIdent {
215 pub doc_keyword: Symbol,
218 #[derive(Diagnostic)]
219 #[diag(passes::doc_fake_variadic_not_valid)]
220 pub struct DocFakeVariadicNotValid {
225 #[derive(Diagnostic)]
226 #[diag(passes::doc_keyword_only_impl)]
227 pub struct DocKeywordOnlyImpl {
232 #[derive(Diagnostic)]
233 #[diag(passes::doc_inline_conflict)]
235 pub struct DocKeywordConflict {
237 pub spans: MultiSpan,
240 #[derive(LintDiagnostic)]
241 #[diag(passes::doc_inline_only_use)]
243 pub struct DocInlineOnlyUse {
246 #[label(passes::not_a_use_item_label)]
247 pub item_span: Option<Span>,
250 #[derive(Diagnostic)]
251 #[diag(passes::doc_attr_not_crate_level)]
252 pub struct DocAttrNotCrateLevel<'a> {
255 pub attr_name: &'a str,
258 #[derive(LintDiagnostic)]
259 #[diag(passes::doc_test_unknown)]
260 pub struct DocTestUnknown {
264 #[derive(LintDiagnostic)]
265 #[diag(passes::doc_test_takes_list)]
266 pub struct DocTestTakesList;
268 #[derive(LintDiagnostic)]
269 #[diag(passes::doc_primitive)]
270 pub struct DocPrimitive;
272 #[derive(LintDiagnostic)]
273 #[diag(passes::doc_test_unknown_any)]
274 pub struct DocTestUnknownAny {
278 #[derive(LintDiagnostic)]
279 #[diag(passes::doc_test_unknown_spotlight)]
281 #[note(passes::no_op_note)]
282 pub struct DocTestUnknownSpotlight {
284 #[suggestion_short(applicability = "machine-applicable", code = "notable_trait")]
288 #[derive(LintDiagnostic)]
289 #[diag(passes::doc_test_unknown_include)]
290 pub struct DocTestUnknownInclude {
293 pub inner: &'static str,
294 #[suggestion(code = "#{inner}[doc = include_str!(\"{value}\")]")]
295 pub sugg: (Span, Applicability),
298 #[derive(LintDiagnostic)]
299 #[diag(passes::doc_invalid)]
300 pub struct DocInvalid;
302 #[derive(Diagnostic)]
303 #[diag(passes::pass_by_value)]
304 pub struct PassByValue {
311 #[derive(Diagnostic)]
312 #[diag(passes::allow_incoherent_impl)]
313 pub struct AllowIncoherentImpl {
320 #[derive(Diagnostic)]
321 #[diag(passes::has_incoherent_inherent_impl)]
322 pub struct HasIncoherentInherentImpl {
329 #[derive(LintDiagnostic)]
330 #[diag(passes::must_use_async)]
331 pub struct MustUseAsync {
336 #[derive(LintDiagnostic)]
337 #[diag(passes::must_use_no_effect)]
338 pub struct MustUseNoEffect {
339 pub article: &'static str,
340 pub target: rustc_hir::Target,
343 #[derive(Diagnostic)]
344 #[diag(passes::must_not_suspend)]
345 pub struct MustNotSuspend {
352 #[derive(LintDiagnostic)]
353 #[diag(passes::cold)]
360 #[derive(LintDiagnostic)]
361 #[diag(passes::link)]
365 pub span: Option<Span>,
368 #[derive(LintDiagnostic)]
369 #[diag(passes::link_name)]
371 pub struct LinkName<'a> {
373 pub attr_span: Option<Span>,
379 #[derive(Diagnostic)]
380 #[diag(passes::no_link)]
388 #[derive(Diagnostic)]
389 #[diag(passes::export_name)]
390 pub struct ExportName {
397 #[derive(Diagnostic)]
398 #[diag(passes::rustc_layout_scalar_valid_range_not_struct)]
399 pub struct RustcLayoutScalarValidRangeNotStruct {
406 #[derive(Diagnostic)]
407 #[diag(passes::rustc_layout_scalar_valid_range_arg)]
408 pub struct RustcLayoutScalarValidRangeArg {
413 #[derive(Diagnostic)]
414 #[diag(passes::rustc_legacy_const_generics_only)]
415 pub struct RustcLegacyConstGenericsOnly {
419 pub param_span: Span,
422 #[derive(Diagnostic)]
423 #[diag(passes::rustc_legacy_const_generics_index)]
424 pub struct RustcLegacyConstGenericsIndex {
428 pub generics_span: Span,
431 #[derive(Diagnostic)]
432 #[diag(passes::rustc_legacy_const_generics_index_exceed)]
433 pub struct RustcLegacyConstGenericsIndexExceed {
437 pub arg_count: usize,
440 #[derive(Diagnostic)]
441 #[diag(passes::rustc_legacy_const_generics_index_negative)]
442 pub struct RustcLegacyConstGenericsIndexNegative {
444 pub invalid_args: Vec<Span>,
447 #[derive(Diagnostic)]
448 #[diag(passes::rustc_dirty_clean)]
449 pub struct RustcDirtyClean {
454 #[derive(LintDiagnostic)]
455 #[diag(passes::link_section)]
457 pub struct LinkSection {
462 #[derive(LintDiagnostic)]
463 #[diag(passes::no_mangle_foreign)]
466 pub struct NoMangleForeign {
469 #[suggestion(code = "", applicability = "machine-applicable")]
471 pub foreign_item_kind: &'static str,
474 #[derive(LintDiagnostic)]
475 #[diag(passes::no_mangle)]
477 pub struct NoMangle {
482 #[derive(Diagnostic)]
483 #[diag(passes::repr_ident, code = "E0565")]
484 pub struct ReprIdent {
489 #[derive(LintDiagnostic)]
490 #[diag(passes::repr_conflicting, code = "E0566")]
491 pub struct ReprConflicting;
493 #[derive(Diagnostic)]
494 #[diag(passes::used_static)]
495 pub struct UsedStatic {
500 #[derive(Diagnostic)]
501 #[diag(passes::used_compiler_linker)]
502 pub struct UsedCompilerLinker {
504 pub spans: Vec<Span>,
507 #[derive(Diagnostic)]
508 #[diag(passes::allow_internal_unstable)]
509 pub struct AllowInternalUnstable {
516 #[derive(Diagnostic)]
517 #[diag(passes::debug_visualizer_placement)]
518 pub struct DebugVisualizerPlacement {
523 #[derive(Diagnostic)]
524 #[diag(passes::debug_visualizer_invalid)]
525 #[note(passes::note_1)]
526 #[note(passes::note_2)]
527 #[note(passes::note_3)]
528 pub struct DebugVisualizerInvalid {
533 #[derive(Diagnostic)]
534 #[diag(passes::debug_visualizer_unreadable)]
535 pub struct DebugVisualizerUnreadable<'a> {
542 #[derive(Diagnostic)]
543 #[diag(passes::rustc_allow_const_fn_unstable)]
544 pub struct RustcAllowConstFnUnstable {
551 #[derive(Diagnostic)]
552 #[diag(passes::rustc_std_internal_symbol)]
553 pub struct RustcStdInternalSymbol {
560 #[derive(Diagnostic)]
561 #[diag(passes::const_trait)]
562 pub struct ConstTrait {
567 #[derive(Diagnostic)]
568 #[diag(passes::link_ordinal)]
569 pub struct LinkOrdinal {
574 #[derive(Diagnostic)]
575 #[diag(passes::stability_promotable)]
576 pub struct StabilityPromotable {
581 #[derive(LintDiagnostic)]
582 #[diag(passes::deprecated)]
583 pub struct Deprecated;
585 #[derive(LintDiagnostic)]
586 #[diag(passes::macro_use)]
587 pub struct MacroUse {
591 #[derive(LintDiagnostic)]
592 #[diag(passes::macro_export)]
593 pub struct MacroExport;
595 #[derive(LintDiagnostic)]
596 #[diag(passes::plugin_registrar)]
597 pub struct PluginRegistrar;
599 #[derive(Subdiagnostic)]
600 pub enum UnusedNote {
601 #[note(passes::unused_empty_lints_note)]
602 EmptyList { name: Symbol },
603 #[note(passes::unused_no_lints_note)]
604 NoLints { name: Symbol },
605 #[note(passes::unused_default_method_body_const_note)]
606 DefaultMethodBodyConst,
609 #[derive(LintDiagnostic)]
610 #[diag(passes::unused)]
612 #[suggestion(code = "", applicability = "machine-applicable")]
615 pub note: UnusedNote,
618 #[derive(Diagnostic)]
619 #[diag(passes::non_exported_macro_invalid_attrs, code = "E0518")]
620 pub struct NonExportedMacroInvalidAttrs {
626 #[derive(LintDiagnostic)]
627 #[diag(passes::unused_duplicate)]
628 pub struct UnusedDuplicate {
629 #[suggestion(code = "", applicability = "machine-applicable")]
634 pub warning: Option<()>,
637 #[derive(Diagnostic)]
638 #[diag(passes::unused_multiple)]
639 pub struct UnusedMultiple {
641 #[suggestion(code = "", applicability = "machine-applicable")]
648 #[derive(Diagnostic)]
649 #[diag(passes::rustc_lint_opt_ty)]
650 pub struct RustcLintOptTy {
657 #[derive(Diagnostic)]
658 #[diag(passes::rustc_lint_opt_deny_field_access)]
659 pub struct RustcLintOptDenyFieldAccess {
666 #[derive(Diagnostic)]
667 #[diag(passes::collapse_debuginfo)]
668 pub struct CollapseDebuginfo {
675 #[derive(LintDiagnostic)]
676 #[diag(passes::deprecated_annotation_has_no_effect)]
677 pub struct DeprecatedAnnotationHasNoEffect {
678 #[suggestion(applicability = "machine-applicable", code = "")]
682 #[derive(Diagnostic)]
683 #[diag(passes::unknown_external_lang_item, code = "E0264")]
684 pub struct UnknownExternLangItem {
687 pub lang_item: Symbol,
690 #[derive(Diagnostic)]
691 #[diag(passes::missing_panic_handler)]
692 pub struct MissingPanicHandler;
694 #[derive(Diagnostic)]
695 #[diag(passes::alloc_func_required)]
696 pub struct AllocFuncRequired;
698 #[derive(Diagnostic)]
699 #[diag(passes::missing_alloc_error_handler)]
700 pub struct MissingAllocErrorHandler;
702 #[derive(Diagnostic)]
703 #[diag(passes::missing_lang_item)]
706 pub struct MissingLangItem {
710 #[derive(Diagnostic)]
711 #[diag(passes::lang_item_on_incorrect_target, code = "E0718")]
712 pub struct LangItemOnIncorrectTarget {
717 pub expected_target: Target,
718 pub actual_target: Target,
721 #[derive(Diagnostic)]
722 #[diag(passes::unknown_lang_item, code = "E0522")]
723 pub struct UnknownLangItem {
730 pub struct InvalidAttrAtCrateLevel {
732 pub snippet: Option<String>,
736 impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
739 handler: &'_ rustc_errors::Handler,
740 ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
742 handler.struct_err(rustc_errors::fluent::passes::invalid_attr_at_crate_level);
743 diag.set_span(self.span);
744 diag.set_arg("name", self.name);
745 // Only emit an error with a suggestion if we can create a string out
746 // of the attribute span
747 if let Some(src) = self.snippet {
748 let replacement = src.replace("#!", "#");
749 diag.span_suggestion_verbose(
751 rustc_errors::fluent::passes::suggestion,
753 rustc_errors::Applicability::MachineApplicable,
760 #[derive(Diagnostic)]
761 #[diag(passes::duplicate_diagnostic_item)]
762 pub struct DuplicateDiagnosticItem {
768 #[derive(Diagnostic)]
769 #[diag(passes::duplicate_diagnostic_item_in_crate)]
770 pub struct DuplicateDiagnosticItemInCrate {
771 #[note(passes::diagnostic_item_first_defined)]
772 pub span: Option<Span>,
773 pub orig_crate_name: Symbol,
775 pub have_orig_crate_name: Option<()>,
776 pub crate_name: Symbol,
780 #[derive(Diagnostic)]
788 #[derive(Diagnostic)]
789 #[diag(passes::align)]
796 #[derive(Diagnostic)]
797 #[diag(passes::size)]
804 #[derive(Diagnostic)]
805 #[diag(passes::homogeneous_aggregate)]
806 pub struct HomogeneousAggregate {
809 pub homogeneous_aggregate: String,
812 #[derive(Diagnostic)]
813 #[diag(passes::layout_of)]
814 pub struct LayoutOf {
817 pub normalized_ty: String,
818 pub ty_layout: String,
821 #[derive(Diagnostic)]
822 #[diag(passes::unrecognized_field)]
823 pub struct UnrecognizedField {
829 #[derive(Diagnostic)]
830 #[diag(passes::layout)]
834 pub layout_error: String,
837 #[derive(Diagnostic)]
838 #[diag(passes::feature_stable_twice, code = "E0711")]
839 pub struct FeatureStableTwice {
844 pub prev_since: Symbol,
847 #[derive(Diagnostic)]
848 #[diag(passes::feature_previously_declared, code = "E0711")]
849 pub struct FeaturePreviouslyDeclared<'a, 'b> {
853 pub declared: &'a str,
854 pub prev_declared: &'b str,
857 #[derive(Diagnostic)]
858 #[diag(passes::expr_not_allowed_in_context, code = "E0744")]
859 pub struct ExprNotAllowedInContext<'a> {
863 pub context: &'a str,
866 #[derive(Diagnostic)]
867 #[diag(passes::const_impl_const_trait)]
868 pub struct ConstImplConstTrait {
875 pub struct BreakNonLoop<'a> {
877 pub head: Option<Span>,
879 pub suggestion: String,
880 pub loop_label: Option<Label>,
881 pub break_label: Option<Label>,
882 pub break_expr_kind: &'a ExprKind<'a>,
883 pub break_expr_span: Span,
886 impl<'a> IntoDiagnostic<'_> for BreakNonLoop<'a> {
889 handler: &rustc_errors::Handler,
890 ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
891 let mut diag = handler.struct_span_err_with_code(
893 rustc_errors::fluent::passes::break_non_loop,
896 diag.set_arg("kind", self.kind);
897 diag.span_label(self.span, rustc_errors::fluent::passes::label);
898 if let Some(head) = self.head {
899 diag.span_label(head, rustc_errors::fluent::passes::label2);
901 diag.span_suggestion(
903 rustc_errors::fluent::passes::suggestion,
905 Applicability::MaybeIncorrect,
907 if let (Some(label), None) = (self.loop_label, self.break_label) {
908 match self.break_expr_kind {
909 ExprKind::Path(hir::QPath::Resolved(
911 hir::Path { segments: [segment], res: hir::def::Res::Err, .. },
912 )) if label.ident.to_string() == format!("'{}", segment.ident) => {
913 // This error is redundant, we will have already emitted a
914 // suggestion to use the label when `segment` wasn't found
915 // (hence the `Res::Err` check).
919 diag.span_suggestion(
920 self.break_expr_span,
921 rustc_errors::fluent::passes::break_expr_suggestion,
923 Applicability::MaybeIncorrect,
932 #[derive(Diagnostic)]
933 #[diag(passes::continue_labeled_block, code = "E0696")]
934 pub struct ContinueLabeledBlock {
938 #[label(passes::block_label)]
939 pub block_span: Span,
942 #[derive(Diagnostic)]
943 #[diag(passes::break_inside_closure, code = "E0267")]
944 pub struct BreakInsideClosure<'a> {
948 #[label(passes::closure_label)]
949 pub closure_span: Span,
953 #[derive(Diagnostic)]
954 #[diag(passes::break_inside_async_block, code = "E0267")]
955 pub struct BreakInsideAsyncBlock<'a> {
959 #[label(passes::async_block_label)]
960 pub closure_span: Span,
964 #[derive(Diagnostic)]
965 #[diag(passes::outside_loop, code = "E0268")]
966 pub struct OutsideLoop<'a> {
973 #[derive(Diagnostic)]
974 #[diag(passes::unlabeled_in_labeled_block, code = "E0695")]
975 pub struct UnlabeledInLabeledBlock<'a> {
979 pub cf_type: &'a str,
982 #[derive(Diagnostic)]
983 #[diag(passes::unlabeled_cf_in_while_condition, code = "E0590")]
984 pub struct UnlabeledCfInWhileCondition<'a> {
988 pub cf_type: &'a str,