2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::hir::intravisit::{Visitor, walk_expr, walk_block};
|
2016-04-14 16:13:15 +00:00
|
|
|
use rustc::lint::*;
|
2015-12-21 23:35:56 +00:00
|
|
|
use syntax::codemap::Span;
|
2016-02-06 19:13:25 +00:00
|
|
|
use utils::SpanlessEq;
|
2016-04-14 16:13:15 +00:00
|
|
|
use utils::{get_item_name, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
|
2015-12-21 23:35:56 +00:00
|
|
|
|
2016-01-12 19:23:28 +00:00
|
|
|
/// **What it does:** This lint checks for uses of `contains_key` + `insert` on `HashMap` or
|
|
|
|
/// `BTreeMap`.
|
2015-12-21 23:35:56 +00:00
|
|
|
///
|
2016-01-12 19:23:28 +00:00
|
|
|
/// **Why is this bad?** Using `entry` is more efficient.
|
2015-12-21 23:35:56 +00:00
|
|
|
///
|
2016-01-03 15:31:28 +00:00
|
|
|
/// **Known problems:** Some false negatives, eg.:
|
|
|
|
/// ```
|
|
|
|
/// let k = &key;
|
|
|
|
/// if !m.contains_key(k) { m.insert(k.clone(), v); }
|
|
|
|
/// ```
|
2015-12-21 23:35:56 +00:00
|
|
|
///
|
2016-01-03 16:19:49 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// if !m.contains_key(&k) { m.insert(k, v) }
|
|
|
|
/// ```
|
|
|
|
/// can be rewritten as:
|
|
|
|
/// ```rust
|
|
|
|
/// m.entry(k).or_insert(v);
|
|
|
|
/// ```
|
2015-12-21 23:35:56 +00:00
|
|
|
declare_lint! {
|
2016-01-12 19:23:28 +00:00
|
|
|
pub MAP_ENTRY,
|
2015-12-21 23:35:56 +00:00
|
|
|
Warn,
|
2016-01-12 19:23:28 +00:00
|
|
|
"use of `contains_key` followed by `insert` on a `HashMap` or `BTreeMap`"
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct HashMapLint;
|
|
|
|
|
|
|
|
impl LintPass for HashMapLint {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-01-12 19:23:28 +00:00
|
|
|
lint_array!(MAP_ENTRY)
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass for HashMapLint {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
2016-02-18 19:19:16 +00:00
|
|
|
if let ExprIf(ref check, ref then_block, ref else_block) = expr.node {
|
|
|
|
if let ExprUnary(UnOp::UnNot, ref check) = check.node {
|
|
|
|
if let Some((ty, map, key)) = check_cond(cx, check) {
|
|
|
|
// in case of `if !m.contains_key(&k) { m.insert(k, v); }`
|
|
|
|
// we can give a better error message
|
|
|
|
let sole_expr = else_block.is_none() &&
|
2016-02-29 11:19:32 +00:00
|
|
|
((then_block.expr.is_some() as usize) + then_block.stmts.len() == 1);
|
2016-01-03 16:19:49 +00:00
|
|
|
|
2016-02-18 19:19:16 +00:00
|
|
|
let mut visitor = InsertVisitor {
|
|
|
|
cx: cx,
|
|
|
|
span: expr.span,
|
|
|
|
ty: ty,
|
|
|
|
map: map,
|
|
|
|
key: key,
|
|
|
|
sole_expr: sole_expr,
|
|
|
|
};
|
2016-01-03 15:31:28 +00:00
|
|
|
|
2016-02-18 19:19:16 +00:00
|
|
|
walk_block(&mut visitor, then_block);
|
2016-01-12 19:23:28 +00:00
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
} else if let Some(ref else_block) = *else_block {
|
|
|
|
if let Some((ty, map, key)) = check_cond(cx, check) {
|
|
|
|
let mut visitor = InsertVisitor {
|
|
|
|
cx: cx,
|
|
|
|
span: expr.span,
|
|
|
|
ty: ty,
|
|
|
|
map: map,
|
|
|
|
key: key,
|
|
|
|
sole_expr: false,
|
|
|
|
};
|
2016-01-12 19:23:28 +00:00
|
|
|
|
2016-02-18 19:19:16 +00:00
|
|
|
walk_expr(&mut visitor, else_block);
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:19:16 +00:00
|
|
|
fn check_cond<'a, 'tcx, 'b>(cx: &'a LateContext<'a, 'tcx>, check: &'b Expr) -> Option<(&'static str, &'b Expr, &'b Expr)> {
|
|
|
|
if_let_chain! {[
|
|
|
|
let ExprMethodCall(ref name, _, ref params) = check.node,
|
|
|
|
params.len() >= 2,
|
|
|
|
name.node.as_str() == "contains_key",
|
|
|
|
let ExprAddrOf(_, ref key) = params[1].node
|
|
|
|
], {
|
|
|
|
let map = ¶ms[0];
|
|
|
|
let obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(map));
|
|
|
|
|
2016-04-14 16:13:15 +00:00
|
|
|
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
|
2016-02-18 19:19:16 +00:00
|
|
|
Some(("BTreeMap", map, key))
|
|
|
|
}
|
2016-04-14 16:13:15 +00:00
|
|
|
else if match_type(cx, obj_ty, &paths::HASHMAP) {
|
2016-02-18 19:19:16 +00:00
|
|
|
Some(("HashMap", map, key))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
}}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InsertVisitor<'a, 'tcx: 'a, 'b> {
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
span: Span,
|
|
|
|
ty: &'static str,
|
|
|
|
map: &'b Expr,
|
|
|
|
key: &'b Expr,
|
|
|
|
sole_expr: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, 'v, 'b> Visitor<'v> for InsertVisitor<'a, 'tcx, 'b> {
|
|
|
|
fn visit_expr(&mut self, expr: &'v Expr) {
|
|
|
|
if_let_chain! {[
|
2015-12-21 23:35:56 +00:00
|
|
|
let ExprMethodCall(ref name, _, ref params) = expr.node,
|
2016-01-03 15:31:28 +00:00
|
|
|
params.len() == 3,
|
2015-12-21 23:35:56 +00:00
|
|
|
name.node.as_str() == "insert",
|
2016-02-18 19:19:16 +00:00
|
|
|
get_item_name(self.cx, self.map) == get_item_name(self.cx, &*params[0]),
|
|
|
|
SpanlessEq::new(self.cx).eq_expr(self.key, ¶ms[1])
|
2015-12-21 23:35:56 +00:00
|
|
|
], {
|
2016-02-18 19:19:16 +00:00
|
|
|
|
|
|
|
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
|
|
|
|
&format!("usage of `contains_key` followed by `insert` on `{}`", self.ty), |db| {
|
|
|
|
if self.sole_expr {
|
|
|
|
let help = format!("{}.entry({}).or_insert({})",
|
|
|
|
snippet(self.cx, self.map.span, "map"),
|
|
|
|
snippet(self.cx, params[1].span, ".."),
|
|
|
|
snippet(self.cx, params[2].span, ".."));
|
|
|
|
|
|
|
|
db.span_suggestion(self.span, "Consider using", help);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let help = format!("Consider using `{}.entry({})`",
|
|
|
|
snippet(self.cx, self.map.span, "map"),
|
|
|
|
snippet(self.cx, params[1].span, ".."));
|
|
|
|
|
|
|
|
db.span_note(self.span, &help);
|
|
|
|
}
|
2016-01-13 16:17:19 +00:00
|
|
|
});
|
2016-02-18 19:19:16 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
if !self.sole_expr {
|
|
|
|
walk_expr(self, expr);
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|