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