]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/empty_drop.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / empty_drop.rs
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};
7
8 declare_clippy_lint! {
9 /// ### What it does
10 /// Checks for empty `Drop` implementations.
11 ///
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.
16 ///
17 /// ### Example
18 /// ```rust
19 /// struct S;
20 ///
21 /// impl Drop for S {
22 /// fn drop(&mut self) {}
23 /// }
24 /// ```
25 /// Use instead:
26 /// ```rust
27 /// struct S;
28 /// ```
29 #[clippy::version = "1.62.0"]
30 pub EMPTY_DROP,
31 restriction,
32 "empty `Drop` implementations"
33 }
34 declare_lint_pass!(EmptyDrop => [EMPTY_DROP]);
35
36 impl LateLintPass<'_> for EmptyDrop {
37 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
38 if_chain! {
39 if let ItemKind::Impl(Impl {
40 of_trait: Some(ref trait_ref),
41 items: [child],
42 ..
43 }) = item.kind;
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();
52 then {
53 span_lint_and_sugg(
54 cx,
55 EMPTY_DROP,
56 item.span,
57 "empty drop implementation",
58 "try removing this impl",
59 String::new(),
60 Applicability::MaybeIncorrect
61 );
62 }
63 }
64 }
65 }