2018-10-06 11:18:06 -05:00
|
|
|
// 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.
|
|
|
|
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::rustc::hir::*;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::rustc_errors::Applicability;
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::syntax::source_map::Span;
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::SpanlessEq;
|
|
|
|
use crate::utils::{get_item_name, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
|
2018-11-27 14:14:15 -06:00
|
|
|
use if_chain::if_chain;
|
2015-12-21 17:35:56 -06:00
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
|
|
|
|
/// or `BTreeMap`.
|
2015-12-21 17:35:56 -06:00
|
|
|
///
|
2016-01-12 13:23:28 -06:00
|
|
|
/// **Why is this bad?** Using `entry` is more efficient.
|
2015-12-21 17:35:56 -06:00
|
|
|
///
|
2016-01-03 09:31:28 -06:00
|
|
|
/// **Known problems:** Some false negatives, eg.:
|
2016-07-29 13:36:33 -05:00
|
|
|
/// ```rust
|
2016-01-03 09:31:28 -06:00
|
|
|
/// let k = &key;
|
2018-11-27 14:14:15 -06:00
|
|
|
/// if !m.contains_key(k) {
|
|
|
|
/// m.insert(k.clone(), v);
|
|
|
|
/// }
|
2016-01-03 09:31:28 -06:00
|
|
|
/// ```
|
2015-12-21 17:35:56 -06:00
|
|
|
///
|
2016-01-03 10:19:49 -06:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2018-11-27 14:14:15 -06:00
|
|
|
/// if !m.contains_key(&k) {
|
|
|
|
/// m.insert(k, v)
|
|
|
|
/// }
|
2016-01-03 10:19:49 -06:00
|
|
|
/// ```
|
|
|
|
/// can be rewritten as:
|
|
|
|
/// ```rust
|
|
|
|
/// m.entry(k).or_insert(v);
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-01-12 13:23:28 -06:00
|
|
|
pub MAP_ENTRY,
|
2018-03-28 08:24:26 -05:00
|
|
|
perf,
|
2016-01-12 13:23:28 -06:00
|
|
|
"use of `contains_key` followed by `insert` on a `HashMap` or `BTreeMap`"
|
2015-12-21 17:35:56 -06:00
|
|
|
}
|
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-12-21 17:35:56 -06:00
|
|
|
pub struct HashMapLint;
|
|
|
|
|
|
|
|
impl LintPass for HashMapLint {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-01-12 13:23:28 -06:00
|
|
|
lint_array!(MAP_ENTRY)
|
2015-12-21 17:35:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapLint {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::If(ref check, ref then_block, ref else_block) = expr.node {
|
|
|
|
if let ExprKind::Unary(UnOp::UnNot, ref check) = check.node {
|
2016-02-18 13:19:16 -06:00
|
|
|
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
|
2017-04-12 04:06:32 -05:00
|
|
|
let sole_expr = {
|
2018-11-27 14:14:15 -06:00
|
|
|
else_block.is_none()
|
|
|
|
&& if let ExprKind::Block(ref then_block, _) = then_block.node {
|
|
|
|
(then_block.expr.is_some() as usize) + then_block.stmts.len() == 1
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
2017-03-31 16:36:45 -05:00
|
|
|
};
|
2016-01-03 10:19:49 -06:00
|
|
|
|
2016-02-18 13:19:16 -06:00
|
|
|
let mut visitor = InsertVisitor {
|
2018-03-15 10:07:15 -05:00
|
|
|
cx,
|
2016-02-18 13:19:16 -06:00
|
|
|
span: expr.span,
|
2018-03-15 10:07:15 -05:00
|
|
|
ty,
|
|
|
|
map,
|
|
|
|
key,
|
|
|
|
sole_expr,
|
2016-02-18 13:19:16 -06:00
|
|
|
};
|
2016-01-03 09:31:28 -06:00
|
|
|
|
2017-03-31 12:23:35 -05:00
|
|
|
walk_expr(&mut visitor, &**then_block);
|
2016-01-12 13:23:28 -06:00
|
|
|
}
|
2016-02-18 13:19:16 -06:00
|
|
|
} else if let Some(ref else_block) = *else_block {
|
|
|
|
if let Some((ty, map, key)) = check_cond(cx, check) {
|
|
|
|
let mut visitor = InsertVisitor {
|
2018-03-15 10:07:15 -05:00
|
|
|
cx,
|
2016-02-18 13:19:16 -06:00
|
|
|
span: expr.span,
|
2018-03-15 10:07:15 -05:00
|
|
|
ty,
|
|
|
|
map,
|
|
|
|
key,
|
2016-02-18 13:19:16 -06:00
|
|
|
sole_expr: false,
|
|
|
|
};
|
2016-01-12 13:23:28 -06:00
|
|
|
|
2016-02-18 13:19:16 -06:00
|
|
|
walk_expr(&mut visitor, else_block);
|
2015-12-21 17:35:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 05:14:54 -06:00
|
|
|
fn check_cond<'a, 'tcx, 'b>(
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
2017-08-09 02:30:56 -05:00
|
|
|
check: &'b Expr,
|
2016-12-21 05:14:54 -06:00
|
|
|
) -> Option<(&'static str, &'b Expr, &'b Expr)> {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref params) = check.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
if params.len() >= 2;
|
2018-06-28 08:46:58 -05:00
|
|
|
if path.ident.name == "contains_key";
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::AddrOf(_, ref key) = params[1].node;
|
2017-10-23 14:18:02 -05:00
|
|
|
then {
|
|
|
|
let map = ¶ms[0];
|
|
|
|
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(map));
|
2017-11-04 14:55:56 -05:00
|
|
|
|
2017-10-23 14:18:02 -05:00
|
|
|
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
|
|
|
|
Some(("BTreeMap", map, key))
|
|
|
|
}
|
|
|
|
else if match_type(cx, obj_ty, &paths::HASHMAP) {
|
|
|
|
Some(("HashMap", map, key))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
};
|
2016-02-18 13:19:16 -06:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-02-18 13:19:16 -06:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:32:21 -06:00
|
|
|
impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr) {
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref params) = expr.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
if params.len() == 3;
|
2018-06-28 08:46:58 -05:00
|
|
|
if path.ident.name == "insert";
|
2017-10-23 14:18:02 -05:00
|
|
|
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]);
|
|
|
|
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, ".."));
|
2017-11-04 14:55:56 -05:00
|
|
|
|
2018-09-15 11:14:08 -05:00
|
|
|
db.span_suggestion_with_applicability(
|
2018-09-18 10:07:54 -05:00
|
|
|
self.span,
|
|
|
|
"consider using",
|
|
|
|
help,
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 10:07:54 -05:00
|
|
|
);
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let help = format!("{}.entry({})",
|
|
|
|
snippet(self.cx, self.map.span, "map"),
|
|
|
|
snippet(self.cx, params[1].span, ".."));
|
2017-11-04 14:55:56 -05:00
|
|
|
|
2018-09-15 11:14:08 -05:00
|
|
|
db.span_suggestion_with_applicability(
|
2018-09-18 10:07:54 -05:00
|
|
|
self.span,
|
|
|
|
"consider using",
|
|
|
|
help,
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 10:07:54 -05:00
|
|
|
);
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 13:19:16 -06:00
|
|
|
|
|
|
|
if !self.sole_expr {
|
|
|
|
walk_expr(self, expr);
|
2015-12-21 17:35:56 -06:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 04:32:21 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2017-05-12 05:02:42 -05:00
|
|
|
NestedVisitorMap::None
|
2016-12-06 04:32:21 -06:00
|
|
|
}
|
2015-12-21 17:35:56 -06:00
|
|
|
}
|