1 use rustc_errors::{fluent, AddSubdiagnostic, Applicability, DecorateLint, EmissionGuarantee};
2 use rustc_hir::def_id::DefId;
3 use rustc_macros::{LintDiagnostic, SessionSubdiagnostic};
4 use rustc_middle::ty::{Predicate, Ty, TyCtxt};
5 use rustc_span::{Span, Symbol};
7 use crate::LateContext;
9 #[derive(LintDiagnostic)]
10 #[diag(lint_noop_method_call)]
12 pub struct NoopMethodCallDiag<'a> {
14 pub receiver_ty: Ty<'a>,
19 #[derive(LintDiagnostic)]
20 #[diag(lint_pass_by_value)]
21 pub struct PassByValueDiag {
23 #[suggestion(code = "{ty}", applicability = "maybe-incorrect")]
27 #[derive(LintDiagnostic)]
28 #[diag(lint_redundant_semicolons)]
29 pub struct RedundantSemicolonsDiag {
31 #[suggestion(code = "", applicability = "maybe-incorrect")]
35 pub struct DropTraitConstraintsDiag<'a> {
36 pub predicate: Predicate<'a>,
41 impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for DropTraitConstraintsDiag<'a> {
42 fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
43 let mut diag = diag.build(fluent::lint_drop_trait_constraints);
44 diag.set_arg("predicate", self.predicate);
45 diag.set_arg("needs_drop", self.tcx.def_path_str(self.def_id));
50 pub struct DropGlue<'a> {
55 impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for DropGlue<'a> {
56 fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
57 let mut diag = diag.build(fluent::lint_drop_glue);
58 diag.set_arg("needs_drop", self.tcx.def_path_str(self.def_id));
63 #[derive(LintDiagnostic)]
64 #[diag(lint_range_endpoint_out_of_range)]
65 pub struct RangeEndpointOutOfRange<'a> {
67 #[suggestion(code = "{start}..={literal}{suffix}", applicability = "machine-applicable")]
74 #[derive(LintDiagnostic)]
75 #[diag(lint_overflowing_bin_hex)]
76 pub struct OverflowingBinHex<'a> {
82 pub sign: OverflowingBinHexSign,
84 pub sub: Option<OverflowingBinHexSub<'a>>,
87 pub enum OverflowingBinHexSign {
92 impl AddSubdiagnostic for OverflowingBinHexSign {
93 fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
95 OverflowingBinHexSign::Positive => {
96 diag.note(fluent::positive_note);
98 OverflowingBinHexSign::Negative => {
99 diag.note(fluent::negative_note);
100 diag.note(fluent::negative_becomes_note);
106 #[derive(SessionSubdiagnostic)]
107 pub enum OverflowingBinHexSub<'a> {
110 code = "{sans_suffix}{suggestion_ty}",
111 applicability = "machine-applicable"
116 suggestion_ty: &'a str,
117 sans_suffix: &'a str,
120 Help { suggestion_ty: &'a str },
123 pub struct OverflowingInt<'a> {
128 pub suggestion_ty: Option<&'a str>,
131 // FIXME: refactor with `Option<&'a str>` in macro
132 impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for OverflowingInt<'a> {
133 fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
134 let mut diag = diag.build(fluent::lint_overflowing_int);
135 diag.set_arg("ty", self.ty);
136 diag.set_arg("lit", self.lit);
137 diag.set_arg("min", self.min);
138 diag.set_arg("max", self.max);
139 diag.note(fluent::note);
140 if let Some(suggestion_ty) = self.suggestion_ty {
141 diag.set_arg("suggestion_ty", suggestion_ty);
142 diag.help(fluent::help);
148 #[derive(LintDiagnostic)]
149 #[diag(lint_only_cast_u8_to_char)]
150 pub struct OnlyCastu8ToChar {
151 #[suggestion(code = "'\\u{{{literal:X}}}'", applicability = "machine-applicable")]
156 #[derive(LintDiagnostic)]
157 #[diag(lint_overflowing_uint)]
159 pub struct OverflowingUInt<'a> {
166 #[derive(LintDiagnostic)]
167 #[diag(lint_overflowing_literal)]
169 pub struct OverflowingLiteral<'a> {
174 #[derive(LintDiagnostic)]
175 #[diag(lint_unused_comparisons)]
176 pub struct UnusedComparisons;
178 #[derive(LintDiagnostic)]
179 #[diag(lint_variant_size_differences)]
180 pub struct VariantSizeDifferencesDiag {
184 #[derive(LintDiagnostic)]
185 #[diag(lint_atomic_ordering_load)]
187 pub struct AtomicOrderingLoad;
189 #[derive(LintDiagnostic)]
190 #[diag(lint_atomic_ordering_store)]
192 pub struct AtomicOrderingStore;
194 #[derive(LintDiagnostic)]
195 #[diag(lint_atomic_ordering_fence)]
197 pub struct AtomicOrderingFence;
199 #[derive(LintDiagnostic)]
200 #[diag(lint_atomic_ordering_invalid)]
202 pub struct InvalidAtomicOrderingDiag {
205 pub fail_order_arg_span: Span,
208 #[derive(LintDiagnostic)]
209 #[diag(lint_unused_op)]
210 pub struct UnusedOp<'a> {
214 #[suggestion(style = "verbose", code = "let _ = ", applicability = "machine-applicable")]
215 pub suggestion: Span,
218 #[derive(LintDiagnostic)]
219 #[diag(lint_unused_result)]
220 pub struct UnusedResult<'a> {
224 // FIXME(davidtwco): this isn't properly translatable becauses of the
226 #[derive(LintDiagnostic)]
227 #[diag(lint_unused_closure)]
229 pub struct UnusedClosure<'a> {
235 // FIXME(davidtwco): this isn't properly translatable becauses of the
237 #[derive(LintDiagnostic)]
238 #[diag(lint_unused_generator)]
240 pub struct UnusedGenerator<'a> {
246 // FIXME(davidtwco): this isn't properly translatable becauses of the pre/post
248 pub struct UnusedDef<'a, 'b> {
251 pub cx: &'a LateContext<'b>,
253 pub note: Option<Symbol>,
256 // FIXME: refactor with `Option<String>` in macro
257 impl<'a, 'b, G: EmissionGuarantee> DecorateLint<'_, G> for UnusedDef<'a, 'b> {
258 fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
259 let mut diag = diag.build(fluent::lint_unused_def);
260 diag.set_arg("pre", self.pre);
261 diag.set_arg("post", self.post);
262 diag.set_arg("def", self.cx.tcx.def_path_str(self.def_id));
263 // check for #[must_use = "..."]
264 if let Some(note) = self.note {
265 diag.note(note.as_str());
271 #[derive(LintDiagnostic)]
272 #[diag(lint_path_statement_drop)]
273 pub struct PathStatementDrop {
275 pub sub: PathStatementDropSub,
278 #[derive(SessionSubdiagnostic)]
279 pub enum PathStatementDropSub {
282 code = "drop({snippet});",
283 applicability = "machine-applicable"
297 #[derive(LintDiagnostic)]
298 #[diag(lint_path_statement_no_effect)]
299 pub struct PathStatementNoEffect;
301 #[derive(LintDiagnostic)]
302 #[diag(lint_unused_delim)]
303 pub struct UnusedDelim<'a> {
304 pub delim: &'static str,
307 pub suggestion: Option<UnusedDelimSuggestion>,
310 #[derive(Subdiagnostic)]
311 #[multipart_suggestion(suggestion, applicability = "machine-applicable")]
312 pub struct UnusedDelimSuggestion {
313 #[suggestion_part(code = "{start_replace}")]
314 pub start_span: Span,
315 pub start_replace: &'static str,
316 #[suggestion_part(code = "{end_replace}")]
318 pub end_replace: &'static str,
321 #[derive(LintDiagnostic)]
322 #[diag(lint_unused_import_braces)]
323 pub struct UnusedImportBracesDiag {
327 #[derive(LintDiagnostic)]
328 #[diag(lint_unused_allocation)]
329 pub struct UnusedAllocationDiag;
331 #[derive(LintDiagnostic)]
332 #[diag(lint_unused_allocation_mut)]
333 pub struct UnusedAllocationMutDiag;