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