Use span_suggestion in ENTRY lint
This commit is contained in:
parent
f63329761f
commit
6fa9bf64d7
13
src/entry.rs
13
src/entry.rs
@ -1,7 +1,7 @@
|
||||
use rustc::lint::*;
|
||||
use rustc_front::hir::*;
|
||||
use syntax::codemap::Span;
|
||||
use utils::{get_item_name, is_exp_equal, match_type, snippet, span_help_and_lint, walk_ptrs_ty};
|
||||
use utils::{get_item_name, is_exp_equal, match_type, snippet, span_lint_and_then, walk_ptrs_ty};
|
||||
use utils::{BTREEMAP_PATH, HASHMAP_PATH};
|
||||
|
||||
/// **What it does:** This lint checks for uses of `contains_key` + `insert` on `HashMap` or
|
||||
@ -92,20 +92,21 @@ fn check_for_insert(cx: &LateContext, span: Span, map: &Expr, key: &Expr, expr:
|
||||
is_exp_equal(cx, key, ¶ms[1])
|
||||
], {
|
||||
let help = if sole_expr {
|
||||
format!("Consider using `{}.entry({}).or_insert({})`",
|
||||
format!("{}.entry({}).or_insert({})",
|
||||
snippet(cx, map.span, ".."),
|
||||
snippet(cx, params[1].span, ".."),
|
||||
snippet(cx, params[2].span, ".."))
|
||||
}
|
||||
else {
|
||||
format!("Consider using `{}.entry({})`",
|
||||
format!("{}.entry({})",
|
||||
snippet(cx, map.span, ".."),
|
||||
snippet(cx, params[1].span, ".."))
|
||||
};
|
||||
|
||||
span_help_and_lint(cx, MAP_ENTRY, span,
|
||||
&format!("usage of `contains_key` followed by `insert` on `{}`", kind),
|
||||
&help);
|
||||
span_lint_and_then(cx, MAP_ENTRY, span,
|
||||
&format!("usage of `contains_key` followed by `insert` on `{}`", kind), |db| {
|
||||
db.span_suggestion(span, "Consider using", help.clone());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,32 +11,37 @@ fn foo() {}
|
||||
|
||||
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); }
|
||||
//~^ERROR: usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~^^HELP: Consider using `m.entry(k).or_insert(v)`
|
||||
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~| HELP Consider
|
||||
//~| SUGGESTION m.entry(k).or_insert(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); }
|
||||
//~^ERROR: usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~^^HELP: Consider using `m.entry(k)`
|
||||
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~| HELP Consider
|
||||
//~| SUGGESTION m.entry(k)
|
||||
}
|
||||
|
||||
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 };
|
||||
//~^ERROR: usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~^^HELP: Consider using `m.entry(k).or_insert(v)`
|
||||
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~| HELP Consider
|
||||
//~| SUGGESTION m.entry(k).or_insert(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 };
|
||||
//~^ERROR: usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~^^HELP: Consider using `m.entry(k)`
|
||||
//~^ ERROR usage of `contains_key` followed by `insert` on `HashMap`
|
||||
//~| HELP Consider
|
||||
//~| SUGGESTION m.entry(k)
|
||||
}
|
||||
|
||||
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 };
|
||||
//~^ERROR: usage of `contains_key` followed by `insert` on `BTreeMap`
|
||||
//~^^HELP: Consider using `m.entry(k)`
|
||||
//~^ ERROR usage of `contains_key` followed by `insert` on `BTreeMap`
|
||||
//~| 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user