X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fentry.rs;h=4403ef45ca339aa56ebd224792749fc29e85bdc1;hb=6d1225981177587fbb68d9c4902c770c3dbaafb0;hp=83f78b2d72d60ba2613da75b2607b7fc087a2aa8;hpb=7db5d0e6de112440dd13e78e2cc0bb1a7be25641;p=rust.git diff --git a/clippy_lints/src/entry.rs b/clippy_lints/src/entry.rs index 83f78b2d72d..4403ef45ca3 100644 --- a/clippy_lints/src/entry.rs +++ b/clippy_lints/src/entry.rs @@ -1,11 +1,13 @@ 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}; +use crate::utils::{snippet_with_applicability, span_lint_and_then, walk_ptrs_ty}; use if_chain::if_chain; +use rustc::declare_lint_pass; use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; +use rustc_session::declare_tool_lint; use syntax::source_map::Span; declare_clippy_lint! { @@ -16,21 +18,32 @@ /// /// **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, @@ -42,17 +55,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { 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 ExprKind::Unary(UnOp::UnNot, ref check) = check.kind { 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() - && if let ExprKind::Block(ref then_block, _) = then_block.node { + && if let ExprKind::Block(ref then_block, _) = then_block.kind { (then_block.expr.is_some() as usize) + then_block.stmts.len() == 1 } else { true } + // XXXManishearth we can also check for if/else blocks containing `None`. }; let mut visitor = InsertVisitor { @@ -89,10 +103,10 @@ fn check_cond<'a, 'tcx, 'b>( check: &'b Expr, ) -> Option<(&'static str, &'b Expr, &'b Expr)> { if_chain! { - if let ExprKind::MethodCall(ref path, _, ref params) = check.node; + if let ExprKind::MethodCall(ref path, _, ref params) = check.kind; if params.len() >= 2; if path.ident.name == sym!(contains_key); - if let ExprKind::AddrOf(_, ref key) = params[1].node; + if let ExprKind::AddrOf(BorrowKind::Ref, _, ref key) = params[1].kind; then { let map = ¶ms[0]; let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(map)); @@ -124,19 +138,21 @@ struct InsertVisitor<'a, 'tcx, 'b> { impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> { fn visit_expr(&mut self, expr: &'tcx Expr) { if_chain! { - if let ExprKind::MethodCall(ref path, _, ref params) = expr.node; + if let ExprKind::MethodCall(ref path, _, ref params) = expr.kind; if params.len() == 3; if path.ident.name == sym!(insert); if get_item_name(self.cx, self.map) == get_item_name(self.cx, ¶ms[0]); if SpanlessEq::new(self.cx).eq_expr(self.key, ¶ms[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| { 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, "..")); + let mut app = Applicability::MachineApplicable; + let help = format!("{}.entry({}).or_insert({});", + snippet_with_applicability(self.cx, self.map.span, "map", &mut app), + snippet_with_applicability(self.cx, params[1].span, "..", &mut app), + snippet_with_applicability(self.cx, params[2].span, "..", &mut app)); db.span_suggestion( self.span, @@ -146,15 +162,13 @@ fn visit_expr(&mut self, expr: &'tcx Expr) { ); } else { - let help = format!("{}.entry({})", + let help = format!("consider using `{}.entry({})`", snippet(self.cx, self.map.span, "map"), snippet(self.cx, params[1].span, "..")); - db.span_suggestion( + db.span_label( self.span, - "consider using", - help, - Applicability::MachineApplicable, // snippet + &help, ); } });