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};
7 /// **What it does:** Prevents the safe `std::mem::drop` function from being called on `std::mem::ManuallyDrop`.
9 /// **Why is this bad?** The safe `drop` function does not drop the inner value of a `ManuallyDrop`.
11 /// **Known problems:** Does not catch cases if the user binds `std::mem::drop`
12 /// to a different name and calls it that way.
18 /// drop(std::mem::ManuallyDrop::new(S));
24 /// std::mem::ManuallyDrop::drop(&mut std::mem::ManuallyDrop::new(S));
27 pub UNDROPPED_MANUALLY_DROPS,
29 "use of safe `std::mem::drop` function to drop a std::mem::ManuallyDrop, which will not drop the inner value"
32 declare_lint_pass!(UndroppedManuallyDrops => [UNDROPPED_MANUALLY_DROPS]);
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) {
41 UNDROPPED_MANUALLY_DROPS,
43 "the inner value of this ManuallyDrop will not be dropped",
45 "to drop a `ManuallyDrop<T>`, use std::mem::ManuallyDrop::drop",