1 use clippy_utils
::{diagnostics::span_lint_and_sugg, peel_blocks}
;
2 use if_chain
::if_chain
;
3 use rustc_errors
::Applicability
;
4 use rustc_hir
::{Body, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node}
;
5 use rustc_lint
::{LateContext, LateLintPass}
;
6 use rustc_session
::{declare_lint_pass, declare_tool_lint}
;
10 /// Checks for empty `Drop` implementations.
12 /// ### Why is this bad?
13 /// Empty `Drop` implementations have no effect when dropping an instance of the type. They are
14 /// most likely useless. However, an empty `Drop` implementation prevents a type from being
15 /// destructured, which might be the intention behind adding the implementation as a marker.
22 /// fn drop(&mut self) {}
29 #[clippy::version = "1.62.0"]
32 "empty `Drop` implementations"
34 declare_lint_pass
!(EmptyDrop
=> [EMPTY_DROP
]);
36 impl LateLintPass
<'_
> for EmptyDrop
{
37 fn check_item(&mut self, cx
: &LateContext
<'_
>, item
: &Item
<'_
>) {
39 if let ItemKind
::Impl(Impl
{
40 of_trait
: Some(ref trait_ref
),
44 if trait_ref
.trait_def_id() == cx
.tcx
.lang_items().drop_trait();
45 if let impl_item_hir
= child
.id
.hir_id();
46 if let Some(Node
::ImplItem(impl_item
)) = cx
.tcx
.hir().find(impl_item_hir
);
47 if let ImplItemKind
::Fn(_
, b
) = &impl_item
.kind
;
48 if let Body { value: func_expr, .. }
= cx
.tcx
.hir().body(*b
);
49 let func_expr
= peel_blocks(func_expr
);
50 if let ExprKind
::Block(block
, _
) = func_expr
.kind
;
51 if block
.stmts
.is_empty() && block
.expr
.is_none();
57 "empty drop implementation",
58 "try removing this impl",
60 Applicability
::MaybeIncorrect