Skip to content

Commit

Permalink
Auto merge of #4495 - JohnTitor:fix-map-entry-false-positive, r=phansch
Browse files Browse the repository at this point in the history
Fix `map_entry` false positive

Fixes #4219

changelog: Fix `map_entry` false positive
  • Loading branch information
bors committed Sep 4, 2019
2 parents 98a2524 + 5c760f0 commit e3ae76a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -140,6 +140,7 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
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| {
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

0 comments on commit e3ae76a

Please sign in to comment.