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