Skip to content

Commit 5ffaae7

Browse files
committed
migrate: ImproperCTypes
1 parent e610047 commit 5ffaae7

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_errors::{fluent, AddToDiagnostic, Applicability, DecorateLint};
1+
use rustc_errors::{fluent, AddToDiagnostic, Applicability, DecorateLint, DiagnosticMessage};
22
use rustc_hir::def_id::DefId;
33
use rustc_macros::{LintDiagnostic, Subdiagnostic};
44
use rustc_middle::ty::{Predicate, Ty, TyCtxt};
@@ -52,12 +52,12 @@ pub struct EnumIntrinsicsMemVariant<'a> {
5252
// let_underscore.rs
5353
#[derive(LintDiagnostic)]
5454
pub enum NonBindingLet {
55-
#[diag(lint::non_binding_let_on_sync_lock)]
55+
#[diag(lint_non_binding_let_on_sync_lock)]
5656
SyncLock {
5757
#[subdiagnostic]
5858
sub: NonBindingLetSub,
5959
},
60-
#[diag(lint::non_binding_let_on_drop_type)]
60+
#[diag(lint_non_binding_let_on_drop_type)]
6161
DropType {
6262
#[subdiagnostic]
6363
sub: NonBindingLetSub,
@@ -80,12 +80,12 @@ impl AddToDiagnostic for NonBindingLetSub {
8080
{
8181
diag.span_suggestion_verbose(
8282
self.suggestion,
83-
fluent::lint::non_binding_let_suggestion,
83+
fluent::lint_non_binding_let_suggestion,
8484
"_unused",
8585
Applicability::MachineApplicable,
8686
);
8787
diag.multipart_suggestion(
88-
fluent::lint::non_binding_let_multi_suggestion,
88+
fluent::lint_non_binding_let_multi_suggestion,
8989
vec![
9090
(self.multi_suggestion_start, "drop(".to_string()),
9191
(self.multi_suggestion_end, ")".to_string()),
@@ -568,6 +568,38 @@ pub struct OverflowingLiteral<'a> {
568568
#[diag(lint_unused_comparisons)]
569569
pub struct UnusedComparisons;
570570

571+
pub struct ImproperCTypes<'a> {
572+
pub ty: Ty<'a>,
573+
pub desc: &'a str,
574+
pub label: Span,
575+
pub help: Option<DiagnosticMessage>,
576+
pub note: DiagnosticMessage,
577+
pub span_note: Option<Span>,
578+
}
579+
580+
impl<'a> DecorateLint<'a, ()> for ImproperCTypes<'_> {
581+
fn decorate_lint<'b>(
582+
self,
583+
diag: &'b mut rustc_errors::DiagnosticBuilder<'a, ()>,
584+
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
585+
diag.set_arg("ty", self.ty);
586+
diag.set_arg("desc", self.desc);
587+
diag.span_label(self.label, fluent::label);
588+
if let Some(help) = self.help {
589+
diag.help(help);
590+
}
591+
diag.note(self.note);
592+
if let Some(note) = self.span_note {
593+
diag.span_note(note, fluent::note);
594+
}
595+
diag
596+
}
597+
598+
fn msg(&self) -> rustc_errors::DiagnosticMessage {
599+
fluent::lint_improper_ctypes
600+
}
601+
}
602+
571603
#[derive(LintDiagnostic)]
572604
#[diag(lint_variant_size_differences)]
573605
pub struct VariantSizeDifferencesDiag {

compiler/rustc_lint/src/types.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#![deny(rustc::untranslatable_diagnostic)]
22
#![deny(rustc::diagnostic_outside_of_impl)]
33
use crate::lints::{
4-
AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, InvalidAtomicOrderingDiag,
5-
OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign, OverflowingBinHexSub,
6-
OverflowingInt, OverflowingLiteral, OverflowingUInt, RangeEndpointOutOfRange,
7-
UnusedComparisons, VariantSizeDifferencesDiag,
4+
AtomicOrderingFence, AtomicOrderingLoad, AtomicOrderingStore, ImproperCTypes,
5+
InvalidAtomicOrderingDiag, OnlyCastu8ToChar, OverflowingBinHex, OverflowingBinHexSign,
6+
OverflowingBinHexSub, OverflowingInt, OverflowingLiteral, OverflowingUInt,
7+
RangeEndpointOutOfRange, UnusedComparisons, VariantSizeDifferencesDiag,
88
};
99
use crate::{LateContext, LateLintPass, LintContext};
1010
use rustc_ast as ast;
@@ -1131,27 +1131,21 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11311131
CItemKind::Declaration => IMPROPER_CTYPES,
11321132
CItemKind::Definition => IMPROPER_CTYPES_DEFINITIONS,
11331133
};
1134-
1135-
self.cx.struct_span_lint(lint, sp, fluent::lint_improper_ctypes, |lint| {
1136-
let item_description = match self.mode {
1137-
CItemKind::Declaration => "block",
1138-
CItemKind::Definition => "fn",
1134+
let desc = match self.mode {
1135+
CItemKind::Declaration => "block",
1136+
CItemKind::Definition => "fn",
1137+
};
1138+
let span_note = if let ty::Adt(def, _) = ty.kind()
1139+
&& let Some(sp) = self.cx.tcx.hir().span_if_local(def.did()) {
1140+
Some(sp)
1141+
} else {
1142+
None
11391143
};
1140-
#[allow(rustc::diagnostic_outside_of_impl)]
1141-
lint.set_arg("ty", ty);
1142-
lint.set_arg("desc", item_description);
1143-
lint.span_label(sp, fluent::label);
1144-
if let Some(help) = help {
1145-
lint.help(help);
1146-
}
1147-
lint.note(note);
1148-
if let ty::Adt(def, _) = ty.kind() {
1149-
if let Some(sp) = self.cx.tcx.hir().span_if_local(def.did()) {
1150-
lint.span_note(sp, fluent::note);
1151-
}
1152-
}
1153-
lint
1154-
});
1144+
self.cx.emit_spanned_lint(
1145+
lint,
1146+
sp,
1147+
ImproperCTypes { ty, desc, label: sp, help, note, span_note },
1148+
);
11551149
}
11561150

11571151
fn check_for_opaque_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {

0 commit comments

Comments
 (0)