From a777c46dff4fdb54e29a19e273d8677d485232e6 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Fri, 4 Nov 2022 03:02:09 +0800 Subject: [PATCH] Use `derive(Subdiagnostic)` for `ChangeFieldsToBeOfUnitType`. --- .../locales/en-US/passes.ftl | 3 +-- compiler/rustc_passes/src/errors.rs | 26 +++---------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/passes.ftl b/compiler/rustc_error_messages/locales/en-US/passes.ftl index a5b002fa3579e..a7cb9f14e8247 100644 --- a/compiler/rustc_error_messages/locales/en-US/passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/passes.ftl @@ -685,8 +685,7 @@ passes_change_fields_to_be_of_unit_type = consider changing the { $num -> [one] field *[other] fields - } to be of unit type to suppress this warning - while preserving the field numbering, or remove the { $num -> + } to be of unit type to suppress this warning while preserving the field numbering, or remove the { $num -> [one] field *[other] fields } diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index d39d7629b287f..83a51bcd097a8 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -12,8 +12,6 @@ use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_middle::ty::{MainDefinition, Ty}; use rustc_span::{Span, Symbol, DUMMY_SP}; -use rustc_errors::{pluralize, AddToDiagnostic, Diagnostic, SubdiagnosticMessage}; - use crate::lang_items::Duplicate; #[derive(LintDiagnostic)] @@ -1502,28 +1500,10 @@ pub struct IgnoredDerivedImpls { pub trait_list_len: usize, } +#[derive(Subdiagnostic)] +#[multipart_suggestion(passes_change_fields_to_be_of_unit_type, applicability = "has-placeholders")] pub struct ChangeFieldsToBeOfUnitType { pub num: usize, + #[suggestion_part(code = "()")] pub spans: Vec, } - -// FIXME: Replace this impl with a derive. -impl AddToDiagnostic for ChangeFieldsToBeOfUnitType { - fn add_to_diagnostic_with(self, diag: &mut Diagnostic, _: F) - where - F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, - { - diag.multipart_suggestion( - &format!( - "consider changing the field{s} to be of unit type to \ - suppress this warning while preserving the field \ - numbering, or remove the field{s}", - s = pluralize!(self.num) - ), - self.spans.iter().map(|sp| (*sp, "()".to_string())).collect(), - // "HasPlaceholders" because applying this fix by itself isn't - // enough: All constructor calls have to be adjusted as well - Applicability::HasPlaceholders, - ); - } -}