map_entry test: Fix semicolon, add run-rustfix
This commit is contained in:
parent
e2f4b60661
commit
4368771548
@ -65,6 +65,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
|
|||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
// XXXManishearth we can also check for if/else blocks containing `None`.
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut visitor = InsertVisitor {
|
let mut visitor = InsertVisitor {
|
||||||
@ -147,7 +148,7 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
|
|||||||
&format!("usage of `contains_key` followed by `insert` on a `{}`", 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 mut app = Applicability::MachineApplicable;
|
let mut app = Applicability::MachineApplicable;
|
||||||
let help = format!("{}.entry({}).or_insert({})",
|
let help = format!("{}.entry({}).or_insert({});",
|
||||||
snippet_with_applicability(self.cx, self.map.span, "map", &mut app),
|
snippet_with_applicability(self.cx, self.map.span, "map", &mut app),
|
||||||
snippet_with_applicability(self.cx, params[1].span, "..", &mut app),
|
snippet_with_applicability(self.cx, params[1].span, "..", &mut app),
|
||||||
snippet_with_applicability(self.cx, params[2].span, "..", &mut app));
|
snippet_with_applicability(self.cx, params[2].span, "..", &mut app));
|
||||||
@ -164,7 +165,7 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
|
|||||||
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_help(
|
db.span_label(
|
||||||
self.span,
|
self.span,
|
||||||
&help,
|
&help,
|
||||||
);
|
);
|
||||||
|
15
tests/ui/entry_fixable.fixed
Normal file
15
tests/ui/entry_fixable.fixed
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#![allow(unused, clippy::needless_pass_by_value)]
|
||||||
|
#![warn(clippy::map_entry)]
|
||||||
|
|
||||||
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
use std::hash::Hash;
|
||||||
|
|
||||||
|
fn foo() {}
|
||||||
|
|
||||||
|
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
|
||||||
|
m.entry(k).or_insert(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
@ -1,3 +1,5 @@
|
|||||||
|
// run-rustfix
|
||||||
|
|
||||||
#![allow(unused, clippy::needless_pass_by_value)]
|
#![allow(unused, clippy::needless_pass_by_value)]
|
||||||
#![warn(clippy::map_entry)]
|
#![warn(clippy::map_entry)]
|
||||||
|
|
||||||
@ -12,10 +14,4 @@ fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
|
|
||||||
if !m.contains_key(&k) {
|
|
||||||
m.insert(o, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||||
--> $DIR/entry_fixable.rs:10:5
|
--> $DIR/entry_fixable.rs:12:5
|
||||||
|
|
|
|
||||||
LL | / if !m.contains_key(&k) {
|
LL | / if !m.contains_key(&k) {
|
||||||
LL | | m.insert(k, v);
|
LL | | m.insert(k, v);
|
||||||
LL | | }
|
LL | | }
|
||||||
| |_____^ help: consider using: `m.entry(k).or_insert(v)`
|
| |_____^ help: consider using: `m.entry(k).or_insert(v);`
|
||||||
|
|
|
|
||||||
= note: `-D clippy::map-entry` implied by `-D warnings`
|
= note: `-D clippy::map-entry` implied by `-D warnings`
|
||||||
|
|
||||||
|
@ -49,6 +49,13 @@ fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// should not trigger
|
||||||
|
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
|
||||||
|
if !m.contains_key(&k) {
|
||||||
|
m.insert(o, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// should not trigger, because the one uses different HashMap from another one
|
// 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) {
|
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) {
|
if !m.contains_key(&k) {
|
||||||
|
@ -6,18 +6,9 @@ LL | | m.insert(k, v)
|
|||||||
LL | | } else {
|
LL | | } else {
|
||||||
LL | | None
|
LL | | None
|
||||||
LL | | };
|
LL | | };
|
||||||
| |_____^
|
| |_____^ consider using `m.entry(k)`
|
||||||
|
|
|
|
||||||
= note: `-D clippy::map-entry` implied by `-D warnings`
|
= note: `-D clippy::map-entry` implied by `-D warnings`
|
||||||
help: consider using `m.entry(k)`
|
|
||||||
--> $DIR/entry_unfixable.rs:10:5
|
|
||||||
|
|
|
||||||
LL | / if !m.contains_key(&k) {
|
|
||||||
LL | | m.insert(k, v)
|
|
||||||
LL | | } else {
|
|
||||||
LL | | None
|
|
||||||
LL | | };
|
|
||||||
| |_____^
|
|
||||||
|
|
||||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||||
--> $DIR/entry_unfixable.rs:18:5
|
--> $DIR/entry_unfixable.rs:18:5
|
||||||
@ -27,17 +18,7 @@ LL | | None
|
|||||||
LL | | } else {
|
LL | | } else {
|
||||||
LL | | m.insert(k, v)
|
LL | | m.insert(k, v)
|
||||||
LL | | };
|
LL | | };
|
||||||
| |_____^
|
| |_____^ consider using `m.entry(k)`
|
||||||
|
|
|
||||||
help: consider using `m.entry(k)`
|
|
||||||
--> $DIR/entry_unfixable.rs:18:5
|
|
||||||
|
|
|
||||||
LL | / if m.contains_key(&k) {
|
|
||||||
LL | | None
|
|
||||||
LL | | } else {
|
|
||||||
LL | | m.insert(k, v)
|
|
||||||
LL | | };
|
|
||||||
| |_____^
|
|
||||||
|
|
||||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||||
--> $DIR/entry_unfixable.rs:26:5
|
--> $DIR/entry_unfixable.rs:26:5
|
||||||
@ -48,18 +29,7 @@ LL | | m.insert(k, v)
|
|||||||
LL | | } else {
|
LL | | } else {
|
||||||
LL | | None
|
LL | | None
|
||||||
LL | | };
|
LL | | };
|
||||||
| |_____^
|
| |_____^ consider using `m.entry(k)`
|
||||||
|
|
|
||||||
help: consider using `m.entry(k)`
|
|
||||||
--> $DIR/entry_unfixable.rs:26:5
|
|
||||||
|
|
|
||||||
LL | / if !m.contains_key(&k) {
|
|
||||||
LL | | foo();
|
|
||||||
LL | | m.insert(k, v)
|
|
||||||
LL | | } else {
|
|
||||||
LL | | None
|
|
||||||
LL | | };
|
|
||||||
| |_____^
|
|
||||||
|
|
||||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||||
--> $DIR/entry_unfixable.rs:35:5
|
--> $DIR/entry_unfixable.rs:35:5
|
||||||
@ -70,18 +40,7 @@ LL | | } else {
|
|||||||
LL | | foo();
|
LL | | foo();
|
||||||
LL | | m.insert(k, v)
|
LL | | m.insert(k, v)
|
||||||
LL | | };
|
LL | | };
|
||||||
| |_____^
|
| |_____^ consider using `m.entry(k)`
|
||||||
|
|
|
||||||
help: consider using `m.entry(k)`
|
|
||||||
--> $DIR/entry_unfixable.rs:35:5
|
|
||||||
|
|
|
||||||
LL | / if m.contains_key(&k) {
|
|
||||||
LL | | None
|
|
||||||
LL | | } else {
|
|
||||||
LL | | foo();
|
|
||||||
LL | | m.insert(k, v)
|
|
||||||
LL | | };
|
|
||||||
| |_____^
|
|
||||||
|
|
||||||
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
|
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
|
||||||
--> $DIR/entry_unfixable.rs:44:5
|
--> $DIR/entry_unfixable.rs:44:5
|
||||||
@ -92,18 +51,7 @@ LL | | m.insert(k, v)
|
|||||||
LL | | } else {
|
LL | | } else {
|
||||||
LL | | None
|
LL | | None
|
||||||
LL | | };
|
LL | | };
|
||||||
| |_____^
|
| |_____^ consider using `m.entry(k)`
|
||||||
|
|
|
||||||
help: consider using `m.entry(k)`
|
|
||||||
--> $DIR/entry_unfixable.rs:44:5
|
|
||||||
|
|
|
||||||
LL | / if !m.contains_key(&k) {
|
|
||||||
LL | | foo();
|
|
||||||
LL | | m.insert(k, v)
|
|
||||||
LL | | } else {
|
|
||||||
LL | | None
|
|
||||||
LL | | };
|
|
||||||
| |_____^
|
|
||||||
|
|
||||||
error: aborting due to 5 previous errors
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user