4 use syntax::codemap::Span;
5 use utils::{match_def_path, paths, span_note_and_lint};
7 /// **What it does:** This lint checks for calls to `std::mem::drop` with a reference instead of an owned value.
9 /// **Why is this bad?** Calling `drop` on a reference will only drop the reference itself, which is a no-op. It will not call the `drop` method (from the `Drop` trait implementation) on the underlying referenced value, which is likely what was intended.
11 /// **Known problems:** None
15 /// let mut lock_guard = mutex.lock();
16 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex still locked
17 /// operation_that_requires_mutex_to_be_unlocked();
21 "call to `std::mem::drop` with a reference instead of an owned value, \
22 which will not call the `Drop::drop` method on the underlying value"
25 #[allow(missing_copy_implementations)]
28 impl LintPass for Pass {
29 fn get_lints(&self) -> LintArray {
34 impl LateLintPass for Pass {
35 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
36 if let ExprCall(ref path, ref args) = expr.node {
37 if let ExprPath(None, _) = path.node {
38 let def_id = cx.tcx.expect_def(path.id).def_id();
39 if match_def_path(cx, def_id, &paths::DROP) {
43 check_drop_arg(cx, expr.span, &*args[0]);
50 fn check_drop_arg(cx: &LateContext, call_span: Span, arg: &Expr) {
51 let arg_ty = cx.tcx.expr_ty(arg);
52 if let ty::TyRef(..) = arg_ty.sty {
53 span_note_and_lint(cx,
56 "call to `std::mem::drop` with a reference argument. \
57 Dropping a reference does nothing",
59 &format!("argument has type {}", arg_ty.sty));