Use span_suggestion in entry lints

This commit is contained in:
mcarton 2016-07-03 19:24:44 +02:00
parent ffa840d4f2
commit 2f259b8cd3
No known key found for this signature in database
GPG key ID: 5E427C794CBA45E8
2 changed files with 24 additions and 18 deletions

View file

@ -121,21 +121,21 @@ impl<'a, 'tcx, 'v, 'b> Visitor<'v> for InsertVisitor<'a, 'tcx, 'b> {
SpanlessEq::new(self.cx).eq_expr(self.key, &params[1]) SpanlessEq::new(self.cx).eq_expr(self.key, &params[1])
], { ], {
span_lint_and_then(self.cx, MAP_ENTRY, self.span, span_lint_and_then(self.cx, MAP_ENTRY, self.span,
&format!("usage of `contains_key` followed by `insert` on `{}`", self.ty), |db| { &format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |db| {
if self.sole_expr { if self.sole_expr {
let help = format!("{}.entry({}).or_insert({})", let help = format!("{}.entry({}).or_insert({})",
snippet(self.cx, self.map.span, "map"), snippet(self.cx, self.map.span, "map"),
snippet(self.cx, params[1].span, ".."), snippet(self.cx, params[1].span, ".."),
snippet(self.cx, params[2].span, "..")); snippet(self.cx, params[2].span, ".."));
db.span_suggestion(self.span, "Consider using", help); db.span_suggestion(self.span, "consider using", help);
} }
else { else {
let help = format!("Consider using `{}.entry({})`", let help = format!("{}.entry({})",
snippet(self.cx, self.map.span, "map"), snippet(self.cx, self.map.span, "map"),
snippet(self.cx, params[1].span, "..")); snippet(self.cx, params[1].span, ".."));
db.span_note(self.span, &help); db.span_suggestion(self.span, "consider using", help);
} }
}); });
}} }}

View file

@ -11,45 +11,51 @@ fn foo() {}
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) { fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) { m.insert(k, v); } if !m.contains_key(&k) { m.insert(k, v); }
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `HashMap`
//~| HELP Consider //~| HELP consider
//~| SUGGESTION m.entry(k).or_insert(v) //~| SUGGESTION m.entry(k).or_insert(v)
} }
fn insert_if_absent1<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) { fn insert_if_absent1<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) { foo(); m.insert(k, v); } if !m.contains_key(&k) { foo(); m.insert(k, v); }
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `HashMap`
//~| NOTE Consider using `m.entry(k)` //~| HELP consider
//~| SUGGESTION m.entry(k)
} }
fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) { fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) { m.insert(k, v) } else { None }; if !m.contains_key(&k) { m.insert(k, v) } else { None };
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `HashMap`
//~| NOTE Consider using `m.entry(k)` //~| HELP consider
//~| SUGGESTION m.entry(k)
} }
fn insert_if_present2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) { fn insert_if_present2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
if m.contains_key(&k) { None } else { m.insert(k, v) }; if m.contains_key(&k) { None } else { m.insert(k, v) };
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `HashMap`
//~| NOTE Consider using `m.entry(k)` //~| HELP consider
//~| SUGGESTION m.entry(k)
} }
fn insert_if_absent3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) { fn insert_if_absent3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) { foo(); m.insert(k, v) } else { None }; if !m.contains_key(&k) { foo(); m.insert(k, v) } else { None };
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `HashMap`
//~| NOTE Consider using `m.entry(k)` //~| HELP consider
//~| SUGGESTION m.entry(k)
} }
fn insert_if_present3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) { fn insert_if_present3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
if m.contains_key(&k) { None } else { foo(); m.insert(k, v) }; if m.contains_key(&k) { None } else { foo(); m.insert(k, v) };
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `HashMap`
//~| NOTE Consider using `m.entry(k)` //~| HELP consider
//~| SUGGESTION m.entry(k)
} }
fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) { fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) { foo(); m.insert(k, v) } else { None }; if !m.contains_key(&k) { foo(); m.insert(k, v) } else { None };
//~^ ERROR usage of `contains_key` followed by `insert` on `BTreeMap` //~^ ERROR usage of `contains_key` followed by `insert` on a `BTreeMap`
//~| NOTE Consider using `m.entry(k)` //~| HELP consider
//~| SUGGESTION m.entry(k)
} }
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) { fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {