Skip to content

Commit 4368771

Browse files
committed
map_entry test: Fix semicolon, add run-rustfix
1 parent e2f4b60 commit 4368771

File tree

6 files changed

+34
-67
lines changed

6 files changed

+34
-67
lines changed

clippy_lints/src/entry.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
6565
} else {
6666
true
6767
}
68+
// XXXManishearth we can also check for if/else blocks containing `None`.
6869
};
6970

7071
let mut visitor = InsertVisitor {
@@ -147,7 +148,7 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
147148
&format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |db| {
148149
if self.sole_expr {
149150
let mut app = Applicability::MachineApplicable;
150-
let help = format!("{}.entry({}).or_insert({})",
151+
let help = format!("{}.entry({}).or_insert({});",
151152
snippet_with_applicability(self.cx, self.map.span, "map", &mut app),
152153
snippet_with_applicability(self.cx, params[1].span, "..", &mut app),
153154
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> {
164165
snippet(self.cx, self.map.span, "map"),
165166
snippet(self.cx, params[1].span, ".."));
166167

167-
db.span_help(
168+
db.span_label(
168169
self.span,
169170
&help,
170171
);

tests/ui/entry_fixable.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
3+
#![allow(unused, clippy::needless_pass_by_value)]
4+
#![warn(clippy::map_entry)]
5+
6+
use std::collections::{BTreeMap, HashMap};
7+
use std::hash::Hash;
8+
9+
fn foo() {}
10+
11+
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
12+
m.entry(k).or_insert(v);
13+
}
14+
15+
fn main() {}

tests/ui/entry_fixable.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#![allow(unused, clippy::needless_pass_by_value)]
24
#![warn(clippy::map_entry)]
35

@@ -12,10 +14,4 @@ fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
1214
}
1315
}
1416

15-
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
16-
if !m.contains_key(&k) {
17-
m.insert(o, v);
18-
}
19-
}
20-
2117
fn main() {}

tests/ui/entry_fixable.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error: usage of `contains_key` followed by `insert` on a `HashMap`
2-
--> $DIR/entry_fixable.rs:10:5
2+
--> $DIR/entry_fixable.rs:12:5
33
|
44
LL | / if !m.contains_key(&k) {
55
LL | | m.insert(k, v);
66
LL | | }
7-
| |_____^ help: consider using: `m.entry(k).or_insert(v)`
7+
| |_____^ help: consider using: `m.entry(k).or_insert(v);`
88
|
99
= note: `-D clippy::map-entry` implied by `-D warnings`
1010

tests/ui/entry_unfixable.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
4949
};
5050
}
5151

52+
// should not trigger
53+
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
54+
if !m.contains_key(&k) {
55+
m.insert(o, v);
56+
}
57+
}
58+
5259
// should not trigger, because the one uses different HashMap from another one
5360
fn insert_from_different_map<K: Eq + Hash, V>(m: HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
5461
if !m.contains_key(&k) {

tests/ui/entry_unfixable.stderr

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@ LL | | m.insert(k, v)
66
LL | | } else {
77
LL | | None
88
LL | | };
9-
| |_____^
9+
| |_____^ consider using `m.entry(k)`
1010
|
1111
= note: `-D clippy::map-entry` implied by `-D warnings`
12-
help: consider using `m.entry(k)`
13-
--> $DIR/entry_unfixable.rs:10:5
14-
|
15-
LL | / if !m.contains_key(&k) {
16-
LL | | m.insert(k, v)
17-
LL | | } else {
18-
LL | | None
19-
LL | | };
20-
| |_____^
2112

2213
error: usage of `contains_key` followed by `insert` on a `HashMap`
2314
--> $DIR/entry_unfixable.rs:18:5
@@ -27,17 +18,7 @@ LL | | None
2718
LL | | } else {
2819
LL | | m.insert(k, v)
2920
LL | | };
30-
| |_____^
31-
|
32-
help: consider using `m.entry(k)`
33-
--> $DIR/entry_unfixable.rs:18:5
34-
|
35-
LL | / if m.contains_key(&k) {
36-
LL | | None
37-
LL | | } else {
38-
LL | | m.insert(k, v)
39-
LL | | };
40-
| |_____^
21+
| |_____^ consider using `m.entry(k)`
4122

4223
error: usage of `contains_key` followed by `insert` on a `HashMap`
4324
--> $DIR/entry_unfixable.rs:26:5
@@ -48,18 +29,7 @@ LL | | m.insert(k, v)
4829
LL | | } else {
4930
LL | | None
5031
LL | | };
51-
| |_____^
52-
|
53-
help: consider using `m.entry(k)`
54-
--> $DIR/entry_unfixable.rs:26:5
55-
|
56-
LL | / if !m.contains_key(&k) {
57-
LL | | foo();
58-
LL | | m.insert(k, v)
59-
LL | | } else {
60-
LL | | None
61-
LL | | };
62-
| |_____^
32+
| |_____^ consider using `m.entry(k)`
6333

6434
error: usage of `contains_key` followed by `insert` on a `HashMap`
6535
--> $DIR/entry_unfixable.rs:35:5
@@ -70,18 +40,7 @@ LL | | } else {
7040
LL | | foo();
7141
LL | | m.insert(k, v)
7242
LL | | };
73-
| |_____^
74-
|
75-
help: consider using `m.entry(k)`
76-
--> $DIR/entry_unfixable.rs:35:5
77-
|
78-
LL | / if m.contains_key(&k) {
79-
LL | | None
80-
LL | | } else {
81-
LL | | foo();
82-
LL | | m.insert(k, v)
83-
LL | | };
84-
| |_____^
43+
| |_____^ consider using `m.entry(k)`
8544

8645
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
8746
--> $DIR/entry_unfixable.rs:44:5
@@ -92,18 +51,7 @@ LL | | m.insert(k, v)
9251
LL | | } else {
9352
LL | | None
9453
LL | | };
95-
| |_____^
96-
|
97-
help: consider using `m.entry(k)`
98-
--> $DIR/entry_unfixable.rs:44:5
99-
|
100-
LL | / if !m.contains_key(&k) {
101-
LL | | foo();
102-
LL | | m.insert(k, v)
103-
LL | | } else {
104-
LL | | None
105-
LL | | };
106-
| |_____^
54+
| |_____^ consider using `m.entry(k)`
10755

10856
error: aborting due to 5 previous errors
10957

0 commit comments

Comments
 (0)