1 use crate::LateContext;
2 use crate::LateLintPass;
3 use crate::LintContext;
5 use rustc_span::symbol::sym;
8 /// The `drop_bounds` lint checks for generics with `std::ops::Drop` as
14 /// fn foo<T: Drop>() {}
21 /// A generic trait bound of the form `T: Drop` is most likely misleading
22 /// and not what the programmer intended (they probably should have used
23 /// `std::mem::needs_drop` instead).
25 /// `Drop` bounds do not actually indicate whether a type can be trivially
26 /// dropped or not, because a composite type containing `Drop` types does
27 /// not necessarily implement `Drop` itself. Naïvely, one might be tempted
28 /// to write an implementation that assumes that a type can be trivially
29 /// dropped while also supplying a specialization for `T: Drop` that
30 /// actually calls the destructor. However, this breaks down e.g. when `T`
31 /// is `String`, which does not implement `Drop` itself but contains a
32 /// `Vec`, which does implement `Drop`, so assuming `T` can be trivially
33 /// dropped would lead to a memory leak here.
35 /// Furthermore, the `Drop` trait only contains one method, `Drop::drop`,
36 /// which may not be called explicitly in user code (`E0040`), so there is
37 /// really no use case for using `Drop` in trait bounds, save perhaps for
38 /// some obscure corner cases, which can use `#[allow(drop_bounds)]`.
41 "bounds of the form `T: Drop` are most likely incorrect"
45 /// The `dyn_drop` lint checks for trait objects with `std::ops::Drop`.
50 /// fn foo(_x: Box<dyn Drop>) {}
57 /// A trait object bound of the form `dyn Drop` is most likely misleading
58 /// and not what the programmer intended.
60 /// `Drop` bounds do not actually indicate whether a type can be trivially
61 /// dropped or not, because a composite type containing `Drop` types does
62 /// not necessarily implement `Drop` itself. Naïvely, one might be tempted
63 /// to write a deferred drop system, to pull cleaning up memory out of a
64 /// latency-sensitive code path, using `dyn Drop` trait objects. However,
65 /// this breaks down e.g. when `T` is `String`, which does not implement
66 /// `Drop`, but should probably be accepted.
68 /// To write a trait object bound that accepts anything, use a placeholder
69 /// trait with a blanket implementation.
72 /// trait Placeholder {}
73 /// impl<T> Placeholder for T {}
74 /// fn foo(_x: Box<dyn Placeholder>) {}
78 "trait objects of the form `dyn Drop` are useless"
82 /// Lint for bounds of the form `T: Drop`, which usually
83 /// indicate an attempt to emulate `std::mem::needs_drop`.
84 DropTraitConstraints => [DROP_BOUNDS, DYN_DROP]
87 impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
88 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
90 use rustc_middle::ty::PredicateKind::*;
92 let predicates = cx.tcx.explicit_predicates_of(item.def_id);
93 for &(predicate, span) in predicates.predicates {
94 let trait_predicate = match predicate.kind().skip_binder() {
95 Trait(trait_predicate) => trait_predicate,
98 if trait_predicate.constness == ty::BoundConstness::ConstIfConst {
99 // `~const Drop` definitely have meanings so avoid linting here.
102 let def_id = trait_predicate.trait_ref.def_id;
103 if cx.tcx.lang_items().drop_trait() == Some(def_id) {
104 // Explicitly allow `impl Drop`, a drop-guards-as-Voldemort-type pattern.
105 if trait_predicate.trait_ref.self_ty().is_impl_trait() {
108 cx.struct_span_lint(DROP_BOUNDS, span, |lint| {
109 let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) {
110 Some(needs_drop) => needs_drop,
114 "bounds on `{}` are most likely incorrect, consider instead \
115 using `{}` to detect whether a type can be trivially dropped",
117 cx.tcx.def_path_str(needs_drop)
119 lint.build(&msg).emit()
125 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
126 let bounds = match &ty.kind {
127 hir::TyKind::TraitObject(bounds, _lifetime, _syntax) => bounds,
130 for bound in &bounds[..] {
131 let def_id = bound.trait_ref.trait_def_id();
132 if cx.tcx.lang_items().drop_trait() == def_id {
133 cx.struct_span_lint(DYN_DROP, bound.span, |lint| {
134 let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) {
135 Some(needs_drop) => needs_drop,
139 "types that do not implement `Drop` can still have drop glue, consider \
140 instead using `{}` to detect whether a type is trivially dropped",
141 cx.tcx.def_path_str(needs_drop)
143 lint.build(&msg).emit()