From 5ffaae758e45cc87b435b1a929c8aae9d9ea5a69 Mon Sep 17 00:00:00 2001 From: Rejyr Date: Fri, 7 Oct 2022 06:38:20 -0400 Subject: [PATCH] migrate: `ImproperCTypes` --- compiler/rustc_lint/src/lints.rs | 42 ++++++++++++++++++++++++++++---- compiler/rustc_lint/src/types.rs | 42 ++++++++++++++------------------ 2 files changed, 55 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 0f314606a94..a6e00c3e883 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1,4 +1,4 @@ -use rustc_errors::{fluent, AddToDiagnostic, Applicability, DecorateLint}; +use rustc_errors::{fluent, AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage}; use rustc_hir::def_id::DefId; use rustc_macros::{LintDiagnostic, Subdiagnostic}; use rustc_middle::ty::{Predicate, Ty, TyCtxt}; @@ -52,12 +52,12 @@ pub struct EnumIntrinsicsMemVariant<'a> { // let_underscore.rs #[derive(LintDiagnostic)] pub enum NonBindingLet { - #[diag(lint::non_binding_let_on_sync_lock)] + #[diag(lint_non_binding_let_on_sync_lock)] SyncLock { #[subdiagnostic] sub: NonBindingLetSub, }, - #[diag(lint::non_binding_let_on_drop_type)] + #[diag(lint_non_binding_let_on_drop_type)] DropType { #[subdiagnostic] sub: NonBindingLetSub, @@ -80,12 +80,12 @@ impl AddToDiagnostic for NonBindingLetSub { { diag.span_suggestion_verbose( self.suggestion, - fluent::lint::non_binding_let_suggestion, + fluent::lint_non_binding_let_suggestion, "_unused", Applicability::MachineApplicable, ); diag.multipart_suggestion( - fluent::lint::non_binding_let_multi_suggestion, + fluent::lint_non_binding_let_multi_suggestion, vec![ (self.multi_suggestion_start, "drop(".to_string()), (self.multi_suggestion_end, ")".to_string()), @@ -568,6 +568,38 @@ pub struct OverflowingLiteral<'a> { #[diag(lint_unused_comparisons)] pub struct UnusedComparisons; +pub struct ImproperCTypes<'a> { + pub ty: Ty<'a>, + pub desc: &'a str, + pub label: Span, + pub help: Option, + pub note: DiagnosticMessage, + pub span_note: Option, +} + +impl<'a> DecorateLint<'a, ()> for ImproperCTypes<'_> { + fn decorate_lint<'b>( + self, + diag: &'b mut rustc_errors::DiagnosticBuilder<'a, ()>, + ) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> { + diag.set_arg("ty", self.ty); + diag.set_arg("desc", self.desc); + diag.span_label(self.label, fluent::label); + if let Some(help) = self.help { + diag.help(help); + } + diag.note(self.note); + if let Some(note) = self.span_note { + diag.span_note(note, fluent::note); + } + diag + } + + fn msg(&self) -> rustc_errors::DiagnosticMessage { + fluent::lint_improper_ctypes + } +} + #[derive(LintDiagnostic)] #[diag(lint_variant_size_differences)] pub struct VariantSizeDifferencesDiag { diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 3d797d84c04..a112292eb14 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -1,10 +1,10 @@ #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] use crate::lints::{ - AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, InvalidAtomicOrderingDiag, - OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign, OverflowingBinHexSub, - OverflowingInt, OverflowingLiteral, OverflowingUInt, RangeEndpointOutOfRange, - UnusedComparisons, VariantSizeDifferencesDiag, + AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, ImproperCTypes, + InvalidAtomicOrderingDiag, OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign, + OverflowingBinHexSub, OverflowingInt, OverflowingLiteral, OverflowingUInt, + RangeEndpointOutOfRange, UnusedComparisons, VariantSizeDifferencesDiag, }; use crate::{LateContext, LateLintPass, LintContext}; use rustc_ast as ast; @@ -1131,27 +1131,21 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { CItemKind::Declaration => IMPROPER_CTYPES, CItemKind::Definition => IMPROPER_CTYPES_DEFINITIONS, }; - - self.cx.struct_span_lint(lint, sp, fluent::lint_improper_ctypes, |lint| { - let item_description = match self.mode { - CItemKind::Declaration => "block", - CItemKind::Definition => "fn", + let desc = match self.mode { + CItemKind::Declaration => "block", + CItemKind::Definition => "fn", + }; + let span_note = if let ty::Adt(def, _) = ty.kind() + && let Some(sp) = self.cx.tcx.hir().span_if_local(def.did()) { + Some(sp) + } else { + None }; - #[allow(rustc::diagnostic_outside_of_impl)] - lint.set_arg("ty", ty); - lint.set_arg("desc", item_description); - lint.span_label(sp, fluent::label); - if let Some(help) = help { - lint.help(help); - } - lint.note(note); - if let ty::Adt(def, _) = ty.kind() { - if let Some(sp) = self.cx.tcx.hir().span_if_local(def.did()) { - lint.span_note(sp, fluent::note); - } - } - lint - }); + self.cx.emit_spanned_lint( + lint, + sp, + ImproperCTypes { ty, desc, label: sp, help, note, span_note }, + ); } fn check_for_opaque_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {