]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lint/src/errors.rs
Rollup merge of #101245 - GuillaumeGomez:remove-unneeded-where-whitespace, r=notriddle
[rust.git] / compiler / rustc_lint / src / errors.rs
1 use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed};
2 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3 use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic};
4 use rustc_span::{Span, Symbol};
5
6 #[derive(SessionDiagnostic)]
7 #[diag(lint::overruled_attribute, code = "E0453")]
8 pub struct OverruledAttribute {
9     #[primary_span]
10     pub span: Span,
11     #[label]
12     pub overruled: Span,
13     pub lint_level: String,
14     pub lint_source: Symbol,
15     #[subdiagnostic]
16     pub sub: OverruledAttributeSub,
17 }
18 //
19 pub enum OverruledAttributeSub {
20     DefaultSource { id: String },
21     NodeSource { span: Span, reason: Option<Symbol> },
22     CommandLineSource,
23 }
24
25 impl AddSubdiagnostic for OverruledAttributeSub {
26     fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
27         match self {
28             OverruledAttributeSub::DefaultSource { id } => {
29                 diag.note(fluent::lint::default_source);
30                 diag.set_arg("id", id);
31             }
32             OverruledAttributeSub::NodeSource { span, reason } => {
33                 diag.span_label(span, fluent::lint::node_source);
34                 if let Some(rationale) = reason {
35                     diag.note(rationale.as_str());
36                 }
37             }
38             OverruledAttributeSub::CommandLineSource => {
39                 diag.note(fluent::lint::command_line_source);
40             }
41         }
42     }
43 }
44
45 #[derive(SessionDiagnostic)]
46 #[diag(lint::malformed_attribute, code = "E0452")]
47 pub struct MalformedAttribute {
48     #[primary_span]
49     pub span: Span,
50     #[subdiagnostic]
51     pub sub: MalformedAttributeSub,
52 }
53
54 #[derive(SessionSubdiagnostic)]
55 pub enum MalformedAttributeSub {
56     #[label(lint::bad_attribute_argument)]
57     BadAttributeArgument(#[primary_span] Span),
58     #[label(lint::reason_must_be_string_literal)]
59     ReasonMustBeStringLiteral(#[primary_span] Span),
60     #[label(lint::reason_must_come_last)]
61     ReasonMustComeLast(#[primary_span] Span),
62 }
63
64 #[derive(SessionDiagnostic)]
65 #[diag(lint::unknown_tool_in_scoped_lint, code = "E0710")]
66 pub struct UnknownToolInScopedLint {
67     #[primary_span]
68     pub span: Option<Span>,
69     pub tool_name: Symbol,
70     pub lint_name: String,
71     #[help]
72     pub is_nightly_build: Option<()>,
73 }
74
75 #[derive(SessionDiagnostic)]
76 #[diag(lint::builtin_ellipsis_inclusive_range_patterns, code = "E0783")]
77 pub struct BuiltinEllpisisInclusiveRangePatterns {
78     #[primary_span]
79     pub span: Span,
80     #[suggestion_short(code = "{replace}", applicability = "machine-applicable")]
81     pub suggestion: Span,
82     pub replace: String,
83 }
84
85 pub struct RequestedLevel {
86     pub level: Level,
87     pub lint_name: String,
88 }
89
90 impl AddSubdiagnostic for RequestedLevel {
91     fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
92         diag.note(fluent::lint::requested_level);
93         diag.set_arg(
94             "level",
95             match self.level {
96                 Level::Allow => "-A",
97                 Level::Warn => "-W",
98                 Level::ForceWarn(_) => "--force-warn",
99                 Level::Deny => "-D",
100                 Level::Forbid => "-F",
101                 Level::Expect(_) => {
102                     unreachable!("lints with the level of `expect` should not run this code");
103                 }
104             },
105         );
106         diag.set_arg("lint_name", self.lint_name);
107     }
108 }
109
110 #[derive(SessionDiagnostic)]
111 #[diag(lint::unsupported_group, code = "E0602")]
112 pub struct UnsupportedGroup {
113     pub lint_group: String,
114 }
115
116 pub struct CheckNameUnknown {
117     pub lint_name: String,
118     pub suggestion: Option<Symbol>,
119     pub sub: RequestedLevel,
120 }
121
122 impl SessionDiagnostic<'_> for CheckNameUnknown {
123     fn into_diagnostic(
124         self,
125         sess: &ParseSess,
126     ) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
127         let mut diag = sess.struct_err(fluent::lint::check_name_unknown);
128         diag.code(rustc_errors::error_code!(E0602));
129         if let Some(suggestion) = self.suggestion {
130             diag.help(fluent::lint::help);
131             diag.set_arg("suggestion", suggestion);
132         }
133         diag.set_arg("lint_name", self.lint_name);
134         diag.subdiagnostic(self.sub);
135         diag
136     }
137 }
138
139 #[derive(SessionDiagnostic)]
140 #[diag(lint::check_name_unknown_tool, code = "E0602")]
141 pub struct CheckNameUnknownTool {
142     pub tool_name: Symbol,
143     #[subdiagnostic]
144     pub sub: RequestedLevel,
145 }
146
147 #[derive(SessionDiagnostic)]
148 #[diag(lint::check_name_warning)]
149 pub struct CheckNameWarning {
150     pub msg: String,
151     #[subdiagnostic]
152     pub sub: RequestedLevel,
153 }
154
155 #[derive(SessionDiagnostic)]
156 #[diag(lint::check_name_deprecated)]
157 pub struct CheckNameDeprecated {
158     pub lint_name: String,
159     pub new_name: String,
160     #[subdiagnostic]
161     pub sub: RequestedLevel,
162 }