2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::{snippet, snippet_opt, snippet_with_applicability};
|
2021-03-13 23:01:03 +00:00
|
|
|
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
|
2021-03-16 16:06:34 +00:00
|
|
|
use clippy_utils::SpanlessEq;
|
|
|
|
use clippy_utils::{get_item_name, paths};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind, UnOp};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2021-02-27 04:04:02 +00:00
|
|
|
use rustc_span::sym;
|
2015-12-21 23:35:56 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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
|
2019-08-03 06:01:27 +00:00
|
|
|
/// # 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);
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-03 06:01:27 +00:00
|
|
|
/// # use std::collections::HashMap;
|
|
|
|
/// # let mut map = HashMap::new();
|
|
|
|
/// # let k = 1;
|
|
|
|
/// # let v = 1;
|
|
|
|
/// if !map.contains_key(&k) {
|
|
|
|
/// map.insert(k, v);
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-08-03 06:01:27 +00:00
|
|
|
/// can both be rewritten as:
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2019-08-03 06:01:27 +00:00
|
|
|
/// # use std::collections::HashMap;
|
|
|
|
/// # let mut map = HashMap::new();
|
|
|
|
/// # let k = 1;
|
|
|
|
/// # let v = 1;
|
|
|
|
/// map.entry(k).or_insert(v);
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2016-01-12 19:23:28 +00:00
|
|
|
pub MAP_ENTRY,
|
2018-03-28 13:24:26 +00:00
|
|
|
perf,
|
2016-01-12 19:23:28 +00:00
|
|
|
"use of `contains_key` followed by `insert` on a `HashMap` or `BTreeMap`"
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(HashMapPass => [MAP_ENTRY]);
|
2015-12-21 23:35:56 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for HashMapPass {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-01-01 18:38:11 +00:00
|
|
|
if let ExprKind::If(ref check, ref then_block, ref else_block) = expr.kind {
|
2021-02-09 08:15:53 +00:00
|
|
|
if let ExprKind::Unary(UnOp::Not, ref check) = check.kind {
|
2016-02-18 19:19:16 +00: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 09:06:32 +00:00
|
|
|
let sole_expr = {
|
2018-11-27 20:14:15 +00:00
|
|
|
else_block.is_none()
|
2019-09-27 15:16:06 +00:00
|
|
|
&& if let ExprKind::Block(ref then_block, _) = then_block.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
(then_block.expr.is_some() as usize) + then_block.stmts.len() == 1
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
2019-09-25 11:38:17 +00:00
|
|
|
// XXXManishearth we can also check for if/else blocks containing `None`.
|
2017-03-31 21:36:45 +00:00
|
|
|
};
|
2016-01-03 16:19:49 +00:00
|
|
|
|
2016-02-18 19:19:16 +00:00
|
|
|
let mut visitor = InsertVisitor {
|
2018-03-15 15:07:15 +00:00
|
|
|
cx,
|
2016-02-18 19:19:16 +00:00
|
|
|
span: expr.span,
|
2018-03-15 15:07:15 +00:00
|
|
|
ty,
|
|
|
|
map,
|
|
|
|
key,
|
|
|
|
sole_expr,
|
2016-02-18 19:19:16 +00:00
|
|
|
};
|
2016-01-03 15:31:28 +00:00
|
|
|
|
2017-03-31 17:23:35 +00:00
|
|
|
walk_expr(&mut visitor, &**then_block);
|
2016-01-12 19:23:28 +00:00
|
|
|
}
|
2016-02-18 19:19:16 +00: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 15:07:15 +00:00
|
|
|
cx,
|
2016-02-18 19:19:16 +00:00
|
|
|
span: expr.span,
|
2018-03-15 15:07:15 +00:00
|
|
|
ty,
|
|
|
|
map,
|
|
|
|
key,
|
2016-02-18 19:19:16 +00:00
|
|
|
sole_expr: false,
|
|
|
|
};
|
2016-01-12 19:23:28 +00:00
|
|
|
|
2016-02-18 19:19:16 +00:00
|
|
|
walk_expr(&mut visitor, else_block);
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_cond<'a>(cx: &LateContext<'_>, check: &'a Expr<'a>) -> Option<(&'static str, &'a Expr<'a>, &'a Expr<'a>)> {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref params, _) = check.kind;
|
2017-10-23 19:18:02 +00:00
|
|
|
if params.len() >= 2;
|
2019-05-17 21:53:54 +00:00
|
|
|
if path.ident.name == sym!(contains_key);
|
2019-11-27 22:30:10 +00:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref key) = params[1].kind;
|
2017-10-23 19:18:02 +00:00
|
|
|
then {
|
|
|
|
let map = ¶ms[0];
|
2020-09-24 12:49:22 +00:00
|
|
|
let obj_ty = cx.typeck_results().expr_ty(map).peel_refs();
|
2017-11-04 19:55:56 +00:00
|
|
|
|
2019-05-17 21:53:54 +00:00
|
|
|
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
|
2017-10-23 19:18:02 +00:00
|
|
|
Some(("BTreeMap", map, key))
|
|
|
|
}
|
2021-02-27 04:04:02 +00:00
|
|
|
else if is_type_diagnostic_item(cx, obj_ty, sym::hashmap_type) {
|
2017-10-23 19:18:02 +00:00
|
|
|
Some(("HashMap", map, key))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
};
|
2016-02-18 19:19:16 +00:00
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
struct InsertVisitor<'a, 'tcx, 'b> {
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2016-02-18 19:19:16 +00:00
|
|
|
span: Span,
|
|
|
|
ty: &'static str,
|
2019-12-27 07:12:26 +00:00
|
|
|
map: &'b Expr<'b>,
|
|
|
|
key: &'b Expr<'b>,
|
2016-02-18 19:19:16 +00:00
|
|
|
sole_expr: bool,
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(ref path, _, ref params, _) = expr.kind;
|
2017-10-23 19:18:02 +00:00
|
|
|
if params.len() == 3;
|
2019-05-17 21:53:54 +00:00
|
|
|
if path.ident.name == sym!(insert);
|
2017-10-23 19:18:02 +00: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]);
|
2019-09-03 00:41:37 +00:00
|
|
|
if snippet_opt(self.cx, self.map.span) == snippet_opt(self.cx, params[0].span);
|
2017-10-23 19:18:02 +00:00
|
|
|
then {
|
|
|
|
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
|
2020-04-17 06:08:00 +00:00
|
|
|
&format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |diag| {
|
2017-10-23 19:18:02 +00:00
|
|
|
if self.sole_expr {
|
2019-09-25 11:29:39 +00:00
|
|
|
let mut app = Applicability::MachineApplicable;
|
2019-09-25 11:38:17 +00:00
|
|
|
let help = format!("{}.entry({}).or_insert({});",
|
2019-09-25 11:29:39 +00:00
|
|
|
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));
|
2017-11-04 19:55:56 +00:00
|
|
|
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2018-09-18 15:07:54 +00:00
|
|
|
self.span,
|
|
|
|
"consider using",
|
|
|
|
help,
|
2018-09-18 17:01:17 +00:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2018-09-18 15:07:54 +00:00
|
|
|
);
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-09-25 11:29:39 +00:00
|
|
|
let help = format!("consider using `{}.entry({})`",
|
2017-10-23 19:18:02 +00:00
|
|
|
snippet(self.cx, self.map.span, "map"),
|
|
|
|
snippet(self.cx, params[1].span, ".."));
|
2017-11-04 19:55:56 +00:00
|
|
|
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_label(
|
2018-09-18 15:07:54 +00:00
|
|
|
self.span,
|
2019-09-25 11:29:39 +00:00
|
|
|
&help,
|
2018-09-18 15:07:54 +00:00
|
|
|
);
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
|
|
|
|
if !self.sole_expr {
|
|
|
|
walk_expr(self, expr);
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2017-05-12 10:02:42 +00:00
|
|
|
NestedVisitorMap::None
|
2016-12-06 10:32:21 +00:00
|
|
|
}
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|