Skip to content

Commit e3ae76a

Browse files
committed
Auto merge of rust-lang#4495 - JohnTitor:fix-map-entry-false-positive, r=phansch
Fix `map_entry` false positive Fixes rust-lang#4219 changelog: Fix `map_entry` false positive
2 parents 98a2524 + 5c760f0 commit e3ae76a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clippy_lints/src/entry.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::SpanlessEq;
2-
use crate::utils::{get_item_name, higher, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty};
2+
use crate::utils::{get_item_name, higher, match_type, paths, snippet, snippet_opt, span_lint_and_then, walk_ptrs_ty};
33
use if_chain::if_chain;
44
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
55
use rustc::hir::*;
@@ -140,6 +140,7 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
140140
if path.ident.name == sym!(insert);
141141
if get_item_name(self.cx, self.map) == get_item_name(self.cx, &params[0]);
142142
if SpanlessEq::new(self.cx).eq_expr(self.key, &params[1]);
143+
if snippet_opt(self.cx, self.map.span) == snippet_opt(self.cx, params[0].span);
143144
then {
144145
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
145146
&format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |db| {

tests/ui/entry.rs

+14
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,18 @@ fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v:
6868
}
6969
}
7070

71+
// should not trigger, because the one uses different HashMap from another one
72+
fn insert_from_different_map<K: Eq + Hash, V>(m: HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
73+
if !m.contains_key(&k) {
74+
n.insert(k, v);
75+
}
76+
}
77+
78+
// should not trigger, because the one uses different HashMap from another one
79+
fn insert_from_different_map2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
80+
if !m.contains_key(&k) {
81+
n.insert(k, v);
82+
}
83+
}
84+
7185
fn main() {}

0 commit comments

Comments
 (0)