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 /// `Drop` bounds do not really accomplish anything. A type may have
22 /// compiler-generated drop glue without implementing the `Drop` trait
23 /// itself. The `Drop` trait also only has one method, `Drop::drop`, and
24 /// that function is by fiat not callable in user code. So there is really
25 /// no use case for using `Drop` in trait bounds.
27 /// The most likely use case of a drop bound is to distinguish between
28 /// types that have destructors and types that don't. Combined with
29 /// specialization, a naive coder would write an implementation that
30 /// assumed a type could be trivially dropped, then write a specialization
31 /// for `T: Drop` that actually calls the destructor. Except that doing so
32 /// is not correct; String, for example, doesn't actually implement Drop,
33 /// but because String contains a Vec, assuming it can be trivially dropped
37 "bounds of the form `T: Drop` are useless"
41 /// The `dyn_drop` lint checks for trait objects with `std::ops::Drop`.
46 /// fn foo(_x: Box<dyn Drop>) {}
53 /// A trait object bound of the form `dyn Drop` is most likely misleading
54 /// and not what the programmer intended.
56 /// `Drop` bounds do not actually indicate whether a type can be trivially
57 /// dropped or not, because a composite type containing `Drop` types does
58 /// not necessarily implement `Drop` itself. Naïvely, one might be tempted
59 /// to write a deferred drop system, to pull cleaning up memory out of a
60 /// latency-sensitive code path, using `dyn Drop` trait objects. However,
61 /// this breaks down e.g. when `T` is `String`, which does not implement
62 /// `Drop`, but should probably be accepted.
64 /// To write a trait object bound that accepts anything, use a placeholder
65 /// trait with a blanket implementation.
68 /// trait Placeholder {}
69 /// impl<T> Placeholder for T {}
70 /// fn foo(_x: Box<dyn Placeholder>) {}
74 "trait objects of the form `dyn Drop` are useless"
78 /// Lint for bounds of the form `T: Drop`, which usually
79 /// indicate an attempt to emulate `std::mem::needs_drop`.
80 DropTraitConstraints => [DROP_BOUNDS, DYN_DROP]
83 impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
84 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
85 use rustc_middle::ty::PredicateKind::*;
87 let predicates = cx.tcx.explicit_predicates_of(item.def_id);
88 for &(predicate, span) in predicates.predicates {
89 let trait_predicate = match predicate.kind().skip_binder() {
90 Trait(trait_predicate) => trait_predicate,
93 let def_id = trait_predicate.trait_ref.def_id;
94 if cx.tcx.lang_items().drop_trait() == Some(def_id) {
95 // Explicitly allow `impl Drop`, a drop-guards-as-Voldemort-type pattern.
96 if trait_predicate.trait_ref.self_ty().is_impl_trait() {
99 cx.struct_span_lint(DROP_BOUNDS, span, |lint| {
100 let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) {
101 Some(needs_drop) => needs_drop,
105 "bounds on `{}` are useless, consider instead \
106 using `{}` to detect if a type has a destructor",
108 cx.tcx.def_path_str(needs_drop)
110 lint.build(&msg).emit()
116 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
117 let bounds = match &ty.kind {
118 hir::TyKind::TraitObject(bounds, _lifetime, _syntax) => bounds,
121 for bound in &bounds[..] {
122 let def_id = bound.trait_ref.trait_def_id();
123 if cx.tcx.lang_items().drop_trait() == def_id {
124 cx.struct_span_lint(DYN_DROP, bound.span, |lint| {
125 let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) {
126 Some(needs_drop) => needs_drop,
130 "types that do not implement `Drop` can still have drop glue, consider \
131 instead using `{}` to detect whether a type is trivially dropped",
132 cx.tcx.def_path_str(needs_drop)
134 lint.build(&msg).emit()