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