]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/undropped_manually_drops.rs
Rollup merge of #91861 - juniorbassani:use-from-array-in-collections-examples, r...
[rust.git] / src / tools / clippy / clippy_lints / src / undropped_manually_drops.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::is_type_lang_item;
3 use clippy_utils::{match_function_call, paths};
4 use rustc_hir::{lang_items, Expr};
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     /// Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`.
11     ///
12     /// ### Why is this bad?
13     /// The safe `drop` function does not drop the inner value of a `ManuallyDrop`.
14     ///
15     /// ### Known problems
16     /// Does not catch cases if the user binds `std::mem::drop`
17     /// to a different name and calls it that way.
18     ///
19     /// ### Example
20     /// ```rust
21     /// struct S;
22     /// drop(std::mem::ManuallyDrop::new(S));
23     /// ```
24     /// Use instead:
25     /// ```rust
26     /// struct S;
27     /// unsafe {
28     ///     std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
29     /// }
30     /// ```
31     #[clippy::version = "1.49.0"]
32     pub UNDROPPED_MANUALLY_DROPS,
33     correctness,
34     "use of safe `std::mem::drop` function to drop a std::mem::ManuallyDrop, which will not drop the inner value"
35 }
36
37 declare_lint_pass!(UndroppedManuallyDrops => [UNDROPPED_MANUALLY_DROPS]);
38
39 impl<'tcx> LateLintPass<'tcx> for UndroppedManuallyDrops {
40     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
41         if let Some([arg_0, ..]) = match_function_call(cx, expr, &paths::DROP) {
42             let ty = cx.typeck_results().expr_ty(arg_0);
43             if is_type_lang_item(cx, ty, lang_items::LangItem::ManuallyDrop) {
44                 span_lint_and_help(
45                     cx,
46                     UNDROPPED_MANUALLY_DROPS,
47                     expr.span,
48                     "the inner value of this ManuallyDrop will not be dropped",
49                     None,
50                     "to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop",
51                 );
52             }
53         }
54     }
55 }