Auto merge of #4495 - JohnTitor:fix-map-entry-false-positive, r=phansch

Fix `map_entry` false positive

Fixes #4219

changelog: Fix `map_entry` false positive
This commit is contained in:
bors 2019-09-04 16:25:49 +00:00
commit e3ae76aeac
2 changed files with 16 additions and 1 deletions

View File

@ -1,5 +1,5 @@
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, span_lint_and_then, walk_ptrs_ty};
use if_chain::if_chain;
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc::hir::*;
@ -140,6 +140,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr) {
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| {

View File

@ -68,4 +68,18 @@ fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v:
}
}
// should not trigger, because the one uses different HashMap from another one
fn insert_from_different_map<K: Eq + Hash, V>(m: HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) {
n.insert(k, v);
}
}
// should not trigger, because the one uses different HashMap from another one
fn insert_from_different_map2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) {
n.insert(k, v);
}
}
fn main() {}