]> 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 c59bfd1ad92dc24aa3f9968254b22892763ffcf6..16009d8ab86d8e14b3078d7187015ddff8097d6f 100644 (file)
@@ -1,63 +1,58 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
 use crate::utils::SpanlessEq;
-use crate::utils::{get_item_name, 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::*;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
 use syntax::source_map::Span;
 
-/// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
-/// or `BTreeMap`.
-///
-/// **Why is this bad?** Using `entry` is more efficient.
-///
-/// **Known problems:** Some false negatives, eg.:
-/// ```rust
-/// let k = &key;
-/// if !m.contains_key(k) {
-///     m.insert(k.clone(), v);
-/// }
-/// ```
-///
-/// **Example:**
-/// ```rust
-/// if !m.contains_key(&k) {
-///     m.insert(k, v)
-/// }
-/// ```
-/// can be rewritten as:
-/// ```rust
-/// m.entry(k).or_insert(v);
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
+    /// or `BTreeMap`.
+    ///
+    /// **Why is this bad?** Using `entry` is more efficient.
+    ///
+    /// **Known problems:** Some false negatives, eg.:
+    /// ```rust
+    /// # 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
+    /// # 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 both be rewritten as:
+    /// ```rust
+    /// # 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,
     "use of `contains_key` followed by `insert` on a `HashMap` or `BTreeMap`"
 }
 
-#[derive(Copy, Clone)]
-pub struct HashMapLint;
-
-impl LintPass for HashMapLint {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(MAP_ENTRY)
-    }
-}
+declare_lint_pass!(HashMapPass => [MAP_ENTRY]);
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapLint {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
-        if let ExprKind::If(ref check, ref then_block, ref else_block) = expr.node {
+        if let Some((ref check, ref then_block, ref else_block)) = higher::if_block(&expr) {
             if let ExprKind::Unary(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); }`
@@ -107,7 +102,7 @@ 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 == "contains_key";
+        if path.ident.name == sym!(contains_key);
         if let ExprKind::AddrOf(_, ref key) = params[1].node;
         then {
             let map = &params[0];
@@ -128,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,
@@ -142,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 == "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| {
@@ -154,7 +150,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
                                            snippet(self.cx, params[1].span, ".."),
                                            snippet(self.cx, params[2].span, ".."));
 
-                        db.span_suggestion_with_applicability(
+                        db.span_suggestion(
                             self.span,
                             "consider using",
                             help,
@@ -166,7 +162,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
                                            snippet(self.cx, self.map.span, "map"),
                                            snippet(self.cx, params[1].span, ".."));
 
-                        db.span_suggestion_with_applicability(
+                        db.span_suggestion(
                             self.span,
                             "consider using",
                             help,