Added lint for mem_forget

This commit is contained in:
Taylor Cramer 2016-04-20 13:10:23 -07:00
parent 0e16d9a425
commit 447940c889
4 changed files with 63 additions and 0 deletions

View File

@ -78,6 +78,7 @@ pub mod len_zero;
pub mod lifetimes;
pub mod loops;
pub mod map_clone;
pub mod mem_forget;
pub mod matches;
pub mod methods;
pub mod minmax;
@ -236,6 +237,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_early_lint_pass(box doc::Doc::new(conf.doc_valid_idents));
reg.register_late_lint_pass(box neg_multiply::NegMultiply);
reg.register_late_lint_pass(box unsafe_removed_from_name::UnsafeNameRemoval);
reg.register_late_lint_pass(box mem_forget::MemForget);
reg.register_lint_group("clippy_pedantic", vec![
array_indexing::INDEXING_SLICING,
@ -243,6 +245,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
enum_glob_use::ENUM_GLOB_USE,
if_not_else::IF_NOT_ELSE,
matches::SINGLE_MATCH_ELSE,
mem_forget::MEM_FORGET,
methods::OPTION_UNWRAP_USED,
methods::RESULT_UNWRAP_USED,
methods::WRONG_PUB_SELF_CONVENTION,

38
src/mem_forget.rs Normal file
View File

@ -0,0 +1,38 @@
use rustc::lint::*;
use rustc::hir::{Expr, ExprCall, ExprPath};
use utils::{match_def_path, paths, span_lint};
/// **What it does:** This lint checks for usage of `std::mem::forget(_)`.
///
/// **Why is this bad?** `std::mem::forget(t)` prevents `t` from running its destructor, possibly causing leaks
///
/// **Known problems:** None.
///
/// **Example:** `std::mem::forget(_))`
declare_lint! {
pub MEM_FORGET,
Allow,
"std::mem::forget usage is likely to cause memory leaks"
}
pub struct MemForget;
impl LintPass for MemForget {
fn get_lints(&self) -> LintArray {
lint_array![MEM_FORGET]
}
}
impl LateLintPass for MemForget {
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
if let ExprCall(ref path_expr, _) = e.node {
if let ExprPath(None, _) = path_expr.node {
let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id();
if match_def_path(cx, def_id, &paths::MEM_FORGET) {
span_lint(cx, MEM_FORGET, e.span, "usage of std::mem::forget");
}
}
}
}
}

View File

@ -20,6 +20,7 @@ pub const HASHMAP: [&'static str; 5] = ["std", "collections", "hash", "map", "Ha
pub const HASH: [&'static str; 2] = ["hash", "Hash"];
pub const IO_PRINT: [&'static str; 3] = ["std", "io", "_print"];
pub const LINKED_LIST: [&'static str; 3] = ["collections", "linked_list", "LinkedList"];
pub const MEM_FORGET: [&'static str; 3] = ["core", "mem", "forget"];
pub const MUTEX: [&'static str; 4] = ["std", "sync", "mutex", "Mutex"];
pub const OPEN_OPTIONS: [&'static str; 3] = ["std", "fs", "OpenOptions"];
pub const OPTION: [&'static str; 3] = ["core", "option", "Option"];

View File

@ -0,0 +1,21 @@
#![feature(plugin)]
#![plugin(clippy)]
use std::sync::Arc;
use std::mem::forget as forgetSomething;
use std::mem as memstuff;
#[deny(mem_forget)]
fn main() {
let five: i32 = 5;
forgetSomething(five);
//~^ ERROR usage of std::mem::forget
let six: Arc<i32> = Arc::new(6);
memstuff::forget(six);
//~^ ERROR usage of std::mem::forget
std::mem::forget(7);
//~^ ERROR usage of std::mem::forget
}