]>
Commit | Line | Data |
---|---|---|
1 | use crate::LateContext; | |
2 | use crate::LateLintPass; | |
3 | use crate::LintContext; | |
4 | use rustc_hir as hir; | |
5 | use rustc_span::symbol::sym; | |
6 | ||
7 | declare_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 | ||
40 | declare_lint_pass!( | |
41 | /// Lint for bounds of the form `T: Drop`, which usually | |
42 | /// indicate an attempt to emulate `std::mem::needs_drop`. | |
43 | DropTraitConstraints => [DROP_BOUNDS] | |
44 | ); | |
45 | ||
46 | impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints { | |
47 | fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { | |
48 | use rustc_middle::ty::PredicateKind::*; | |
49 | ||
50 | let predicates = cx.tcx.explicit_predicates_of(item.def_id); | |
51 | for &(predicate, span) in predicates.predicates { | |
52 | let trait_predicate = match predicate.kind().skip_binder() { | |
53 | Trait(trait_predicate, _constness) => trait_predicate, | |
54 | _ => continue, | |
55 | }; | |
56 | let def_id = trait_predicate.trait_ref.def_id; | |
57 | if cx.tcx.lang_items().drop_trait() == Some(def_id) { | |
58 | // Explicitly allow `impl Drop`, a drop-guards-as-Voldemort-type pattern. | |
59 | if trait_predicate.trait_ref.self_ty().is_impl_trait() { | |
60 | continue; | |
61 | } | |
62 | cx.struct_span_lint(DROP_BOUNDS, span, |lint| { | |
63 | let needs_drop = match cx.tcx.get_diagnostic_item(sym::needs_drop) { | |
64 | Some(needs_drop) => needs_drop, | |
65 | None => return, | |
66 | }; | |
67 | let msg = format!( | |
68 | "bounds on `{}` are useless, consider instead \ | |
69 | using `{}` to detect if a type has a destructor", | |
70 | predicate, | |
71 | cx.tcx.def_path_str(needs_drop) | |
72 | ); | |
73 | lint.build(&msg).emit() | |
74 | }); | |
75 | } | |
76 | } | |
77 | } | |
78 | } |