]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_lint/src/traits.rs
New upstream version 1.61.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 ///
94222f64
XL
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).
29967ef6 24 ///
94222f64
XL
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.
34 ///
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)]`.
29967ef6
XL
39 pub DROP_BOUNDS,
40 Warn,
94222f64 41 "bounds of the form `T: Drop` are most likely incorrect"
29967ef6
XL
42}
43
136023e0
XL
44declare_lint! {
45 /// The `dyn_drop` lint checks for trait objects with `std::ops::Drop`.
46 ///
47 /// ### Example
48 ///
49 /// ```rust
50 /// fn foo(_x: Box<dyn Drop>) {}
51 /// ```
52 ///
53 /// {{produces}}
54 ///
55 /// ### Explanation
56 ///
57 /// A trait object bound of the form `dyn Drop` is most likely misleading
58 /// and not what the programmer intended.
59 ///
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.
67 ///
68 /// To write a trait object bound that accepts anything, use a placeholder
69 /// trait with a blanket implementation.
70 ///
71 /// ```rust
72 /// trait Placeholder {}
73 /// impl<T> Placeholder for T {}
74 /// fn foo(_x: Box<dyn Placeholder>) {}
75 /// ```
76 pub DYN_DROP,
77 Warn,
78 "trait objects of the form `dyn Drop` are useless"
79}
80
29967ef6
XL
81declare_lint_pass!(
82 /// Lint for bounds of the form `T: Drop`, which usually
83 /// indicate an attempt to emulate `std::mem::needs_drop`.
136023e0 84 DropTraitConstraints => [DROP_BOUNDS, DYN_DROP]
29967ef6
XL
85);
86
87impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
88 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
5869c6ff 89 use rustc_middle::ty::PredicateKind::*;
29967ef6 90
6a06907d 91 let predicates = cx.tcx.explicit_predicates_of(item.def_id);
29967ef6 92 for &(predicate, span) in predicates.predicates {
a2a8927a
XL
93 let Trait(trait_predicate) = predicate.kind().skip_binder() else {
94 continue
29967ef6
XL
95 };
96 let def_id = trait_predicate.trait_ref.def_id;
97 if cx.tcx.lang_items().drop_trait() == Some(def_id) {
98 // Explicitly allow `impl Drop`, a drop-guards-as-Voldemort-type pattern.
99 if trait_predicate.trait_ref.self_ty().is_impl_trait() {
100 continue;
101 }
102 cx.struct_span_lint(DROP_BOUNDS, span, |lint| {
a2a8927a
XL
103 let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
104 return
29967ef6
XL
105 };
106 let msg = format!(
94222f64
XL
107 "bounds on `{}` are most likely incorrect, consider instead \
108 using `{}` to detect whether a type can be trivially dropped",
29967ef6
XL
109 predicate,
110 cx.tcx.def_path_str(needs_drop)
111 );
ee023bcb 112 lint.build(&msg).emit();
29967ef6
XL
113 });
114 }
115 }
116 }
136023e0
XL
117
118 fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
a2a8927a
XL
119 let hir::TyKind::TraitObject(bounds, _lifetime, _syntax) = &ty.kind else {
120 return
136023e0
XL
121 };
122 for bound in &bounds[..] {
123 let def_id = bound.trait_ref.trait_def_id();
124 if cx.tcx.lang_items().drop_trait() == def_id {
125 cx.struct_span_lint(DYN_DROP, bound.span, |lint| {
a2a8927a
XL
126 let Some(needs_drop) = cx.tcx.get_diagnostic_item(sym::needs_drop) else {
127 return
136023e0
XL
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 );
ee023bcb 134 lint.build(&msg).emit();
136023e0
XL
135 });
136 }
137 }
138 }
29967ef6 139}