]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/errors.rs
add: `lints` for `errors.rs`
[rust.git] / compiler / rustc_lint / src / errors.rs
1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3 use rustc_errors::{
4     fluent, AddToDiagnostic, Diagnostic, ErrorGuaranteed, Handler, IntoDiagnostic,
5     SubdiagnosticMessage,
6 };
7 use rustc_macros::{Diagnostic, Subdiagnostic};
8 use rustc_session::lint::Level;
9 use rustc_span::{Span, Symbol};
10
11 #[derive(Diagnostic)]
12 #[diag(lint_overruled_attribute, code = "E0453")]
13 pub struct OverruledAttribute {
14     #[primary_span]
15     pub span: Span,
16     #[label]
17     pub overruled: Span,
18     pub lint_level: String,
19     pub lint_source: Symbol,
20     #[subdiagnostic]
21     pub sub: OverruledAttributeSub,
22 }
23 //
24 pub enum OverruledAttributeSub {
25     DefaultSource { id: String },
26     NodeSource { span: Span, reason: Option<Symbol> },
27     CommandLineSource,
28 }
29
30 impl AddToDiagnostic for OverruledAttributeSub {
31     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
32     where
33         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
34     {
35         match self {
36             OverruledAttributeSub::DefaultSource { id } => {
37                 diag.note(fluent::lint_default_source);
38                 diag.set_arg("id", id);
39             }
40             OverruledAttributeSub::NodeSource { span, reason } => {
41                 diag.span_label(span, fluent::lint_node_source);
42                 if let Some(rationale) = reason {
43                     #[allow(rustc::untranslatable_diagnostic)]
44                     diag.note(rationale.as_str());
45                 }
46             }
47             OverruledAttributeSub::CommandLineSource => {
48                 diag.note(fluent::lint_command_line_source);
49             }
50         }
51     }
52 }
53
54 #[derive(Diagnostic)]
55 #[diag(lint_malformed_attribute, code = "E0452")]
56 pub struct MalformedAttribute {
57     #[primary_span]
58     pub span: Span,
59     #[subdiagnostic]
60     pub sub: MalformedAttributeSub,
61 }
62
63 #[derive(Subdiagnostic)]
64 pub enum MalformedAttributeSub {
65     #[label(lint_bad_attribute_argument)]
66     BadAttributeArgument(#[primary_span] Span),
67     #[label(lint_reason_must_be_string_literal)]
68     ReasonMustBeStringLiteral(#[primary_span] Span),
69     #[label(lint_reason_must_come_last)]
70     ReasonMustComeLast(#[primary_span] Span),
71 }
72
73 #[derive(Diagnostic)]
74 #[diag(lint_unknown_tool_in_scoped_lint, code = "E0710")]
75 pub struct UnknownToolInScopedLint {
76     #[primary_span]
77     pub span: Option<Span>,
78     pub tool_name: Symbol,
79     pub lint_name: String,
80     #[help]
81     pub is_nightly_build: Option<()>,
82 }
83
84 #[derive(Diagnostic)]
85 #[diag(lint_builtin_ellipsis_inclusive_range_patterns, code = "E0783")]
86 pub struct BuiltinEllpisisInclusiveRangePatterns {
87     #[primary_span]
88     pub span: Span,
89     #[suggestion(style = "short", code = "{replace}", applicability = "machine-applicable")]
90     pub suggestion: Span,
91     pub replace: String,
92 }
93
94 #[derive(Subdiagnostic)]
95 #[note(lint_requested_level)]
96 pub struct RequestedLevel {
97     pub level: Level,
98     pub lint_name: String,
99 }
100
101 #[derive(Diagnostic)]
102 #[diag(lint_unsupported_group, code = "E0602")]
103 pub struct UnsupportedGroup {
104     pub lint_group: String,
105 }
106
107 pub struct CheckNameUnknown {
108     pub lint_name: String,
109     pub suggestion: Option<Symbol>,
110     pub sub: RequestedLevel,
111 }
112
113 impl IntoDiagnostic<'_> for CheckNameUnknown {
114     fn into_diagnostic(
115         self,
116         handler: &Handler,
117     ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
118         let mut diag = handler.struct_err(fluent::lint_check_name_unknown);
119         diag.code(rustc_errors::error_code!(E0602));
120         if let Some(suggestion) = self.suggestion {
121             diag.help(fluent::help);
122             diag.set_arg("suggestion", suggestion);
123         }
124         diag.set_arg("lint_name", self.lint_name);
125         diag.subdiagnostic(self.sub);
126         diag
127     }
128 }
129
130 #[derive(Diagnostic)]
131 #[diag(lint_check_name_unknown_tool, code = "E0602")]
132 pub struct CheckNameUnknownTool {
133     pub tool_name: Symbol,
134     #[subdiagnostic]
135     pub sub: RequestedLevel,
136 }
137
138 #[derive(Diagnostic)]
139 #[diag(lint_check_name_warning)]
140 pub struct CheckNameWarning {
141     pub msg: String,
142     #[subdiagnostic]
143     pub sub: RequestedLevel,
144 }
145
146 #[derive(Diagnostic)]
147 #[diag(lint_check_name_deprecated)]
148 pub struct CheckNameDeprecated {
149     pub lint_name: String,
150     pub new_name: String,
151     #[subdiagnostic]
152     pub sub: RequestedLevel,
153 }