]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/entry.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / entry.rs
index 66ae2966dfbb580bf9771a907cba49c5e01281e4..16009d8ab86d8e14b3078d7187015ddff8097d6f 100644 (file)
@@ -1,6 +1,5 @@
-use crate::utils::sym;
 use crate::utils::SpanlessEq;
-use crate::utils::{get_item_name, higher, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
+use crate::utils::{get_item_name, higher, match_type, paths, snippet, snippet_opt, span_lint_and_then, walk_ptrs_ty};
 use if_chain::if_chain;
 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
 use rustc::hir::*;
     ///
     /// **Known problems:** Some false negatives, eg.:
     /// ```rust
-    /// let k = &key;
-    /// if !m.contains_key(k) {
-    ///     m.insert(k.clone(), v);
+    /// # use std::collections::HashMap;
+    /// # let mut map = HashMap::new();
+    /// # let v = 1;
+    /// # let k = 1;
+    /// if !map.contains_key(&k) {
+    ///     map.insert(k.clone(), v);
     /// }
     /// ```
     ///
     /// **Example:**
     /// ```rust
-    /// if !m.contains_key(&k) {
-    ///     m.insert(k, v)
+    /// # use std::collections::HashMap;
+    /// # let mut map = HashMap::new();
+    /// # let k = 1;
+    /// # let v = 1;
+    /// if !map.contains_key(&k) {
+    ///     map.insert(k, v);
     /// }
     /// ```
-    /// can be rewritten as:
+    /// can both be rewritten as:
     /// ```rust
-    /// m.entry(k).or_insert(v);
+    /// # use std::collections::HashMap;
+    /// # let mut map = HashMap::new();
+    /// # let k = 1;
+    /// # let v = 1;
+    /// map.entry(k).or_insert(v);
     /// ```
     pub MAP_ENTRY,
     perf,
@@ -92,16 +102,16 @@ fn check_cond<'a, 'tcx, 'b>(
     if_chain! {
         if let ExprKind::MethodCall(ref path, _, ref params) = check.node;
         if params.len() >= 2;
-        if path.ident.name == *sym::contains_key;
+        if path.ident.name == sym!(contains_key);
         if let ExprKind::AddrOf(_, ref key) = params[1].node;
         then {
             let map = &params[0];
             let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(map));
 
-            return if match_type(cx, obj_ty, &*paths::BTREEMAP) {
+            return if match_type(cx, obj_ty, &paths::BTREEMAP) {
                 Some(("BTreeMap", map, key))
             }
-            else if match_type(cx, obj_ty, &*paths::HASHMAP) {
+            else if match_type(cx, obj_ty, &paths::HASHMAP) {
                 Some(("HashMap", map, key))
             }
             else {
@@ -113,7 +123,7 @@ fn check_cond<'a, 'tcx, 'b>(
     None
 }
 
-struct InsertVisitor<'a, 'tcx: 'a, 'b> {
+struct InsertVisitor<'a, 'tcx, 'b> {
     cx: &'a LateContext<'a, 'tcx>,
     span: Span,
     ty: &'static str,
@@ -127,9 +137,10 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
         if_chain! {
             if let ExprKind::MethodCall(ref path, _, ref params) = expr.node;
             if params.len() == 3;
-            if path.ident.name == *sym::insert;
+            if path.ident.name == sym!(insert);
             if get_item_name(self.cx, self.map) == get_item_name(self.cx, &params[0]);
             if SpanlessEq::new(self.cx).eq_expr(self.key, &params[1]);
+            if snippet_opt(self.cx, self.map.span) == snippet_opt(self.cx, params[0].span);
             then {
                 span_lint_and_then(self.cx, MAP_ENTRY, self.span,
                                    &format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |db| {