1use rustc_hir::attrs::lang_items::LangItem;
2use rustc_hir::{selfas hir, AmbigArg};
3use rustc_session::{declare_lint, declare_lint_pass};
4use rustc_span::sym;
56use crate::diagnostics::{DropGlue, DropTraitConstraintsDiag};
7use crate::{LateContext, LateLintPass, LintContext};
89#[doc =
r" The `drop_bounds` lint checks for generics with `std::ops::Drop` as"]
#[doc = r" bounds."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn foo<T: Drop>() {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" A generic trait bound of the form `T: Drop` is most likely misleading"]
#[doc =
r" and not what the programmer intended (they probably should have used"]
#[doc = r" `std::mem::needs_drop` instead)."]
#[doc = r""]
#[doc =
r" `Drop` bounds do not actually indicate whether a type can be trivially"]
#[doc =
r" dropped or not, because a composite type containing `Drop` types does"]
#[doc =
r" not necessarily implement `Drop` itself. Naïvely, one might be tempted"]
#[doc =
r" to write an implementation that assumes that a type can be trivially"]
#[doc = r" dropped while also supplying a specialization for `T: Drop` that"]
#[doc =
r" actually calls the destructor. However, this breaks down e.g. when `T`"]
#[doc =
r" is `String`, which does not implement `Drop` itself but contains a"]
#[doc =
r" `Vec`, which does implement `Drop`, so assuming `T` can be trivially"]
#[doc = r" dropped would lead to a memory leak here."]
#[doc = r""]
#[doc =
r" Furthermore, the `Drop` trait only contains one method, `Drop::drop`,"]
#[doc =
r" which may not be called explicitly in user code (`E0040`), so there is"]
#[doc =
r" really no use case for using `Drop` in trait bounds, save perhaps for"]
#[doc = r" some obscure corner cases, which can use `#[allow(drop_bounds)]`."]
pub static DROP_BOUNDS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "DROP_BOUNDS",
default_level: ::rustc_lint_defs::Warn,
desc: "bounds of the form `T: Drop` are most likely incorrect",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
10/// The `drop_bounds` lint checks for generics with `std::ops::Drop` as
11 /// bounds.
12 ///
13 /// ### Example
14 ///
15 /// ```rust
16 /// fn foo<T: Drop>() {}
17 /// ```
18 ///
19 /// {{produces}}
20 ///
21 /// ### Explanation
22 ///
23 /// A generic trait bound of the form `T: Drop` is most likely misleading
24 /// and not what the programmer intended (they probably should have used
25 /// `std::mem::needs_drop` instead).
26 ///
27 /// `Drop` bounds do not actually indicate whether a type can be trivially
28 /// dropped or not, because a composite type containing `Drop` types does
29 /// not necessarily implement `Drop` itself. Naïvely, one might be tempted
30 /// to write an implementation that assumes that a type can be trivially
31 /// dropped while also supplying a specialization for `T: Drop` that
32 /// actually calls the destructor. However, this breaks down e.g. when `T`
33 /// is `String`, which does not implement `Drop` itself but contains a
34 /// `Vec`, which does implement `Drop`, so assuming `T` can be trivially
35 /// dropped would lead to a memory leak here.
36 ///
37 /// Furthermore, the `Drop` trait only contains one method, `Drop::drop`,
38 /// which may not be called explicitly in user code (`E0040`), so there is
39 /// really no use case for using `Drop` in trait bounds, save perhaps for
40 /// some obscure corner cases, which can use `#[allow(drop_bounds)]`.
41pub DROP_BOUNDS,
42 Warn,
43"bounds of the form `T: Drop` are most likely incorrect"
44}4546#[doc =
r" The `dyn_drop` lint checks for trait objects with `std::ops::Drop`."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn foo(_x: Box<dyn Drop>) {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" A trait object bound of the form `dyn Drop` is most likely misleading"]
#[doc = r" and not what the programmer intended."]
#[doc = r""]
#[doc =
r" `Drop` bounds do not actually indicate whether a type can be trivially"]
#[doc =
r" dropped or not, because a composite type containing `Drop` types does"]
#[doc =
r" not necessarily implement `Drop` itself. Naïvely, one might be tempted"]
#[doc =
r" to write a deferred drop system, to pull cleaning up memory out of a"]
#[doc =
r" latency-sensitive code path, using `dyn Drop` trait objects. However,"]
#[doc =
r" this breaks down e.g. when `T` is `String`, which does not implement"]
#[doc = r" `Drop`, but should probably be accepted."]
#[doc = r""]
#[doc =
r" To write a trait object bound that accepts anything, use a placeholder"]
#[doc = r" trait with a blanket implementation."]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" trait Placeholder {}"]
#[doc = r" impl<T> Placeholder for T {}"]
#[doc = r" fn foo(_x: Box<dyn Placeholder>) {}"]
#[doc = r" ```"]
pub static DYN_DROP: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "DYN_DROP",
default_level: ::rustc_lint_defs::Warn,
desc: "trait objects of the form `dyn Drop` are useless",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
47/// The `dyn_drop` lint checks for trait objects with `std::ops::Drop`.
48 ///
49 /// ### Example
50 ///
51 /// ```rust
52 /// fn foo(_x: Box<dyn Drop>) {}
53 /// ```
54 ///
55 /// {{produces}}
56 ///
57 /// ### Explanation
58 ///
59 /// A trait object bound of the form `dyn Drop` is most likely misleading
60 /// and not what the programmer intended.
61 ///
62 /// `Drop` bounds do not actually indicate whether a type can be trivially
63 /// dropped or not, because a composite type containing `Drop` types does
64 /// not necessarily implement `Drop` itself. Naïvely, one might be tempted
65 /// to write a deferred drop system, to pull cleaning up memory out of a
66 /// latency-sensitive code path, using `dyn Drop` trait objects. However,
67 /// this breaks down e.g. when `T` is `String`, which does not implement
68 /// `Drop`, but should probably be accepted.
69 ///
70 /// To write a trait object bound that accepts anything, use a placeholder
71 /// trait with a blanket implementation.
72 ///
73 /// ```rust
74 /// trait Placeholder {}
75 /// impl<T> Placeholder for T {}
76 /// fn foo(_x: Box<dyn Placeholder>) {}
77 /// ```
78pub DYN_DROP,
79 Warn,
80"trait objects of the form `dyn Drop` are useless"
81}8283#[doc = r" Lint for bounds of the form `T: Drop`, which usually"]
#[doc = r" indicate an attempt to emulate `std::mem::needs_drop`."]
pub struct DropTraitConstraints;
#[automatically_derived]
impl ::core::marker::Copy for DropTraitConstraints { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for DropTraitConstraints { }
#[automatically_derived]
impl ::core::clone::Clone for DropTraitConstraints {
#[inline]
fn clone(&self) -> DropTraitConstraints { *self }
}
impl ::rustc_lint_defs::LintPass for DropTraitConstraints {
fn name(&self) -> &'static str { "DropTraitConstraints" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DROP_BOUNDS, DYN_DROP]))
}
}
impl DropTraitConstraints {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DROP_BOUNDS, DYN_DROP]))
}
}declare_lint_pass!(
84/// Lint for bounds of the form `T: Drop`, which usually
85 /// indicate an attempt to emulate `std::mem::needs_drop`.
86DropTraitConstraints => [DROP_BOUNDS, DYN_DROP]
87);
8889impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
90fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
91use rustc_middle::ty::ClauseKind;
9293let gen_clauses = cx.tcx.explicit_clauses_of(item.owner_id);
94for &(clause, span) in gen_clauses.clauses {
95let ClauseKind::Trait(trait_predicate) = clause.kind().skip_binder() else {
96continue;
97 };
98let def_id = trait_predicate.trait_ref.def_id;
99if cx.tcx.is_lang_item(def_id, LangItem::Drop) {
100// Explicitly allow `impl Drop`, a drop-guards-as-unnameable-type pattern.
101if trait_predicate.trait_ref.self_ty().is_opaque() {
102continue;
103 }
104let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { return };
105 cx.emit_span_lint(
106 DROP_BOUNDS,
107 span,
108 DropTraitConstraintsDiag { clause, tcx: cx.tcx, def_id },
109 );
110 }
111 }
112 }
113114fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
115let hir::TyKind::TraitObject(bounds, _lifetime_and_syntax_pointer) = &ty.kind else {
116return;
117 };
118for bound in &bounds[..] {
119let def_id = bound.trait_ref.trait_def_id();
120if def_id.is_some_and(|def_id| cx.tcx.is_lang_item(def_id, LangItem::Drop)) {
121let Some(def_id) = cx.tcx.get_diagnostic_item(sym::needs_drop) else { return };
122 cx.emit_span_lint(DYN_DROP, bound.span, DropGlue { tcx: cx.tcx, def_id });
123 }
124 }
125 }
126}