Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a42afa0

Browse files
committed
migrate: traits.rs
1 parent e5ae9d0 commit a42afa0

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
use rustc_errors::{fluent, AddSubdiagnostic, Applicability, DecorateLint, EmissionGuarantee};
22
use rustc_hir::def_id::DefId;
33
use rustc_macros::{LintDiagnostic, SessionSubdiagnostic};
4-
use rustc_middle::ty::Ty;
4+
use rustc_middle::ty::{Predicate, Ty, TyCtxt};
55
use rustc_span::{Span, Symbol};
66

77
use crate::LateContext;
88

9+
pub struct DropTraitConstraintsDiag<'a> {
10+
pub predicate: Predicate<'a>,
11+
pub tcx: TyCtxt<'a>,
12+
pub def_id: DefId,
13+
}
14+
15+
impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for DropTraitConstraintsDiag<'a> {
16+
fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
17+
let mut diag = diag.build(fluent::lint_drop_trait_constraints);
18+
diag.set_arg("predicate", self.predicate);
19+
diag.set_arg("needs_drop", self.tcx.def_path_str(self.def_id));
20+
diag.emit();
21+
}
22+
}
23+
24+
pub struct DropGlue<'a> {
25+
pub tcx: TyCtxt<'a>,
26+
pub def_id: DefId,
27+
}
28+
29+
impl<'a, G: EmissionGuarantee> DecorateLint<'_, G> for DropGlue<'a> {
30+
fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
31+
let mut diag = diag.build(fluent::lint_drop_glue);
32+
diag.set_arg("needs_drop", self.tcx.def_path_str(self.def_id));
33+
diag.emit();
34+
}
35+
}
36+
937
#[derive(LintDiagnostic)]
1038
#[diag(lint_range_endpoint_out_of_range)]
1139
pub struct RangeEndpointOutOfRange<'a> {

compiler/rustc_lint/src/traits.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use crate::lints::{DropGlue, DropTraitConstraintsDiag};
14
use crate::LateContext;
25
use crate::LateLintPass;
36
use crate::LintContext;
4-
use rustc_errors::fluent;
57
use rustc_hir as hir;
68
use rustc_span::symbol::sym;
79

@@ -101,17 +103,13 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
101103
if trait_predicate.trait_ref.self_ty().is_impl_trait() {
102104
continue;
103105
}
104-
let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
105-
continue;
106+
let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
107+
return
106108
};
107-
cx.struct_span_lint(
109+
cx.emit_spanned_lint(
108110
DROP_BOUNDS,
109111
span,
110-
fluent::lint_drop_trait_constraints,
111-
|lint| {
112-
lint.set_arg("predicate", predicate)
113-
.set_arg("needs_drop", cx.tcx.def_path_str(needs_drop))
114-
},
112+
DropTraitConstraintsDiag { predicate, tcx: cx.tcx, def_id },
115113
);
116114
}
117115
}
@@ -123,12 +121,11 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
123121
};
124122
for bound in &bounds[..] {
125123
let def_id = bound.trait_ref.trait_def_id();
126-
if cx.tcx.lang_items().drop_trait() == def_id
127-
&& let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop)
128-
{
129-
cx.struct_span_lint(DYN_DROP, bound.span, fluent::lint_drop_glue, |lint| {
130-
lint.set_arg("needs_drop", cx.tcx.def_path_str(needs_drop))
131-
});
124+
if cx.tcx.lang_items().drop_trait() == def_id {
125+
let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
126+
return
127+
};
128+
cx.emit_spanned_lint(DYN_DROP, bound.span, DropGlue { tcx: cx.tcx, def_id });
132129
}
133130
}
134131
}

0 commit comments

Comments
 (0)