2 use rustc::hir::intravisit::{Visitor, walk_expr, walk_block};
4 use syntax::codemap::Span;
6 use utils::{get_item_name, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
8 /// **What it does:** This lint checks for uses of `contains_key` + `insert` on `HashMap` or
11 /// **Why is this bad?** Using `entry` is more efficient.
13 /// **Known problems:** Some false negatives, eg.:
16 /// if !m.contains_key(k) { m.insert(k.clone(), v); }
21 /// if !m.contains_key(&k) { m.insert(k, v) }
23 /// can be rewritten as:
25 /// m.entry(k).or_insert(v);
30 "use of `contains_key` followed by `insert` on a `HashMap` or `BTreeMap`"
34 pub struct HashMapLint;
36 impl LintPass for HashMapLint {
37 fn get_lints(&self) -> LintArray {
38 lint_array!(MAP_ENTRY)
42 impl LateLintPass for HashMapLint {
43 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
44 if let ExprIf(ref check, ref then_block, ref else_block) = expr.node {
45 if let ExprUnary(UnOp::UnNot, ref check) = check.node {
46 if let Some((ty, map, key)) = check_cond(cx, check) {
47 // in case of `if !m.contains_key(&k) { m.insert(k, v); }`
48 // we can give a better error message
49 let sole_expr = else_block.is_none() &&
50 ((then_block.expr.is_some() as usize) + then_block.stmts.len() == 1);
52 let mut visitor = InsertVisitor {
61 walk_block(&mut visitor, then_block);
63 } else if let Some(ref else_block) = *else_block {
64 if let Some((ty, map, key)) = check_cond(cx, check) {
65 let mut visitor = InsertVisitor {
74 walk_expr(&mut visitor, else_block);
81 fn check_cond<'a, 'tcx, 'b>(cx: &'a LateContext<'a, 'tcx>, check: &'b Expr) -> Option<(&'static str, &'b Expr, &'b Expr)> {
83 let ExprMethodCall(ref name, _, ref params) = check.node,
85 name.node.as_str() == "contains_key",
86 let ExprAddrOf(_, ref key) = params[1].node
89 let obj_ty = walk_ptrs_ty(cx.tcx.expr_ty(map));
91 return if match_type(cx, obj_ty, &paths::BTREEMAP) {
92 Some(("BTreeMap", map, key))
94 else if match_type(cx, obj_ty, &paths::HASHMAP) {
95 Some(("HashMap", map, key))
105 struct InsertVisitor<'a, 'tcx: 'a, 'b> {
106 cx: &'a LateContext<'a, 'tcx>,
114 impl<'a, 'tcx, 'v, 'b> Visitor<'v> for InsertVisitor<'a, 'tcx, 'b> {
115 fn visit_expr(&mut self, expr: &'v Expr) {
117 let ExprMethodCall(ref name, _, ref params) = expr.node,
119 name.node.as_str() == "insert",
120 get_item_name(self.cx, self.map) == get_item_name(self.cx, &*params[0]),
121 SpanlessEq::new(self.cx).eq_expr(self.key, ¶ms[1])
124 span_lint_and_then(self.cx, MAP_ENTRY, self.span,
125 &format!("usage of `contains_key` followed by `insert` on `{}`", self.ty), |db| {
127 let help = format!("{}.entry({}).or_insert({})",
128 snippet(self.cx, self.map.span, "map"),
129 snippet(self.cx, params[1].span, ".."),
130 snippet(self.cx, params[2].span, ".."));
132 db.span_suggestion(self.span, "Consider using", help);
135 let help = format!("Consider using `{}.entry({})`",
136 snippet(self.cx, self.map.span, "map"),
137 snippet(self.cx, params[1].span, ".."));
139 db.span_note(self.span, &help);
145 walk_expr(self, expr);