]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_lint/src/traits.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_lint / src / traits.rs
CommitLineData
29967ef6
XL
1use crate::LateContext;
2use crate::LateLintPass;
3use crate::LintContext;
4use rustc_hir as hir;
5use rustc_span::symbol::sym;
6
7declare_lint! {
8 /// The `drop_bounds` lint checks for generics with `std::ops::Drop` as
9 /// bounds.
10 ///
11 /// ### Example
12 ///
13 /// ```rust
14 /// fn foo<T: Drop>() {}
15 /// ```
16 ///
17 /// {{produces}}
18 ///
19 /// ### Explanation
20 ///
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.
26 ///
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
34 /// will leak memory.
35 pub DROP_BOUNDS,
36 Warn,
37 "bounds of the form `T: Drop` are useless"
38}
39
136023e0
XL
40declare_lint! {
41 /// The `dyn_drop` lint checks for trait objects with `std::ops::Drop`.
42 ///
43 /// ### Example
44 ///
45 /// ```rust
46 /// fn foo(_x: Box<dyn Drop>) {}
47 /// ```
48 ///
49 /// {{produces}}
50 ///
51 /// ### Explanation
52 ///
53 /// A trait object bound of the form `dyn Drop` is most likely misleading
54 /// and not what the programmer intended.
55 ///
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.
63 ///
64 /// To write a trait object bound that accepts anything, use a placeholder
65 /// trait with a blanket implementation.
66 ///
67 /// ```rust
68 /// trait Placeholder {}
69 /// impl<T> Placeholder for T {}
70 /// fn foo(_x: Box<dyn Placeholder>) {}
71 /// ```
72 pub DYN_DROP,
73 Warn,
74 "trait objects of the form `dyn Drop` are useless"
75}
76
29967ef6
XL
77declare_lint_pass!(
78 /// Lint for bounds of the form `T: Drop`, which usually
79 /// indicate an attempt to emulate `std::mem::needs_drop`.
136023e0 80 DropTraitConstraints => [DROP_BOUNDS, DYN_DROP]
29967ef6
XL
81);
82
83impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
84 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
5869c6ff 85 use rustc_middle::ty::PredicateKind::*;
29967ef6 86
6a06907d 87 let predicates = cx.tcx.explicit_predicates_of(item.def_id);
29967ef6 88 for &(predicate, span) in predicates.predicates {
5869c6ff 89 let trait_predicate = match predicate.kind().skip_binder() {
29967ef6
XL
90 Trait(trait_predicate, _constness) => trait_predicate,
91 _ => continue,
92 };
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() {
97 continue;
98 }
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,
102 None => return,
103 };
104 let msg = format!(
105 "bounds on `{}` are useless, consider instead \
106 using `{}` to detect if a type has a destructor",
107 predicate,
108 cx.tcx.def_path_str(needs_drop)
109 );
110 lint.build(&msg).emit()
111 });
112 }
113 }
114 }
136023e0
XL
115
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,
119 _ => return,
120 };
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,
127 None => return,
128 };
129 let msg = format!(
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)
133 );
134 lint.build(&msg).emit()
135 });
136 }
137 }
138 }
29967ef6 139}