2021-03-25 13:25:04 +00:00
|
|
|
use clippy_utils::{
|
|
|
|
diagnostics::span_lint_and_sugg,
|
|
|
|
is_expr_final_block_expr, is_expr_used_or_unified, match_def_path, paths, peel_hir_expr_while,
|
2021-03-25 13:36:38 +00:00
|
|
|
source::{reindent_multiline, snippet_indent, snippet_with_applicability, snippet_with_context},
|
2021-03-25 13:25:04 +00:00
|
|
|
SpanlessEq,
|
|
|
|
};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-03-25 13:25:04 +00:00
|
|
|
use rustc_hir::{
|
|
|
|
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
|
|
|
|
Expr, ExprKind, Guard, Local, Stmt, StmtKind, UnOp,
|
|
|
|
};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-03-25 13:25:04 +00:00
|
|
|
use rustc_span::{Span, SyntaxContext, DUMMY_SP};
|
|
|
|
use std::fmt::Write;
|
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.
|
|
|
|
///
|
2021-03-25 13:25:04 +00:00
|
|
|
/// **Known problems:** The suggestion may have type inference errors in some cases. e.g.
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2021-03-25 13:25:04 +00:00
|
|
|
/// let mut map = std::collections::HashMap::new();
|
|
|
|
/// let _ = if !map.contains_key(&0) {
|
|
|
|
/// map.insert(0, 0)
|
|
|
|
/// } else {
|
|
|
|
/// None
|
|
|
|
/// };
|
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 {
|
2021-03-25 13:25:04 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-03-25 13:25:04 +00:00
|
|
|
let (cond_expr, then_expr, else_expr) = match expr.kind {
|
|
|
|
ExprKind::If(c, t, e) => (c, t, e),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
let (map_ty, contains_expr) = match try_parse_contains(cx, cond_expr) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return,
|
|
|
|
};
|
2016-01-03 16:19:49 +00:00
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
let then_search = match find_insert_calls(cx, &contains_expr, then_expr) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return,
|
|
|
|
};
|
2016-01-03 15:31:28 +00:00
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
let map_str = snippet_with_context(cx, contains_expr.map.span, contains_expr.call_ctxt, "..", &mut app).0;
|
|
|
|
let key_str = snippet_with_context(cx, contains_expr.key.span, contains_expr.call_ctxt, "..", &mut app).0;
|
2021-03-25 13:36:38 +00:00
|
|
|
let sugg = if let Some(else_expr) = else_expr {
|
|
|
|
// if .. { .. } else { .. }
|
|
|
|
let else_search = match find_insert_calls(cx, &contains_expr, else_expr) {
|
|
|
|
Some(search) if !(then_search.insertions.is_empty() && search.insertions.is_empty()) => search,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if then_search.insertions.is_empty() || else_search.insertions.is_empty() {
|
|
|
|
// if .. { insert } else { .. } or if .. { .. } else { then } of
|
|
|
|
let (then_str, else_str, entry_kind) = if else_search.insertions.is_empty() {
|
|
|
|
if contains_expr.negated {
|
|
|
|
(
|
|
|
|
then_search.snippet_vacant(cx, then_expr.span, &mut app),
|
|
|
|
snippet_with_applicability(cx, else_expr.span, "{ .. }", &mut app),
|
|
|
|
"Vacant(e)",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
then_search.snippet_occupied(cx, then_expr.span, &mut app),
|
|
|
|
snippet_with_applicability(cx, else_expr.span, "{ .. }", &mut app),
|
|
|
|
"Occupied(mut e)",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if contains_expr.negated {
|
|
|
|
(
|
|
|
|
else_search.snippet_occupied(cx, else_expr.span, &mut app),
|
|
|
|
snippet_with_applicability(cx, then_expr.span, "{ .. }", &mut app),
|
|
|
|
"Occupied(mut e)",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
else_search.snippet_vacant(cx, else_expr.span, &mut app),
|
|
|
|
snippet_with_applicability(cx, then_expr.span, "{ .. }", &mut app),
|
|
|
|
"Vacant(e)",
|
|
|
|
)
|
|
|
|
};
|
|
|
|
format!(
|
|
|
|
"if let {}::{} = {}.entry({}) {} else {}",
|
|
|
|
map_ty.entry_path(),
|
|
|
|
entry_kind,
|
|
|
|
map_str,
|
|
|
|
key_str,
|
|
|
|
then_str,
|
|
|
|
else_str,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// if .. { insert } else { insert }
|
|
|
|
let (then_str, else_str, then_entry, else_entry) = if contains_expr.negated {
|
|
|
|
(
|
|
|
|
then_search.snippet_vacant(cx, then_expr.span, &mut app),
|
|
|
|
else_search.snippet_occupied(cx, else_expr.span, &mut app),
|
|
|
|
"Vacant(e)",
|
|
|
|
"Occupied(mut e)",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
then_search.snippet_occupied(cx, then_expr.span, &mut app),
|
|
|
|
else_search.snippet_vacant(cx, else_expr.span, &mut app),
|
|
|
|
"Occupied(mut e)",
|
|
|
|
"Vacant(e)",
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let indent_str = snippet_indent(cx, expr.span);
|
|
|
|
let indent_str = indent_str.as_deref().unwrap_or("");
|
|
|
|
format!(
|
|
|
|
"match {}.entry({}) {{\n{indent} {entry}::{} => {}\n\
|
|
|
|
{indent} {entry}::{} => {}\n{indent}}}",
|
|
|
|
map_str,
|
|
|
|
key_str,
|
|
|
|
then_entry,
|
|
|
|
reindent_multiline(then_str.into(), true, Some(4 + indent_str.len())),
|
|
|
|
else_entry,
|
|
|
|
reindent_multiline(else_str.into(), true, Some(4 + indent_str.len())),
|
|
|
|
entry = map_ty.entry_path(),
|
|
|
|
indent = indent_str,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if then_search.insertions.is_empty() || !contains_expr.negated {
|
2021-03-25 13:25:04 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// if .. { insert }
|
|
|
|
match then_search.as_single_insertion() {
|
|
|
|
Some(insertion) if !insertion.value.can_have_side_effects() => {
|
|
|
|
format!(
|
|
|
|
"{}.entry({}).or_insert({});",
|
|
|
|
map_str,
|
|
|
|
key_str,
|
|
|
|
snippet_with_context(cx, insertion.value.span, insertion.call.span.ctxt(), "..", &mut app).0,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let (body_str, entry_kind) = if contains_expr.negated {
|
|
|
|
(then_search.snippet_vacant(cx, then_expr.span, &mut app), "Vacant(e)")
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
then_search.snippet_occupied(cx, then_expr.span, &mut app),
|
|
|
|
"Occupied(mut e)",
|
|
|
|
)
|
2016-02-18 19:19:16 +00:00
|
|
|
};
|
2021-03-25 13:25:04 +00:00
|
|
|
format!(
|
|
|
|
"if let {}::{} = {}.entry({}) {}",
|
|
|
|
map_ty.entry_path(),
|
|
|
|
entry_kind,
|
|
|
|
map_str,
|
|
|
|
key_str,
|
|
|
|
body_str,
|
|
|
|
)
|
|
|
|
},
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MAP_ENTRY,
|
|
|
|
expr.span,
|
|
|
|
&format!("usage of `contains_key` followed by `insert` on a `{}`", map_ty.name()),
|
|
|
|
"try this",
|
|
|
|
sugg,
|
|
|
|
app,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum MapType {
|
|
|
|
Hash,
|
|
|
|
BTree,
|
|
|
|
}
|
|
|
|
impl MapType {
|
|
|
|
fn name(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Hash => "HashMap",
|
|
|
|
Self::BTree => "BTreeMap",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn entry_path(self) -> &'staic str {
|
|
|
|
match self {
|
|
|
|
Self::Hash => "std::collections::hash_map::Entry",
|
|
|
|
Self::BTree => "std::collections::btree_map::Entry",
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
struct ContainsExpr<'tcx> {
|
|
|
|
negated: bool,
|
|
|
|
map: &'tcx Expr<'tcx>,
|
|
|
|
key: &'tcx Expr<'tcx>,
|
|
|
|
call_ctxt: SyntaxContext,
|
|
|
|
}
|
|
|
|
fn try_parse_contains(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Option<(MapType, ContainsExpr<'tcx>)> {
|
|
|
|
let mut negated = false;
|
|
|
|
let expr = peel_hir_expr_while(expr, |e| match e.kind {
|
|
|
|
ExprKind::Unary(UnOp::Not, e) => {
|
|
|
|
negated = !negated;
|
|
|
|
Some(e)
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
match expr.kind {
|
|
|
|
ExprKind::MethodCall(
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
[map, Expr {
|
|
|
|
kind: ExprKind::AddrOf(_, _, key),
|
|
|
|
span: key_span,
|
|
|
|
..
|
|
|
|
}],
|
|
|
|
_,
|
|
|
|
) if key_span.ctxt() == expr.span.ctxt() => {
|
|
|
|
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
|
|
|
|
let expr = ContainsExpr {
|
|
|
|
negated,
|
|
|
|
map,
|
|
|
|
key,
|
|
|
|
call_ctxt: expr.span.ctxt(),
|
2017-10-23 19:18:02 +00:00
|
|
|
};
|
2021-03-25 13:25:04 +00:00
|
|
|
if match_def_path(cx, id, &paths::BTREEMAP_CONTAINS_KEY) {
|
|
|
|
Some((MapType::BTree, expr))
|
|
|
|
} else if match_def_path(cx, id, &paths::HASHMAP_CONTAINS_KEY) {
|
|
|
|
Some((MapType::Hash, expr))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InsertExpr<'tcx> {
|
|
|
|
map: &'tcx Expr<'tcx>,
|
|
|
|
key: &'tcx Expr<'tcx>,
|
|
|
|
value: &'tcx Expr<'tcx>,
|
|
|
|
}
|
|
|
|
fn try_parse_insert(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> {
|
|
|
|
if let ExprKind::MethodCall(_, _, [map, key, value], _) = expr.kind {
|
|
|
|
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
|
|
|
|
if match_def_path(cx, id, &paths::BTREEMAP_INSERT) || match_def_path(cx, id, &paths::HASHMAP_INSERT) {
|
|
|
|
Some(InsertExpr { map, key, value })
|
|
|
|
} else {
|
|
|
|
None
|
2016-02-18 19:19:16 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
} else {
|
|
|
|
None
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct Insertion<'tcx> {
|
|
|
|
call: &'tcx Expr<'tcx>,
|
|
|
|
value: &'tcx Expr<'tcx>,
|
2016-02-18 19:19:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
// This visitor needs to do a multiple things:
|
|
|
|
// * Find all usages of the map. Only insertions into the map which share the same key are
|
|
|
|
// permitted. All others will prevent the lint.
|
|
|
|
// * Determine if the final statement executed is an insertion. This is needed to use `insert_with`.
|
|
|
|
// * Determine if there's any sub-expression that can't be placed in a closure.
|
|
|
|
// * Determine if there's only a single insert statement. This is needed to give better suggestions.
|
|
|
|
|
|
|
|
#[allow(clippy::struct_excessive_bools)]
|
|
|
|
struct InsertSearcher<'cx, 'i, 'tcx> {
|
|
|
|
cx: &'cx LateContext<'tcx>,
|
|
|
|
/// The map expression used in the contains call.
|
|
|
|
map: &'tcx Expr<'tcx>,
|
|
|
|
/// The key expression used in the contains call.
|
|
|
|
key: &'tcx Expr<'tcx>,
|
|
|
|
/// The context of the top level block. All insert calls must be in the same context.
|
|
|
|
ctxt: SyntaxContext,
|
|
|
|
/// Whether this expression can use the entry api.
|
|
|
|
can_use_entry: bool,
|
|
|
|
// A single insert expression has a slightly different suggestion.
|
|
|
|
is_single_insert: bool,
|
|
|
|
is_map_used: bool,
|
|
|
|
insertions: &'i mut Vec<Insertion<'tcx>>,
|
|
|
|
}
|
|
|
|
impl<'tcx> InsertSearcher<'_, '_, 'tcx> {
|
|
|
|
/// Visit the expression as a branch in control flow. Multiple insert calls can be used, but
|
|
|
|
/// only if they are on separate code paths. This will return whether the map was used in the
|
|
|
|
/// given expression.
|
|
|
|
fn visit_cond_arm(&mut self, e: &'tcx Expr<'_>) -> bool {
|
|
|
|
let is_map_used = self.is_map_used;
|
|
|
|
self.visit_expr(e);
|
|
|
|
let res = self.is_map_used;
|
|
|
|
self.is_map_used = is_map_used;
|
|
|
|
res
|
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for InsertSearcher<'_, '_, 'tcx> {
|
|
|
|
type Map = ErasedMap<'tcx>;
|
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
|
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Semi(e) | StmtKind::Expr(e) => self.visit_expr(e),
|
|
|
|
StmtKind::Local(Local { init: Some(e), .. }) => {
|
|
|
|
self.is_single_insert = false;
|
|
|
|
self.visit_expr(e);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
self.is_single_insert = false;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-01-09 07:13:22 +00:00
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
2021-03-25 13:25:04 +00:00
|
|
|
if !self.can_use_entry {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match try_parse_insert(self.cx, expr) {
|
|
|
|
Some(insert_expr) if SpanlessEq::new(self.cx).eq_expr(self.map, insert_expr.map) => {
|
|
|
|
// Multiple inserts, inserts with a different key, and inserts from a macro can't use the entry api.
|
|
|
|
if self.is_map_used
|
|
|
|
|| !SpanlessEq::new(self.cx).eq_expr(self.key, insert_expr.key)
|
|
|
|
|| expr.span.ctxt() != self.ctxt
|
|
|
|
{
|
|
|
|
self.can_use_entry = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.insertions.push(Insertion {
|
|
|
|
call: expr,
|
|
|
|
value: insert_expr.value,
|
|
|
|
});
|
|
|
|
self.is_map_used = true;
|
|
|
|
|
|
|
|
// The value doesn't affect whether there is only a single insert expression.
|
|
|
|
let is_single_insert = self.is_single_insert;
|
|
|
|
self.visit_expr(insert_expr.value);
|
|
|
|
self.is_single_insert = is_single_insert;
|
|
|
|
},
|
|
|
|
_ if SpanlessEq::new(self.cx).eq_expr(self.map, expr) => {
|
|
|
|
self.is_map_used = true;
|
|
|
|
},
|
|
|
|
_ => match expr.kind {
|
|
|
|
ExprKind::If(cond_expr, then_expr, Some(else_expr)) => {
|
|
|
|
self.is_single_insert = false;
|
|
|
|
self.visit_expr(cond_expr);
|
|
|
|
// Each branch may contain it's own insert expression.
|
|
|
|
let mut is_map_used = self.visit_cond_arm(then_expr);
|
|
|
|
is_map_used |= self.visit_cond_arm(else_expr);
|
|
|
|
self.is_map_used = is_map_used;
|
|
|
|
},
|
|
|
|
ExprKind::Match(scrutinee_expr, arms, _) => {
|
|
|
|
self.is_single_insert = false;
|
|
|
|
self.visit_expr(scrutinee_expr);
|
|
|
|
// Each branch may contain it's own insert expression.
|
|
|
|
let mut is_map_used = self.is_map_used;
|
|
|
|
for arm in arms {
|
|
|
|
if let Some(Guard::If(guard) | Guard::IfLet(_, guard)) = arm.guard {
|
|
|
|
self.visit_expr(guard)
|
|
|
|
}
|
|
|
|
is_map_used |= self.visit_cond_arm(arm.body);
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
self.is_map_used = is_map_used;
|
|
|
|
},
|
|
|
|
ExprKind::Loop(block, ..) => {
|
|
|
|
// Don't allow insertions inside of a loop.
|
|
|
|
let insertions_len = self.insertions.len();
|
|
|
|
self.visit_block(block);
|
|
|
|
if self.insertions.len() != insertions_len {
|
|
|
|
self.can_use_entry = false;
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
},
|
|
|
|
ExprKind::Block(block, _) => self.visit_block(block),
|
|
|
|
ExprKind::InlineAsm(_) | ExprKind::LlvmInlineAsm(_) => {
|
|
|
|
self.can_use_entry = false;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
self.is_single_insert = false;
|
|
|
|
walk_expr(self, expr);
|
|
|
|
},
|
|
|
|
},
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 19:19:16 +00:00
|
|
|
|
2021-03-25 13:25:04 +00:00
|
|
|
struct InsertSearchResults<'tcx> {
|
|
|
|
insertions: Vec<Insertion<'tcx>>,
|
|
|
|
is_single_insert: bool,
|
|
|
|
}
|
|
|
|
impl InsertSearchResults<'tcx> {
|
|
|
|
fn as_single_insertion(&self) -> Option<Insertion<'tcx>> {
|
|
|
|
self.is_single_insert.then(|| self.insertions[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snippet_occupied(&self, cx: &LateContext<'_>, mut span: Span, app: &mut Applicability) -> String {
|
|
|
|
let ctxt = span.ctxt();
|
|
|
|
let mut res = String::new();
|
|
|
|
for insertion in self.insertions.iter() {
|
|
|
|
res.push_str(&snippet_with_applicability(
|
|
|
|
cx,
|
|
|
|
span.until(insertion.call.span),
|
|
|
|
"..",
|
|
|
|
app,
|
|
|
|
));
|
|
|
|
if is_expr_used_or_unified(cx.tcx, insertion.call) {
|
|
|
|
res.push_str("Some(e.insert(");
|
|
|
|
res.push_str(&snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0);
|
|
|
|
res.push_str("))");
|
|
|
|
} else {
|
|
|
|
res.push_str("e.insert(");
|
|
|
|
res.push_str(&snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0);
|
|
|
|
res.push(')');
|
|
|
|
}
|
|
|
|
span = span.trim_start(insertion.call.span).unwrap_or(DUMMY_SP);
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
res.push_str(&snippet_with_applicability(cx, span, "..", app));
|
|
|
|
res
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
|
|
|
|
fn snippet_vacant(&self, cx: &LateContext<'_>, mut span: Span, app: &mut Applicability) -> String {
|
|
|
|
let ctxt = span.ctxt();
|
|
|
|
let mut res = String::new();
|
|
|
|
for insertion in self.insertions.iter() {
|
|
|
|
res.push_str(&snippet_with_applicability(
|
|
|
|
cx,
|
|
|
|
span.until(insertion.call.span),
|
|
|
|
"..",
|
|
|
|
app,
|
|
|
|
));
|
|
|
|
if is_expr_used_or_unified(cx.tcx, insertion.call) {
|
|
|
|
if is_expr_final_block_expr(cx.tcx, insertion.call) {
|
|
|
|
let _ = write!(
|
|
|
|
res,
|
|
|
|
"e.insert({});\n{}None",
|
|
|
|
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0,
|
|
|
|
snippet_indent(cx, insertion.call.span).as_deref().unwrap_or(""),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let _ = write!(
|
|
|
|
res,
|
|
|
|
"{{ e.insert({}); None }}",
|
|
|
|
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let _ = write!(
|
|
|
|
res,
|
|
|
|
"e.insert({})",
|
|
|
|
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
span = span.trim_start(insertion.call.span).unwrap_or(DUMMY_SP);
|
|
|
|
}
|
|
|
|
res.push_str(&snippet_with_applicability(cx, span, "..", app));
|
|
|
|
res
|
2016-12-06 10:32:21 +00:00
|
|
|
}
|
2015-12-21 23:35:56 +00:00
|
|
|
}
|
2021-03-25 13:25:04 +00:00
|
|
|
fn find_insert_calls(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
contains_expr: &ContainsExpr<'tcx>,
|
|
|
|
expr: &'tcx Expr<'_>,
|
|
|
|
) -> Option<InsertSearchResults<'tcx>> {
|
|
|
|
let mut insertions = Vec::new();
|
|
|
|
let mut s = InsertSearcher {
|
|
|
|
cx,
|
|
|
|
map: contains_expr.map,
|
|
|
|
key: contains_expr.key,
|
|
|
|
ctxt: expr.span.ctxt(),
|
|
|
|
insertions: &mut insertions,
|
|
|
|
is_map_used: false,
|
|
|
|
can_use_entry: true,
|
|
|
|
is_single_insert: true,
|
|
|
|
};
|
|
|
|
s.visit_expr(expr);
|
|
|
|
let is_single_insert = s.is_single_insert;
|
|
|
|
s.can_use_entry.then(|| InsertSearchResults {
|
|
|
|
insertions,
|
|
|
|
is_single_insert,
|
|
|
|
})
|
|
|
|
}
|